TGParser.cpp revision a1b1b79be15c4b79a4282f148085ebad1cf877ca
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 67f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (RecordVal *ERV = CurRec->getValue(RV.getName())) { 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. 8221870411d9c1807526b613d04c2ebae5a43c263bBob Wilsonbool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &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) 90f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Value '" + ValName + "' unknown!"); 91f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 92f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Do not allow assignments like 'X = X'. This will just cause infinite loops 93f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // in the resolution machinery. 94f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (BitList.empty()) 9505bce0beee87512e52428d4b80f5a8e79a949576David Greene if (VarInit *VI = dynamic_cast<VarInit*>(V)) 96f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (VI->getName() == ValName) 97f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 9821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 99f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If we are assigning to a subset of the bits in the value... then we must be 100f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // assigning to a field of BitsRecTy, which must have a BitsInit 101f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // initializer. 102f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // 103f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!BitList.empty()) { 10405bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue()); 105f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurVal == 0) 106f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Value '" + ValName + "' is not a bits type"); 107f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 108f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Convert the incoming value to a bits type of the appropriate size... 10905bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size())); 110f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (BI == 0) { 11177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen V->convertInitializerTo(BitsRecTy::get(BitList.size())); 112f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Initializer is not compatible with bit range"); 113f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 11421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 115f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // We should have a BitsInit type now. 11605bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *BInit = dynamic_cast<BitsInit*>(BI); 117f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(BInit != 0); 118f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 11905bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init *, 16> NewBits(CurVal->getNumBits()); 120f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 121f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Loop over bits, assigning values as appropriate. 122f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = BitList.size(); i != e; ++i) { 123f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner unsigned Bit = BitList[i]; 124ca7fd3de360b266783438666054dba198ff77ba2David Greene if (NewBits[Bit]) 125f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" + 126f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner ValName + "' more than once"); 127ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[Bit] = BInit->getBit(i); 128f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 129f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 130f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) 131ca7fd3de360b266783438666054dba198ff77ba2David Greene if (NewBits[i] == 0) 132ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[i] = CurVal->getBit(i); 133f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 134dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene V = BitsInit::get(NewBits); 135f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 136f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 137f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (RV->setValue(V)) 13821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson return Error(Loc, "Value '" + ValName + "' of type '" + 13921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson RV->getType()->getAsString() + 1405d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner "' is incompatible with initializer '" + V->getAsString() +"'"); 141f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 142f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 143f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 144f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template 145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// args as SubClass's template arguments. 146aff9c270de8de7d1a0bc138d391bc67136bad58eCedric Venetbool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { 147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *SC = SubClass.Rec; 148f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add all of the values in the subclass into the current class. 149f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<RecordVal> &Vals = SC->getValues(); 150f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = Vals.size(); i != e; ++i) 151f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddValue(CurRec, SubClass.RefLoc, Vals[i])) 152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 153f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<std::string> &TArgs = SC->getTemplateArgs(); 155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 156f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Ensure that an appropriate number of template arguments are specified. 157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (TArgs.size() < SubClass.TemplateArgs.size()) 158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc, "More template args specified than expected"); 15921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 160f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Loop over all of the template arguments, setting them to the specified 161f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // value or leaving them as the default if necessary. 162f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 163f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (i < SubClass.TemplateArgs.size()) { 164f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If a value is specified for this template arg, set it now. 16521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), 166f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClass.TemplateArgs[i])) 167f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 16821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 169f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Resolve it next. 170f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 17121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 172f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Now remove it. 173f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->removeValue(TArgs[i]); 174f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 175f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 176f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc,"Value not specified for template argument #" 17721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson + utostr(i) + " (" + TArgs[i] + ") of subclass '" + 178f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SC->getName() + "'!"); 179f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 180f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 181f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 182f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Since everything went well, we can now set the "superclass" list for the 183f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // current record. 184f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<Record*> &SCs = SC->getSuperClasses(); 185f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = SCs.size(); i != e; ++i) { 186f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isSubClassOf(SCs[i])) 187f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc, 188f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "Already subclass of '" + SCs[i]->getName() + "'!\n"); 189f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->addSuperClass(SCs[i]); 190f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 19121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 192f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isSubClassOf(SC)) 193f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc, 194f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "Already subclass of '" + SC->getName() + "'!\n"); 195f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->addSuperClass(SC); 196f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 197f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 198f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 199de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// AddSubMultiClass - Add SubMultiClass as a subclass to 200440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson/// CurMC, resolving its template args as SubMultiClass's 201de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// template arguments. 202440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilsonbool TGParser::AddSubMultiClass(MultiClass *CurMC, 2031d512df4a9e249ceb6d6b69e89527c75d872ecd1Bob Wilson SubMultiClassReference &SubMultiClass) { 204de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *SMC = SubMultiClass.MC; 205440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson Record *CurRec = &CurMC->Rec; 206de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 207440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson const std::vector<RecordVal> &MCVals = CurRec->getValues(); 208de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 209de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the values in the subclass into the current class. 210de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues(); 211de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = SMCVals.size(); i != e; ++i) 212de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i])) 213de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 214de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 215440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson int newDefStart = CurMC->DefPrototypes.size(); 216d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 217de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the defs in the subclass into the current multiclass. 218de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(), 219de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene iend = SMC->DefPrototypes.end(); 220de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene i != iend; 221de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene ++i) { 222de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Clone the def and add it to the current multiclass 223de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Record *NewDef = new Record(**i); 224de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 225de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the values in the superclass into the current def. 226de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = MCVals.size(); i != e; ++i) 227de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i])) 228de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 229de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 230440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson CurMC->DefPrototypes.push_back(NewDef); 231de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 23232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 233de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs(); 234de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 235d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // Ensure that an appropriate number of template arguments are 236d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // specified. 237de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) 238d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return Error(SubMultiClass.RefLoc, 239d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene "More template args specified than expected"); 24032558650ae09247b31aa89cdee64e8c849771024Bob Wilson 241de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Loop over all of the template arguments, setting them to the specified 242de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // value or leaving them as the default if necessary. 243de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { 244de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (i < SubMultiClass.TemplateArgs.size()) { 245d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // If a value is specified for this template arg, set it in the 246d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // superclass now. 247d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], 24832558650ae09247b31aa89cdee64e8c849771024Bob Wilson std::vector<unsigned>(), 249de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass.TemplateArgs[i])) 250de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 251de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 252de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Resolve it next. 253de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i])); 25432558650ae09247b31aa89cdee64e8c849771024Bob Wilson 255de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Now remove it. 256de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene CurRec->removeValue(SMCTArgs[i]); 257de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 258d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // If a value is specified for this template arg, set it in the 259d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // new defs now. 260d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene for (MultiClass::RecordVector::iterator j = 261440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson CurMC->DefPrototypes.begin() + newDefStart, 262440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson jend = CurMC->DefPrototypes.end(); 263de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene j != jend; 264de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene ++j) { 265de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Record *Def = *j; 266de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 267d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], 26832558650ae09247b31aa89cdee64e8c849771024Bob Wilson std::vector<unsigned>(), 269de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass.TemplateArgs[i])) 270de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 271de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 272de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Resolve it next. 273de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Def->resolveReferencesTo(Def->getValue(SMCTArgs[i])); 274de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 275de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Now remove it 276de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Def->removeValue(SMCTArgs[i]); 277de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 278de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) { 279d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return Error(SubMultiClass.RefLoc, 280d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene "Value not specified for template argument #" 28132558650ae09247b31aa89cdee64e8c849771024Bob Wilson + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" + 282de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SMC->Rec.getName() + "'!"); 283de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 284de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 285de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 286de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return false; 287de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 288de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 289f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 290f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// Parser Code 291f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 292f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 293f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// isObjectStart - Return true if this is a valid first token for an Object. 294f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstatic bool isObjectStart(tgtok::TokKind K) { 295f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return K == tgtok::Class || K == tgtok::Def || 29621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass; 297f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 298f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 299df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattnerstatic std::string GetNewAnonymousName() { 300df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner static unsigned AnonCounter = 0; 301df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner return "anonymous."+utostr(AnonCounter++); 302df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner} 303df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner 304f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectName - If an object name is specified, return it. Otherwise, 305f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// return an anonymous name. 306f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectName ::= ID 307f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectName ::= /*empty*/ 308f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 309f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::string TGParser::ParseObjectName() { 310df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner if (Lex.getCode() != tgtok::Id) 311df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner return GetNewAnonymousName(); 312c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 313df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner std::string Ret = Lex.getCurStrVal(); 314df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner Lex.Lex(); 315df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner return Ret; 316f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 317f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 318f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 319f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseClassID - Parse and resolve a reference to a class name. This returns 320f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// null on error. 321f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 322f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ClassID ::= ID 323f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 324f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecord *TGParser::ParseClassID() { 325f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 326f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected name for ClassID"); 327f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 328f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 32921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 330f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Result = Records.getClass(Lex.getCurStrVal()); 331f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) 332f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 33321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 334f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 335f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 336f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 337f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 33832558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ParseMultiClassID - Parse and resolve a reference to a multiclass name. 33932558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// This returns null on error. 340de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 341de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// MultiClassID ::= ID 342de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 343de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneMultiClass *TGParser::ParseMultiClassID() { 344de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::Id) { 345de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("expected name for ClassID"); 346de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return 0; 347de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 34832558650ae09247b31aa89cdee64e8c849771024Bob Wilson 349de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; 350de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result == 0) 351de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 35232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 353de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 354de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 355de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 356de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 357f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecord *TGParser::ParseDefmID() { 358f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 359f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected multiclass name"); 360f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 361f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 36221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 363f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner MultiClass *MC = MultiClasses[Lex.getCurStrVal()]; 364f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (MC == 0) { 365f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); 366f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 367f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 36821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 369f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 370f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return &MC->Rec; 37121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson} 372f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 373f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 374f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseSubClassReference - Parse a reference to a subclass or to a templated 375f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// subclass. This returns a SubClassRefTy with a null Record* on error. 376f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 377f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SubClassRef ::= ClassID 378f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SubClassRef ::= ClassID '<' ValueList '>' 379f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 380f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerSubClassReference TGParser:: 381f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerParseSubClassReference(Record *CurRec, bool isDefm) { 382f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference Result; 383f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.RefLoc = Lex.getLoc(); 38421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 385f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (isDefm) 386f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = ParseDefmID(); 387f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner else 388f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = ParseClassID(); 389f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result.Rec == 0) return Result; 39021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 391f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there is no template arg list, we're done. 392f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::less) 393f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 394f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the '<' 39521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 396f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::greater) { 397f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("subclass reference requires a non-empty list of template values"); 398f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; 399f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 400f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 40121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 402e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.TemplateArgs = ParseValueList(CurRec, Result.Rec); 403f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result.TemplateArgs.empty()) { 404f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; // Error parsing value list. 405f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 406f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 40721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 408f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 409f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' in template value list"); 410f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; 411f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 412f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 413f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 41421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 415f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 416f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 417f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 41832558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ParseSubMultiClassReference - Parse a reference to a subclass or to a 41932558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// templated submulticlass. This returns a SubMultiClassRefTy with a null 42032558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// Record* on error. 421de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 422de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// SubMultiClassRef ::= MultiClassID 423de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// SubMultiClassRef ::= MultiClassID '<' ValueList '>' 424de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 425de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneSubMultiClassReference TGParser:: 426de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneParseSubMultiClassReference(MultiClass *CurMC) { 427de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClassReference Result; 428de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.RefLoc = Lex.getLoc(); 42932558650ae09247b31aa89cdee64e8c849771024Bob Wilson 430de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = ParseMultiClassID(); 431de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result.MC == 0) return Result; 43232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 433de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // If there is no template arg list, we're done. 434de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::less) 435de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 436de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); // Eat the '<' 43732558650ae09247b31aa89cdee64e8c849771024Bob Wilson 438de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() == tgtok::greater) { 439de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("subclass reference requires a non-empty list of template values"); 440de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; 441de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 442de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 44332558650ae09247b31aa89cdee64e8c849771024Bob Wilson 444e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec); 445de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result.TemplateArgs.empty()) { 446de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; // Error parsing value list. 447de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 448de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 44932558650ae09247b31aa89cdee64e8c849771024Bob Wilson 450de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::greater) { 451de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("expected '>' in template value list"); 452de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; 453de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 454de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 455de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 456de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 457de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 458de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 459de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 460f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseRangePiece - Parse a bit/value range. 461f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL 462f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL '-' INTVAL 463f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL INTVAL 464f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { 465811281e788432d3136de13f72d05790eced52d92Chris Lattner if (Lex.getCode() != tgtok::IntVal) { 466811281e788432d3136de13f72d05790eced52d92Chris Lattner TokError("expected integer or bitrange"); 467811281e788432d3136de13f72d05790eced52d92Chris Lattner return true; 468811281e788432d3136de13f72d05790eced52d92Chris Lattner } 46963f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman int64_t Start = Lex.getCurIntVal(); 47063f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman int64_t End; 47121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 472f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Start < 0) 473f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("invalid range, cannot be negative"); 47421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 475f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.Lex()) { // eat first character. 47621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson default: 477f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 478f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 479f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::minus: 480f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::IntVal) { 481f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected integer value as end of range"); 482f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 483f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner End = Lex.getCurIntVal(); 485f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::IntVal: 487f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner End = -Lex.getCurIntVal(); 488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 489f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 49021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (End < 0) 491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("invalid range, cannot be negative"); 492f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 49321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 494f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add to the range. 495f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Start < End) { 496f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (; Start <= End; ++Start) 497f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 498f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 499f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (; Start >= End; --Start) 500f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 501f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 502f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 503f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 504f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 505f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseRangeList - Parse a list of scalars and ranges into scalar values. 506f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 507f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangeList ::= RangePiece (',' RangePiece)* 508f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 509f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::vector<unsigned> TGParser::ParseRangeList() { 510f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Result; 51121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 512f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the first piece. 513f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseRangePiece(Result)) 514f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<unsigned>(); 515f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 516f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the comma. 517f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 518f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the next range piece. 519f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseRangePiece(Result)) 520f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<unsigned>(); 521f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 522f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 523f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 524f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 525f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. 526f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalRangeList ::= '<' RangeList '>' 527f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalRangeList ::= /*empty*/ 528f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { 529f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::less) 530f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 53121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 5321e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc StartLoc = Lex.getLoc(); 533f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '<' 53421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 535f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the range list. 536f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges = ParseRangeList(); 537f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return true; 53821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 539f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 540f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of range list"); 541f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(StartLoc, "to match this '<'"); 542f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 543f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '>'. 544f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 545f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 546f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 547f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. 548f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalBitList ::= '{' RangeList '}' 549f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalBitList ::= /*empty*/ 550f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { 551f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) 552f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 55321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 5541e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc StartLoc = Lex.getLoc(); 555f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 55621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 557f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the range list. 558f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges = ParseRangeList(); 559f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return true; 56021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 561f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 562f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit list"); 563f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(StartLoc, "to match this '{'"); 564f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 565f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '}'. 566f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 567f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 568f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 569f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 570f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseType - Parse and return a tblgen type. This returns null on error. 571f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 572f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= STRING // string type 573f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= BIT // bit type 574f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= BITS '<' INTVAL '>' // bits<x> type 575f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= INT // int type 576f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= LIST '<' Type '>' // list<x> type 577f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= CODE // code type 578f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= DAG // dag type 579f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= ClassID // Record Type 580f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 581f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecTy *TGParser::ParseType() { 582f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 583f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: TokError("Unknown token when expecting a type"); return 0; 58477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::String: Lex.Lex(); return StringRecTy::get(); 58577f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Bit: Lex.Lex(); return BitRecTy::get(); 58677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Int: Lex.Lex(); return IntRecTy::get(); 58777f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Code: Lex.Lex(); return CodeRecTy::get(); 58877f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Dag: Lex.Lex(); return DagRecTy::get(); 589f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Id: 59077f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen if (Record *R = ParseClassID()) return RecordRecTy::get(R); 591f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Bits: { 593f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) { // Eat 'bits' 594f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '<' after bits type"); 595f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 596f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 597f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::IntVal) { // Eat '<' 598f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected integer in bits<n> type"); 599f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 600f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 60163f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman uint64_t Val = Lex.getCurIntVal(); 602f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::greater) { // Eat count. 603f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of bits<n> type"); 604f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 605f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 606f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '>' 60777f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return BitsRecTy::get(Val); 608f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 609f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::List: { 610f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) { // Eat 'bits' 611f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '<' after list type"); 612f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 613f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 614f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '<' 615f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecTy *SubType = ParseType(); 616f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SubType == 0) return 0; 61721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 618f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 619f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of list<ty> type"); 620f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 621f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 622f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '>' 62377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return ListRecTy::get(SubType); 624f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 62521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 626f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 627f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 628f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseIDValue - Parse an ID as a value and decode what it means. 629f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 630f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def local value] 631f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def template arg] 632f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [multiclass local value] 633f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [multiclass template argument] 634f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def name] 635f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 63605bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseIDValue(Record *CurRec) { 637f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); 638f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 6391e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc Loc = Lex.getLoc(); 640f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 641f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseIDValue(CurRec, Name, Loc); 642f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 643f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 644f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID 645f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// has already been read. 64605bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseIDValue(Record *CurRec, 6471e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner const std::string &Name, SMLoc NameLoc) { 648f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 649f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (const RecordVal *RV = CurRec->getValue(Name)) 650dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(Name, RV->getType()); 65121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 652f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string TemplateArgName = CurRec->getName()+":"+Name; 653caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene if (CurMultiClass) 654caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene TemplateArgName = CurMultiClass->Rec.getName()+"::"+TemplateArgName; 655caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene 656f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isTemplateArg(TemplateArgName)) { 657f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const RecordVal *RV = CurRec->getValue(TemplateArgName); 658f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(RV && "Template arg doesn't exist??"); 659dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(TemplateArgName, RV->getType()); 660f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 661f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 66221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 663f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass) { 664f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string MCName = CurMultiClass->Rec.getName()+"::"+Name; 665f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass->Rec.isTemplateArg(MCName)) { 666f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); 667f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(RV && "Template arg doesn't exist??"); 668dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(MCName, RV->getType()); 669f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 670f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 67121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 672f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Record *D = Records.getDef(Name)) 67377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return DefInit::get(D); 674f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 675f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(NameLoc, "Variable not defined: '" + Name + "'"); 676f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 677f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 678f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 679d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// ParseOperation - Parse an operator. This returns null on error. 680d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 681d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// Operation ::= XOperator ['<' Type '>'] '(' Args ')' 682d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 68305bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseOperation(Record *CurRec) { 684d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene switch (Lex.getCode()) { 685d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene default: 686d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("unknown operation"); 687d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 688d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene break; 6891434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 6901434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 6911434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 692e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: { // Value ::= !unop '(' Value ')' 693e6c27de069225e1122c78385ad22a2ff656db8e6David Greene UnOpInit::UnaryOp Code; 694e6c27de069225e1122c78385ad22a2ff656db8e6David Greene RecTy *Type = 0; 695d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 696e6c27de069225e1122c78385ad22a2ff656db8e6David Greene switch (Lex.getCode()) { 697e6c27de069225e1122c78385ad22a2ff656db8e6David Greene default: assert(0 && "Unhandled code!"); 698e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: 699e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the operation 700e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Code = UnOpInit::CAST; 701d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 702e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Type = ParseOperatorType(); 703d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 704e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Type == 0) { 7055f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("did not get type for unary operator"); 706e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 707e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 708d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 709e6c27de069225e1122c78385ad22a2ff656db8e6David Greene break; 7101434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 7115f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 7121434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::HEAD; 7135f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 7141434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 7155f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 7161434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::TAIL; 7175f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 7181434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 7195f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 7201434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::EMPTY; 72177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = IntRecTy::get(); 7225f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 723e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 724e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Lex.getCode() != tgtok::l_paren) { 725e6c27de069225e1122c78385ad22a2ff656db8e6David Greene TokError("expected '(' after unary operator"); 726e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 727e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 728e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the '(' 729d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 73005bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *LHS = ParseValue(CurRec); 731e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (LHS == 0) return 0; 732d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 7331434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD 7341434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::TAIL 7351434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::EMPTY) { 73605bce0beee87512e52428d4b80f5a8e79a949576David Greene ListInit *LHSl = dynamic_cast<ListInit*>(LHS); 73705bce0beee87512e52428d4b80f5a8e79a949576David Greene StringInit *LHSs = dynamic_cast<StringInit*>(LHS); 73805bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS); 739e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LHSl == 0 && LHSs == 0 && LHSt == 0) { 740e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list or string type argument in unary operator"); 7415f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 7425f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7435f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSt) { 7445f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); 745e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType()); 746e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LType == 0 && SType == 0) { 747e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list or string type argumnet in unary operator"); 7485f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 7495f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7505f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7515f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene 7521434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD 7531434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::TAIL) { 754e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LHSl == 0 && LHSt == 0) { 755e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list type argumnet in unary operator"); 756e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 757e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 75821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 7595f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSl && LHSl->getSize() == 0) { 7605f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("empty list argument in unary operator"); 7615f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 7625f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7635f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSl) { 76405bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Item = LHSl->getElement(0); 76505bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *Itemt = dynamic_cast<TypedInit*>(Item); 7665f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (Itemt == 0) { 7675f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("untyped list element in unary operator"); 7685f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 7695f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7701434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD) { 7715f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = Itemt->getType(); 77221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 77377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = ListRecTy::get(Itemt->getType()); 7745f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 77521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 7765f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene assert(LHSt && "expected list type argument in unary operator"); 7775f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); 7785f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LType == 0) { 7795f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("expected list type argumnet in unary operator"); 7805f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 7815f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7821434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD) { 7835f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = LType->getElementType(); 78421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 7855f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = LType; 7865f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7875f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7885f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7895f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 7905f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene 791e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Lex.getCode() != tgtok::r_paren) { 792e6c27de069225e1122c78385ad22a2ff656db8e6David Greene TokError("expected ')' in unary operator"); 793e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 794e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 795e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the ')' 796dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass); 797e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 798d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 799d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XConcat: 80021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XSRA: 801d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XSRL: 802d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XSHL: 8036786d5e18387465d6106eaef0b7923526ef0bc10David Greene case tgtok::XEq: 804c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')' 8058d978a75b477c0e4861d937096518eebba639e72Chris Lattner tgtok::TokKind OpTok = Lex.getCode(); 8068d978a75b477c0e4861d937096518eebba639e72Chris Lattner SMLoc OpLoc = Lex.getLoc(); 8078d978a75b477c0e4861d937096518eebba639e72Chris Lattner Lex.Lex(); // eat the operation 8088d978a75b477c0e4861d937096518eebba639e72Chris Lattner 809d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene BinOpInit::BinaryOp Code; 810d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene RecTy *Type = 0; 811d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 8128d978a75b477c0e4861d937096518eebba639e72Chris Lattner switch (OpTok) { 813d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene default: assert(0 && "Unhandled code!"); 81477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break; 81577f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break; 81677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break; 81777f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break; 81877f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break; 81921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XStrConcat: 820d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Code = BinOpInit::STRCONCAT; 82177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = StringRecTy::get(); 822d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene break; 823d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 824c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 825d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::l_paren) { 826d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected '(' after binary operator"); 827d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 828d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 829d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the '(' 830d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 83105bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init*, 2> InitList; 832c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 8338d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.push_back(ParseValue(CurRec)); 8348d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.back() == 0) return 0; 835d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 8368d978a75b477c0e4861d937096518eebba639e72Chris Lattner while (Lex.getCode() == tgtok::comma) { 8378d978a75b477c0e4861d937096518eebba639e72Chris Lattner Lex.Lex(); // eat the ',' 83821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 8398d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.push_back(ParseValue(CurRec)); 8408d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.back() == 0) return 0; 8418d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 842d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 843d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::r_paren) { 8448d978a75b477c0e4861d937096518eebba639e72Chris Lattner TokError("expected ')' in operator"); 845d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 846d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 847d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the ')' 8488d978a75b477c0e4861d937096518eebba639e72Chris Lattner 8498d978a75b477c0e4861d937096518eebba639e72Chris Lattner // We allow multiple operands to associative operators like !strconcat as 8508d978a75b477c0e4861d937096518eebba639e72Chris Lattner // shorthand for nesting them. 8518d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (Code == BinOpInit::STRCONCAT) { 8528d978a75b477c0e4861d937096518eebba639e72Chris Lattner while (InitList.size() > 2) { 85305bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *RHS = InitList.pop_back_val(); 854dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type)) 855dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene ->Fold(CurRec, CurMultiClass); 8568d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.back() = RHS; 8578d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 8588d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 859c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 8608d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.size() == 2) 861dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) 8628d978a75b477c0e4861d937096518eebba639e72Chris Lattner ->Fold(CurRec, CurMultiClass); 863c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 8648d978a75b477c0e4861d937096518eebba639e72Chris Lattner Error(OpLoc, "expected two operands to operator"); 8658d978a75b477c0e4861d937096518eebba639e72Chris Lattner return 0; 866d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 867d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 8689bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 869beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 8704afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 8714afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TernOpInit::TernaryOp Code; 8724afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene RecTy *Type = 0; 8734afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 8744afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene tgtok::TokKind LexCode = Lex.getCode(); 8754afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the operation 8764afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene switch (LexCode) { 8774afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene default: assert(0 && "Unhandled code!"); 8789bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 8799bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene Code = TernOpInit::IF; 8809bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene break; 881beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 882beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene Code = TernOpInit::FOREACH; 883beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene break; 8844afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: 8854afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Code = TernOpInit::SUBST; 8864afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene break; 8874afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 8884afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::l_paren) { 8894afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected '(' after ternary operator"); 8904afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 8914afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 8924afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the '(' 8934afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 89405bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *LHS = ParseValue(CurRec); 8954afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (LHS == 0) return 0; 8964afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 8974afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::comma) { 8984afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ',' in ternary operator"); 8994afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 9004afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 9014afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ',' 90221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 90305bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *MHS = ParseValue(CurRec); 9044afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (MHS == 0) return 0; 9054afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 9064afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::comma) { 9074afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ',' in ternary operator"); 9084afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 9094afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 9104afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ',' 91121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 91205bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *RHS = ParseValue(CurRec); 9134afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (RHS == 0) return 0; 9144afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 9154afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::r_paren) { 9164afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ')' in binary operator"); 9174afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 9184afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 9194afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ')' 9204afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 9214afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene switch (LexCode) { 9224afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene default: assert(0 && "Unhandled code!"); 9239bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: { 924548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling // FIXME: The `!if' operator doesn't handle non-TypedInit well at 925548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling // all. This can be made much more robust. 92605bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS); 92705bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS); 928548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 929548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RecTy *MHSTy = 0; 930548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RecTy *RHSTy = 0; 931548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 932548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSt == 0 && RHSt == 0) { 93305bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS); 93405bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS); 935548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 936548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSbits && RHSbits && 937548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling MHSbits->getNumBits() == RHSbits->getNumBits()) { 93877f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = BitRecTy::get(); 939548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling break; 940548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else { 94105bce0beee87512e52428d4b80f5a8e79a949576David Greene BitInit *MHSbit = dynamic_cast<BitInit*>(MHS); 94205bce0beee87512e52428d4b80f5a8e79a949576David Greene BitInit *RHSbit = dynamic_cast<BitInit*>(RHS); 943548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 944548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSbit && RHSbit) { 94577f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = BitRecTy::get(); 946548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling break; 947548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } 948548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } 949548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else if (MHSt != 0 && RHSt != 0) { 950548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling MHSTy = MHSt->getType(); 951548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RHSTy = RHSt->getType(); 952548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } 953548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 954548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (!MHSTy || !RHSTy) { 9559bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene TokError("could not get type for !if"); 9569bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene return 0; 9579bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 958548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 959548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSTy->typeIsConvertibleTo(RHSTy)) { 960548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling Type = RHSTy; 961548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else if (RHSTy->typeIsConvertibleTo(MHSTy)) { 962548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling Type = MHSTy; 96321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 9649bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene TokError("inconsistent types for !if"); 9659bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene return 0; 9669bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 9679bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene break; 9689bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 969beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: { 97005bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS); 971beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene if (MHSt == 0) { 972beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene TokError("could not get type for !foreach"); 973beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene return 0; 974beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene } 975beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene Type = MHSt->getType(); 976beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene break; 977beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene } 9784afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { 97905bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS); 9804afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (RHSt == 0) { 9814afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("could not get type for !subst"); 9824afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 9834afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 9844afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Type = RHSt->getType(); 9854afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene break; 9864afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 9874afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 988dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec, 98921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson CurMultiClass); 9904afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 991d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 992d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("could not parse operation"); 993d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 994d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene} 995d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 996d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// ParseOperatorType - Parse a type for an operator. This returns 997d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// null on error. 998d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 999d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// OperatorType ::= '<' Type '>' 1000d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 1001a9ad04191cb56c42944b17980b8b2bb2afe11ab2Dan GohmanRecTy *TGParser::ParseOperatorType() { 1002d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene RecTy *Type = 0; 1003d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1004d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::less) { 1005d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1006d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1007d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1008d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the < 1009d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1010d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Type = ParseType(); 1011d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1012d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Type == 0) { 1013d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1014d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1015d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1016d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1017d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::greater) { 1018d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1019d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1020d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1021d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the > 1022d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1023d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return Type; 1024d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene} 1025d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1026d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1027f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseSimpleValue - Parse a tblgen value. This returns null on error. 1028f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1029f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= IDValue 1030f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= INTVAL 1031d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner/// SimpleValue ::= STRVAL+ 1032f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= CODEFRAGMENT 1033f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '?' 1034f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '{' ValueList '}' 1035f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= ID '<' ValueListNE '>' 1036f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '[' ValueList ']' 1037f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '(' IDValue DagArgList ')' 1038f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' 1039f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SHLTOK '(' Value ',' Value ')' 1040f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SRATOK '(' Value ',' Value ')' 1041f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SRLTOK '(' Value ',' Value ')' 1042f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' 1043f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 104405bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { 104505bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *R = 0; 1046f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 1047f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: TokError("Unknown token when parsing a value"); break; 1048dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; 1049d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner case tgtok::StrVal: { 1050d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner std::string Val = Lex.getCurStrVal(); 1051d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Lex.Lex(); 105221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1053da4231f134989af7dc6bd3408821ba573def27b2Jim Grosbach // Handle multiple consecutive concatenated strings. 1054d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner while (Lex.getCode() == tgtok::StrVal) { 1055d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Val += Lex.getCurStrVal(); 1056d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Lex.Lex(); 1057d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner } 105821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1059dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = StringInit::get(Val); 1060d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner break; 1061d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner } 1062f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::CodeFragment: 1063dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = CodeInit::get(Lex.getCurStrVal()); 1064578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner Lex.Lex(); 1065578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner break; 1066578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner case tgtok::question: 1067dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = UnsetInit::get(); 1068578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner Lex.Lex(); 1069578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner break; 1070f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Id: { 10711e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc NameLoc = Lex.getLoc(); 1072f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 1073f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) // consume the Id. 1074f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue 107521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1076f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Value ::= ID '<' ValueListNE '>' 1077f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() == tgtok::greater) { 1078f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected non-empty value list"); 1079f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1080f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1081e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1082f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // This is a CLASS<initvalslist> expression. This is supposed to synthesize 1083f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // a new anonymous definition, deriving from CLASS<initvalslist> with no 1084f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // body. 1085f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Class = Records.getClass(Name); 1086f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!Class) { 1087f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(NameLoc, "Expected a class name, got '" + Name + "'"); 1088f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1089f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1090e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 109105bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> ValueList = ParseValueList(CurRec, Class); 1092e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ValueList.empty()) return 0; 109321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1094e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() != tgtok::greater) { 1095e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected '>' at end of value list"); 1096e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1097e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1098e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '>' 109921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1100f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Create the new record, set it as CurRec temporarily. 1101f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner static unsigned AnonCounter = 0; 11029c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++), 11039c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner NameLoc, 11049c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner Records); 1105f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference SCRef; 1106f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.RefLoc = NameLoc; 1107f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.Rec = Class; 1108f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.TemplateArgs = ValueList; 1109f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add info about the subclass to NewRec. 1110f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddSubClass(NewRec, SCRef)) 1111f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1112f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner NewRec->resolveReferences(); 1113f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addDef(NewRec); 111421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1115f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // The result of the expression is a reference to the new record. 111677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return DefInit::get(NewRec); 111721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1118f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_brace: { // Value ::= '{' ValueList '}' 11191e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc BraceLoc = Lex.getLoc(); 1120f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 112105bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Vals; 112221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1123f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1124f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Vals = ParseValueList(CurRec); 1125f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Vals.empty()) return 0; 1126f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1127f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1128f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit list value"); 1129f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1130f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1131f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '}' 113221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 113305bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init *, 16> NewBits(Vals.size()); 1134ca7fd3de360b266783438666054dba198ff77ba2David Greene 1135f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 113605bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get()); 1137f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Bit == 0) { 11385d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ 11395d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner ") is not convertable to a bit"); 1140f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1141f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1142ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[Vals.size()-i-1] = Bit; 1143f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1144dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return BitsInit::get(NewBits); 1145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1146f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_square: { // Value ::= '[' ValueList ']' 1147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '[' 114805bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Vals; 114921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1150e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *DeducedEltTy = 0; 1151e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ListRecTy *GivenListTy = 0; 115221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1153e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ItemType != 0) { 1154e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType); 1155e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ListType == 0) { 1156e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene std::stringstream s; 115721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson s << "Type mismatch for list, expected list type, got " 1158e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene << ItemType->getAsString(); 1159e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError(s.str()); 11606a44adade20bdea80c76b68833ec5eff5baaba98Jim Grosbach return 0; 1161e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1162e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene GivenListTy = ListType; 116321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1164e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1165f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 116621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Vals = ParseValueList(CurRec, 0, 116721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson GivenListTy ? GivenListTy->getElementType() : 0); 1168f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Vals.empty()) return 0; 1169f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1170f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 1171f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ']' at end of list value"); 1172f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1173f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1174f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ']' 1175e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1176e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *GivenEltTy = 0; 1177e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() == tgtok::less) { 1178e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Optional list element type 1179e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '<' 1180e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1181e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene GivenEltTy = ParseType(); 1182e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenEltTy == 0) { 1183e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Couldn't parse element type 1184e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1185e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1186e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1187e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() != tgtok::greater) { 1188e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected '>' at end of list element type"); 1189e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1190e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1191e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '>' 1192e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1193e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1194e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Check elements 1195e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *EltTy = 0; 119605bce0beee87512e52428d4b80f5a8e79a949576David Greene for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end(); 1197e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene i != ie; 1198e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++i) { 119905bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *TArg = dynamic_cast<TypedInit*>(*i); 1200e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (TArg == 0) { 1201e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Untyped list element"); 1202e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1203e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1204e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy != 0) { 1205e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = resolveTypes(EltTy, TArg->getType()); 1206e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy == 0) { 1207e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Incompatible types in list elements"); 1208e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1209e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 121021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1211e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = TArg->getType(); 1212e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1213e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1214e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1215e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenEltTy != 0) { 1216e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy != 0) { 1217e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Verify consistency 1218e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { 1219e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Incompatible types in list elements"); 1220e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1221e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1222e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1223e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = GivenEltTy; 1224e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1225e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1226e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy == 0) { 1227e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ItemType == 0) { 1228e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("No type for list"); 1229e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1230e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1231e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene DeducedEltTy = GivenListTy->getElementType(); 123221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1233e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Make sure the deduced type is compatible with the given type 1234e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenListTy) { 1235e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { 1236e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Element type mismatch for list"); 1237e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1238e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1239e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1240e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene DeducedEltTy = EltTy; 1241e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 124221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1243dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return ListInit::get(Vals, DeducedEltTy); 1244f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1245f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' 1246f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '(' 1247c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) { 12483dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner TokError("expected identifier in dag init"); 12493dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner return 0; 12503dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner } 125121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 125205bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Operator = ParseValue(CurRec); 1253578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner if (Operator == 0) return 0; 1254c7cafcd815519b06318629b424abe746437e1389David Greene 12557cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman // If the operator name is present, parse it. 12567cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman std::string OperatorName; 12577cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman if (Lex.getCode() == tgtok::colon) { 12587cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman if (Lex.Lex() != tgtok::VarName) { // eat the ':' 12597cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman TokError("expected variable name in dag operator"); 12607cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman return 0; 12617cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman } 12627cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman OperatorName = Lex.getCurStrVal(); 12637cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman Lex.Lex(); // eat the VarName. 12647cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman } 126521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 126605bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<std::pair<llvm::Init*, std::string> > DagArgs; 1267f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_paren) { 1268f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner DagArgs = ParseDagArgList(CurRec); 1269f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (DagArgs.empty()) return 0; 1270f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 127121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1272f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_paren) { 1273f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ')' in dag init"); 1274f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1275f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1276f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ')' 127721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1278dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return DagInit::get(Operator, OperatorName, DagArgs); 1279f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 128021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 12811434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 12821434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 12831434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 1284e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: // Value ::= !unop '(' Value ')' 1285f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XConcat: 128621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XSRA: 1287f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XSRL: 1288f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XSHL: 12896786d5e18387465d6106eaef0b7923526ef0bc10David Greene case tgtok::XEq: 1290c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')' 12919bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 1292beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 12934afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 1294d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return ParseOperation(CurRec); 1295f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1296f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 129721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1298f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return R; 1299f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1300f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1301f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseValue - Parse a tblgen value. This returns null on error. 1302f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1303f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Value ::= SimpleValue ValueSuffix* 1304f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '{' BitList '}' 1305f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '[' BitList ']' 1306f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '.' ID 1307f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 130805bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) { 130905bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Result = ParseSimpleValue(CurRec, ItemType); 1310f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) return 0; 131121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1312f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the suffixes now if present. 1313f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1314f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 1315f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: return Result; 1316f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_brace: { 13171e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc CurlyLoc = Lex.getLoc(); 1318f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 1319f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Ranges = ParseRangeList(); 1320f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return 0; 132121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1322f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Reverse the bitlist. 1323f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(Ranges.begin(), Ranges.end()); 1324f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result = Result->convertInitializerBitRange(Ranges); 1325f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) { 1326f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(CurlyLoc, "Invalid bit range for value"); 1327f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1328f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 132921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1330f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '}'. 1331f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1332f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit range list"); 1333f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1334f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1335f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1336f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1337f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1338f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_square: { 13391e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc SquareLoc = Lex.getLoc(); 1340f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '[' 1341f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Ranges = ParseRangeList(); 1342f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return 0; 134321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1344f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result = Result->convertInitListSlice(Ranges); 1345f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) { 1346f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(SquareLoc, "Invalid range for list slice"); 1347f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1348f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 134921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1350f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the ']'. 1351f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 1352f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ']' at end of list slice"); 1353f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1354f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1355f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1356f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1357f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1358f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::period: 1359f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::Id) { // eat the . 1360f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected field identifier after '.'"); 1361f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1362f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1363f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!Result->getFieldType(Lex.getCurStrVal())) { 1364f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + 13655d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner Result->getAsString() + "'"); 1366f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1367f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1368dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene Result = FieldInit::get(Result, Lex.getCurStrVal()); 1369f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat field name 1370f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1371f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1372f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1373f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1374f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1375f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList - Parse the argument list for a dag literal expression. 1376f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1377f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList ::= Value (':' VARNAME)? 1378f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)? 137905bce0beee87512e52428d4b80f5a8e79a949576David Greenestd::vector<std::pair<llvm::Init*, std::string> > 1380f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerTGParser::ParseDagArgList(Record *CurRec) { 138105bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<std::pair<llvm::Init*, std::string> > Result; 138221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1383f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 138405bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec); 138505bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >(); 138621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1387f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If the variable name is present, add it. 1388f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string VarName; 1389f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::colon) { 1390f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::VarName) { // eat the ':' 1391f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected variable name in dag literal"); 139205bce0beee87512e52428d4b80f5a8e79a949576David Greene return std::vector<std::pair<llvm::Init*, std::string> >(); 1393f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1394f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner VarName = Lex.getCurStrVal(); 1395f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the VarName. 1396f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 139721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1398f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.push_back(std::make_pair(Val, VarName)); 139921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1400f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) break; 140121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the ',' 1402f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 140321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1404f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 1405f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1406f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1407f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1408f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseValueList - Parse a comma separated list of values, returning them as a 1409f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// vector. Note that this always expects to be able to parse at least one 1410f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// value. It returns an empty list if this is not possible. 1411f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1412f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueList ::= Value (',' Value) 1413f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 141405bce0beee87512e52428d4b80f5a8e79a949576David Greenestd::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, 1415d568b3f55294917d1cc701da14a8a7daeb6563e6Eric Christopher RecTy *EltTy) { 141605bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Result; 1417e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *ItemType = EltTy; 141867acdf2977595a1d9755bf919d678f80ee65364fDavid Greene unsigned int ArgN = 0; 1419e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ArgsRec != 0 && EltTy == 0) { 1420e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs(); 1421e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1422d9746fe58ace4a8625983da1ecf68c0564883616David Greene if (!RV) { 1423d9746fe58ace4a8625983da1ecf68c0564883616David Greene errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN] 1424d9746fe58ace4a8625983da1ecf68c0564883616David Greene << ")\n"; 1425d9746fe58ace4a8625983da1ecf68c0564883616David Greene } 1426e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene assert(RV && "Template argument record not found??"); 1427e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ItemType = RV->getType(); 1428e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++ArgN; 1429e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1430e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.push_back(ParseValue(CurRec, ItemType)); 143105bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Result.back() == 0) return std::vector<Init*>(); 143221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1433f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 1434f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the comma 143521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1436e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ArgsRec != 0 && EltTy == 0) { 1437e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs(); 143867acdf2977595a1d9755bf919d678f80ee65364fDavid Greene if (ArgN >= TArgs.size()) { 143967acdf2977595a1d9755bf919d678f80ee65364fDavid Greene TokError("too many template arguments"); 144005bce0beee87512e52428d4b80f5a8e79a949576David Greene return std::vector<Init*>(); 144121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1442e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1443e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene assert(RV && "Template argument record not found??"); 1444e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ItemType = RV->getType(); 1445e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++ArgN; 1446e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1447e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.push_back(ParseValue(CurRec, ItemType)); 144805bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Result.back() == 0) return std::vector<Init*>(); 1449f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 145021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1451f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 1452f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1453f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1454f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1455f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDeclaration - Read a declaration, returning the name of field ID, or an 1456f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// empty string on error. This can happen in a number of different context's, 1457f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// including within a def or in the template args for a def (which which case 1458f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// CurRec will be non-null) and within the template args for a multiclass (in 1459f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// which case CurRec will be null, but CurMultiClass will be set). This can 1460f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// also happen within a def that is within a multiclass, which will set both 1461f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// CurRec and CurMultiClass. 1462f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1463f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Declaration ::= FIELD? Type ID ('=' Value)? 1464f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 146521870411d9c1807526b613d04c2ebae5a43c263bBob Wilsonstd::string TGParser::ParseDeclaration(Record *CurRec, 1466f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool ParsingTemplateArgs) { 1467f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the field prefix if present. 1468f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool HasField = Lex.getCode() == tgtok::Field; 1469f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (HasField) Lex.Lex(); 147021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1471f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecTy *Type = ParseType(); 1472f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Type == 0) return ""; 147321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1474f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 1475f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Expected identifier in declaration"); 1476f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ""; 1477f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 147821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 14791e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc IdLoc = Lex.getLoc(); 1480f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string DeclName = Lex.getCurStrVal(); 1481f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 148221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1483f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParsingTemplateArgs) { 1484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 1485f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner DeclName = CurRec->getName() + ":" + DeclName; 1486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 1487f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(CurMultiClass); 1488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1489f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass) 1490f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner DeclName = CurMultiClass->Rec.getName() + "::" + DeclName; 1491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 149221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1493f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add the value. 1494f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) 1495f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ""; 149621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1497f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If a value is present, parse it. 1498f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::equal) { 1499f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 15001e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc ValLoc = Lex.getLoc(); 150105bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec, Type); 1502f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0 || 1503f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) 1504f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ""; 1505f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 150621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1507f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return DeclName; 1508f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1509f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1510f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseTemplateArgList - Read a template argument list, which is a non-empty 1511f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// sequence of template-declarations in <>'s. If CurRec is non-null, these are 1512f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// template args for a def, which may or may not be in a multiclass. If null, 1513f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// these are the template args for a multiclass. 1514f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1515f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' 151621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson/// 1517f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseTemplateArgList(Record *CurRec) { 1518f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); 1519f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '<' 152021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1521f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; 152221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1523f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the first declaration. 1524f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1525f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (TemplArg.empty()) 1526f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 152721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1528f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TheRecToAddTo->addTemplateArg(TemplArg); 152921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1530f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 1531f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ',' 153221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1533f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the following declarations. 1534f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1535f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (TemplArg.empty()) 1536f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1537f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TheRecToAddTo->addTemplateArg(TemplArg); 1538f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 153921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1540f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) 1541f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected '>' at end of template argument list"); 1542f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '>'. 1543f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1544f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1545f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1546f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1547f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseBodyItem - Parse a single item at within the body of a def or class. 1548f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1549f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyItem ::= Declaration ';' 1550f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyItem ::= LET ID OptionalBitList '=' Value ';' 1551f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseBodyItem(Record *CurRec) { 1552f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Let) { 155321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseDeclaration(CurRec, false).empty()) 1554f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 155521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1556f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::semi) 1557f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ';' after declaration"); 1558f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1559f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1560f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1561f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1562f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // LET ID OptionalRangeList '=' Value ';' 1563f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::Id) 1564f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected field identifier after let"); 156521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 15661e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc IdLoc = Lex.getLoc(); 1567f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string FieldName = Lex.getCurStrVal(); 1568f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the field name. 156921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1570f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> BitList; 157121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseOptionalBitList(BitList)) 1572f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1573f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(BitList.begin(), BitList.end()); 157421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1575f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::equal) 1576f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected '=' in let expression"); 1577f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '='. 157821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1579e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecordVal *Field = CurRec->getValue(FieldName); 1580e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Field == 0) 1581e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return TokError("Value '" + FieldName + "' unknown!"); 1582e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1583e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *Type = Field->getType(); 158421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 158505bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec, Type); 1586f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0) return true; 158721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1588f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::semi) 1589f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ';' after let expression"); 1590f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 159121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return SetValue(CurRec, IdLoc, FieldName, BitList, Val); 1593f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1594f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1595f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseBody - Read the body of a class or def. Return true on error, false on 1596f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// success. 1597f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1598f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Body ::= ';' 1599f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Body ::= '{' BodyList '}' 1600f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyList BodyItem* 1601f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1602f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseBody(Record *CurRec) { 1603f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is a null definition, just eat the semi and return. 1604f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::semi) { 1605f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1606f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1607f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 160821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1609f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) 1610f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Expected ';' or '{' to start body"); 1611f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '{'. 1612f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 161321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1614f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() != tgtok::r_brace) 1615f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseBodyItem(CurRec)) 1616f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1617f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1618f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '}'. 1619f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1620f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1621f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1622f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1623f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectBody - Parse the body of a def or class. This consists of an 1624f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// optional ClassList followed by a Body. CurRec is the current def or class 1625f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// that is being parsed. 1626f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1627f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectBody ::= BaseClassList Body 1628f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassList ::= /*empty*/ 1629f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassList ::= ':' BaseClassListNE 1630f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassListNE ::= SubClassRef (',' SubClassRef)* 1631f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1632f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseObjectBody(Record *CurRec) { 1633f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there is a baseclass list, read it. 1634f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::colon) { 1635f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 163621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1637f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read all of the subclasses. 1638f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference SubClass = ParseSubClassReference(CurRec, false); 1639f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1640f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Check for error. 1641f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SubClass.Rec == 0) return true; 164221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1643f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add it. 1644f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddSubClass(CurRec, SubClass)) 1645f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 164621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1647f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) break; 1648f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat ','. 1649f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClass = ParseSubClassReference(CurRec, false); 1650f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1651f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1652f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1653f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Process any variables on the let stack. 1654f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 1655f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 1656f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 1657f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack[i][j].Bits, LetStack[i][j].Value)) 1658f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 165921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1660f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseBody(CurRec); 1661f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1662f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1663f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDef - Parse and return a top level or multiclass def, return the record 1664f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// corresponding to it. This returns null on error. 1665f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1666f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// DefInst ::= DEF ObjectName ObjectBody 1667f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1668ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseDef(MultiClass *CurMultiClass) { 16691e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc DefLoc = Lex.getLoc(); 1670f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Def && "Unknown tok"); 167121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // Eat the 'def' token. 1672f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1673f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse ObjectName and make a record for it. 16749c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner Record *CurRec = new Record(ParseObjectName(), DefLoc, Records); 167521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1676f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!CurMultiClass) { 1677f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Top-level def definition. 167821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1679f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Ensure redefinition doesn't happen. 1680f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Records.getDef(CurRec->getName())) { 1681f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(DefLoc, "def '" + CurRec->getName() + "' already defined"); 1682ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1683f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1684f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addDef(CurRec); 1685f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 1686f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Otherwise, a def inside a multiclass, add it to the multiclass. 1687f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) 1688f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { 1689f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(DefLoc, "def '" + CurRec->getName() + 1690f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "' already defined in this multiclass!"); 1691ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1692f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1693f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurMultiClass->DefPrototypes.push_back(CurRec); 1694f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 169521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1696f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseObjectBody(CurRec)) 1697ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 169821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1699f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. 17000d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // See Record::setName(). This resolve step will see any new name 17010d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // for the def that might have been created when resolving 17020d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // inheritance, values and arguments above. 1703f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->resolveReferences(); 170421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1705f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If ObjectBody has template arguments, it's an error. 1706f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); 1707ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1708ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (CurMultiClass) { 1709ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes // Copy the template arguments for the multiclass into the def. 1710ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes const std::vector<std::string> &TArgs = 1711ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes CurMultiClass->Rec.getTemplateArgs(); 1712ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1713ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1714ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); 1715ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes assert(RV && "Template arg doesn't exist?"); 1716ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes CurRec->addValue(*RV); 1717ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 1718ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 1719ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1720ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return false; 1721f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1722f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1723f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseClass - Parse a tblgen class definition. 1724f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1725f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody 1726f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1727f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseClass() { 1728f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); 1729f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 173021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1731f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) 1732f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected class name after 'class' keyword"); 173321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1734f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *CurRec = Records.getClass(Lex.getCurStrVal()); 1735f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 1736f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If the body was previously defined, this is an error. 1737f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!CurRec->getValues().empty() || 1738f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner !CurRec->getSuperClasses().empty() || 1739f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner !CurRec->getTemplateArgs().empty()) 1740f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Class '" + CurRec->getName() + "' already defined"); 1741f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 1742f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is the first reference to this class, create and add it. 17439c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records); 1744f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addClass(CurRec); 1745f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1746f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the name. 174721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1748f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there are template args, parse them. 1749f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::less) 1750f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseTemplateArgList(CurRec)) 1751f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1752f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1753f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Finally, parse the object body. 1754f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseObjectBody(CurRec); 1755f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1756f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1757f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseLetList - Parse a non-empty list of assignment expressions into a list 1758f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// of LetRecords. 1759f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1760f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// LetList ::= LetItem (',' LetItem)* 1761f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// LetItem ::= ID OptionalRangeList '=' Value 1762f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1763f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::vector<LetRecord> TGParser::ParseLetList() { 1764f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<LetRecord> Result; 176521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1766f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1767f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 1768f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected identifier in let definition"); 1769f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 1770f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1771f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 17721e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc NameLoc = Lex.getLoc(); 177321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // Eat the identifier. 1774f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1775f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Check for an optional RangeList. 1776f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Bits; 177721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseOptionalRangeList(Bits)) 1778f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 1779f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(Bits.begin(), Bits.end()); 178021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1781f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::equal) { 1782f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '=' in let expression"); 1783f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 1784f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1785f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '='. 178621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 178705bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(0); 1788f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0) return std::vector<LetRecord>(); 178921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1790f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Now that we have everything, add the record. 1791f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); 179221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1793f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) 1794f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 179521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the comma. 1796f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1797f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1798f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1799f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of 1800ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes/// different related productions. This works inside multiclasses too. 1801f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1802f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LET LetList IN '{' ObjectList '}' 1803f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LET LetList IN Object 1804f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1805ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { 1806f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Let && "Unexpected token"); 1807f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 180821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1809f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add this entry to the let stack. 1810f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<LetRecord> LetInfo = ParseLetList(); 1811f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (LetInfo.empty()) return true; 1812f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack.push_back(LetInfo); 1813f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1814f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::In) 1815f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected 'in' at end of top-level 'let'"); 1816f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 181721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1818f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is a scalar let, just handle it now 1819f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) { 1820f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // LET LetList IN Object 1821ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(CurMultiClass)) 1822f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1823f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { // Object ::= LETCommand '{' ObjectList '}' 18241e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc BraceLoc = Lex.getLoc(); 1825f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Otherwise, this is a group let. 1826f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{'. 182721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1828f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the object list. 1829ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObjectList(CurMultiClass)) 1830f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 183121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1832f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1833f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of top level let command"); 1834f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(BraceLoc, "to match this '{'"); 1835f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1836f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1837f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 183821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1839f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Outside this let scope, this let block is not active. 1840f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack.pop_back(); 1841f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1842f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1843f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1844f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseMultiClass - Parse a multiclass definition. 1845f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 184632558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// MultiClassInst ::= MULTICLASS ID TemplateArgList? 184732558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ':' BaseMultiClassList '{' MultiClassDef+ '}' 1848f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1849f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseMultiClass() { 1850f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); 1851f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the multiclass token. 1852f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1853f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) 1854f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected identifier after multiclass for name"); 1855f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 185621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1857f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (MultiClasses.count(Name)) 1858f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("multiclass '" + Name + "' already defined"); 185921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 186067db883487fca3472fdde51e931657e22d4d0495Chris Lattner CurMultiClass = MultiClasses[Name] = new MultiClass(Name, 186167db883487fca3472fdde51e931657e22d4d0495Chris Lattner Lex.getLoc(), Records); 1862f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the identifier. 186321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1864f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there are template args, parse them. 1865f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::less) 1866f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseTemplateArgList(0)) 1867f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1868f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1869d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene bool inherits = false; 1870d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 1871de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // If there are submulticlasses, parse them. 1872de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() == tgtok::colon) { 1873d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene inherits = true; 1874d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 1875de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 187632558650ae09247b31aa89cdee64e8c849771024Bob Wilson 1877de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Read all of the submulticlasses. 187832558650ae09247b31aa89cdee64e8c849771024Bob Wilson SubMultiClassReference SubMultiClass = 187932558650ae09247b31aa89cdee64e8c849771024Bob Wilson ParseSubMultiClassReference(CurMultiClass); 1880de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene while (1) { 1881de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Check for error. 1882de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (SubMultiClass.MC == 0) return true; 188332558650ae09247b31aa89cdee64e8c849771024Bob Wilson 1884de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add it. 1885de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddSubMultiClass(CurMultiClass, SubMultiClass)) 1886de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 188732558650ae09247b31aa89cdee64e8c849771024Bob Wilson 1888de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::comma) break; 1889de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); // eat ','. 1890de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass = ParseSubMultiClassReference(CurMultiClass); 1891de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 1892de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 1893de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 1894d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (Lex.getCode() != tgtok::l_brace) { 1895d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (!inherits) 1896d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return TokError("expected '{' in multiclass definition"); 189721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson else if (Lex.getCode() != tgtok::semi) 189821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson return TokError("expected ';' in multiclass definition"); 1899d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene else 190021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the ';'. 190121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1902d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (Lex.Lex() == tgtok::r_brace) // eat the '{'. 1903d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return TokError("multiclass must contain at least one def"); 190421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1905270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes while (Lex.getCode() != tgtok::r_brace) { 1906ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes switch (Lex.getCode()) { 1907ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes default: 1908a1b1b79be15c4b79a4282f148085ebad1cf877caDavid Greene return TokError("expected 'let', 'def' or 'defm' in multiclass body"); 1909ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Let: 1910ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Def: 1911ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Defm: 1912ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(CurMultiClass)) 1913ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1914ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes break; 1915ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 1916270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes } 1917d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene Lex.Lex(); // eat the '}'. 1918d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene } 191921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1920f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurMultiClass = 0; 1921f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1922f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1923f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1924e499a2df4492dab21a50d74f3f687b989f910a2fDavid GreeneRecord *TGParser:: 1925e499a2df4492dab21a50d74f3f687b989f910a2fDavid GreeneInstantiateMulticlassDef(MultiClass &MC, 1926e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *DefProto, 1927e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene const std::string &DefmPrefix, 1928e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc) { 1929e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Add in the defm name. If the defm prefix is empty, give each 1930e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // instantiated def a unique name. Otherwise, if "#NAME#" exists in the 1931e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // name, substitute the prefix for #NAME#. Otherwise, use the defm name 1932e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // as a prefix. 1933e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene std::string DefName = DefProto->getName(); 1934e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (DefmPrefix.empty()) { 1935e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene DefName = GetNewAnonymousName(); 1936e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } else { 1937e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene std::string::size_type idx = DefName.find("#NAME#"); 1938e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (idx != std::string::npos) { 1939e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene DefName.replace(idx, 6, DefmPrefix); 1940e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } else { 1941e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Add the suffix to the defm name to get the new name. 1942e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene DefName = DefmPrefix + DefName; 1943e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 1944e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 1945e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1946e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec = new Record(DefName, DefmPrefixLoc, Records); 1947e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1948e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SubClassReference Ref; 1949e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Ref.RefLoc = DefmPrefixLoc; 1950e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Ref.Rec = DefProto; 1951e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene AddSubClass(CurRec, Ref); 1952e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1953e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return CurRec; 1954e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 1955e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1956e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greenebool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, 1957e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec, 1958e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc, 1959e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc SubClassLoc, 1960e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene const std::vector<std::string> &TArgs, 1961e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene std::vector<Init *> &TemplateVals, 1962e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene bool DeleteArgs) { 1963e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Loop over all of the template arguments, setting them to the specified 1964e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // value or leaving them as the default if necessary. 1965e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1966e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Check if a value is specified for this temp-arg. 1967e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (i < TemplateVals.size()) { 1968e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Set it now. 1969e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), 1970e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene TemplateVals[i])) 1971e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return true; 1972e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1973e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Resolve it next. 1974e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 1975e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1976e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (DeleteArgs) 1977e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Now remove it. 1978e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->removeValue(TArgs[i]); 1979e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1980e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 1981e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "value not specified for template argument #"+ 1982e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" + 1983e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene MC.Rec.getName() + "'"); 1984e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 1985e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 1986e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return false; 1987e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 1988e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 1989e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greenebool TGParser::ResolveMulticlassDef(MultiClass &MC, 1990e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec, 1991e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *DefProto, 1992e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc) { 1993e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // If the mdef is inside a 'let' expression, add to each def. 1994e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 1995e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 1996e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 1997e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene LetStack[i][j].Bits, LetStack[i][j].Value)) 1998e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(DefmPrefixLoc, "when instantiating this defm"); 1999e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2000e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Ensure redefinition doesn't happen. 2001e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (Records.getDef(CurRec->getName())) 2002e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(DefmPrefixLoc, "def '" + CurRec->getName() + 2003e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene "' already defined, instantiating defm with subdef '" + 2004e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene DefProto->getName() + "'"); 2005e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2006e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Don't create a top level definition for defm inside multiclasses, 2007e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // instead, only update the prototypes and bind the template args 2008e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // with the new created definition. 2009e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (CurMultiClass) { 2010e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); 2011e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene i != e; ++i) 2012e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) 2013e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(DefmPrefixLoc, "defm '" + CurRec->getName() + 2014e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene "' already defined in this multiclass!"); 2015e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurMultiClass->DefPrototypes.push_back(CurRec); 2016e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2017e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Copy the template arguments for the multiclass into the new def. 2018e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene const std::vector<std::string> &TA = 2019e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurMultiClass->Rec.getTemplateArgs(); 2020e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2021e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = TA.size(); i != e; ++i) { 2022e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]); 2023e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene assert(RV && "Template arg doesn't exist?"); 2024e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->addValue(*RV); 2025e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2026e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } else { 2027e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Records.addDef(CurRec); 2028e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2029e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2030e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return false; 2031e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2032e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2033f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDefm - Parse the instantiation of a multiclass. 2034f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2035f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' 2036f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2037270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopesbool TGParser::ParseDefm(MultiClass *CurMultiClass) { 2038f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); 203921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2040df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner std::string DefmPrefix; 2041df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner if (Lex.Lex() == tgtok::Id) { // eat the defm. 2042df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner DefmPrefix = Lex.getCurStrVal(); 2043df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner Lex.Lex(); // Eat the defm prefix. 2044df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner } 2045c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 20461e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc DefmPrefixLoc = Lex.getLoc(); 2047df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner if (Lex.getCode() != tgtok::colon) 2048f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ':' after defm identifier"); 204921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 20506e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Keep track of the new generated record definitions. 20516e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes std::vector<Record*> NewRecDefs; 20526e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 20536e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // This record also inherits from a regular class (non-multiclass)? 20546e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes bool InheritFromClass = false; 20556e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 2056f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // eat the colon. 2057f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 2058f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 20591e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc SubClassLoc = Lex.getLoc(); 2060f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference Ref = ParseSubClassReference(0, true); 20615654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 20625654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene while (1) { 20635654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Ref.Rec == 0) return true; 20645654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 20655654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // To instantiate a multiclass, we need to first get the multiclass, then 20665654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // instantiate each def contained in the multiclass with the SubClassRef 20675654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // template parameters. 20685654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene MultiClass *MC = MultiClasses[Ref.Rec->getName()]; 20695654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene assert(MC && "Didn't lookup multiclass correctly?"); 207005bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> &TemplateVals = Ref.TemplateArgs; 20715654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 20725654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // Verify that the correct number of template arguments were specified. 20735654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs(); 20745654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (TArgs.size() < TemplateVals.size()) 20755654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene return Error(SubClassLoc, 20765654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene "more template args specified than multiclass expects"); 20775654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 20785654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // Loop over all the def's in the multiclass, instantiating each one. 20795654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { 20805654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Record *DefProto = MC->DefPrototypes[i]; 20815654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 2082e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc); 2083270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes 2084e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc, 2085e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene TArgs, TemplateVals, true/*Delete args*/)) 2086e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "could not instantiate def"); 2087270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes 2088e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc)) 2089e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "could not instantiate def"); 20906e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 20916e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes NewRecDefs.push_back(CurRec); 2092f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 20935654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 2094e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 20955654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Lex.getCode() != tgtok::comma) break; 20965654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Lex.Lex(); // eat ','. 20975654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 20985654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene SubClassLoc = Lex.getLoc(); 20996e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21006e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // A defm can inherit from regular classes (non-multiclass) as 21016e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // long as they come in the end of the inheritance list. 21026e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0); 21036e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21046e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (InheritFromClass) 21056e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes break; 21066e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21075654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Ref = ParseSubClassReference(0, true); 2108f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 21095654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 21106e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (InheritFromClass) { 21116e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Process all the classes to inherit as if they were part of a 21126e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // regular 'def' and inherit all record values. 21136e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes SubClassReference SubClass = ParseSubClassReference(0, false); 21146e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes while (1) { 21156e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Check for error. 21166e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (SubClass.Rec == 0) return true; 21176e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21186e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Get the expanded definition prototypes and teach them about 21196e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // the record values the current class to inherit has 21206e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) { 21216e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes Record *CurRec = NewRecDefs[i]; 21226e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21236e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Add it. 21246e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (AddSubClass(CurRec, SubClass)) 21256e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes return true; 21266e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21276e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Process any variables on the let stack. 21286e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 21296e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 21306e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 21316e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes LetStack[i][j].Bits, LetStack[i][j].Value)) 21326e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes return true; 21336e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 21346e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 21356e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (Lex.getCode() != tgtok::comma) break; 21366e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes Lex.Lex(); // eat ','. 21376e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes SubClass = ParseSubClassReference(0, false); 21386e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 21396e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 21406e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 2141e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes if (!CurMultiClass) 2142e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) 21430d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // See Record::setName(). This resolve step will see any new 21440d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // name for the def that might have been created when resolving 21450d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // inheritance, values and arguments above. 2146e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes NewRecDefs[i]->resolveReferences(); 2147e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes 21485654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Lex.getCode() != tgtok::semi) 21495654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene return TokError("expected ';' at end of defm"); 21505654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Lex.Lex(); 215121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2153f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObject 2156f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= ClassInst 2157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= DefInst 2158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= MultiClassInst 2159f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= DefMInst 2160f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LETCommand '{' ObjectList '}' 2161f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LETCommand Object 2162ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseObject(MultiClass *MC) { 2163f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 2164d6d9dd9f5e950383b7040a864bd8e004f95bcc91Chris Lattner default: 2165d6d9dd9f5e950383b7040a864bd8e004f95bcc91Chris Lattner return TokError("Expected class, def, defm, multiclass or let definition"); 2166ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Let: return ParseTopLevelLet(MC); 2167ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Def: return ParseDef(MC); 2168ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Defm: return ParseDefm(MC); 2169f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Class: return ParseClass(); 2170f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::MultiClass: return ParseMultiClass(); 2171f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2172f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2173f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2174f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectList 2175f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectList :== Object* 2176ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseObjectList(MultiClass *MC) { 2177f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (isObjectStart(Lex.getCode())) { 2178ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(MC)) 2179f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2180f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2181f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2182f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2183f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2184f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseFile() { 2185f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Prime the lexer. 2186f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseObjectList()) return true; 218721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2188f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If we have unread input at the end of the file, report it. 2189f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::Eof) 2190f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 219121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2192f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Unexpected input at top level"); 2193f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2194f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2195