2007-11-23 04:49:04 +08:00
|
|
|
//===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:37:13 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-11-23 04:49:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class represents the Parser for tablegen files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef TGPARSER_H
|
|
|
|
#define TGPARSER_H
|
|
|
|
|
|
|
|
#include "TGLexer.h"
|
2010-09-28 01:42:11 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2009-06-21 11:36:54 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
2007-11-23 04:49:04 +08:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class Record;
|
|
|
|
class RecordVal;
|
2010-12-13 08:23:57 +08:00
|
|
|
class RecordKeeper;
|
2011-07-19 01:02:57 +08:00
|
|
|
class RecTy;
|
2011-07-14 06:25:51 +08:00
|
|
|
class Init;
|
2007-11-23 04:49:04 +08:00
|
|
|
struct MultiClass;
|
2009-03-20 07:26:52 +08:00
|
|
|
struct SubClassReference;
|
2009-04-23 00:42:54 +08:00
|
|
|
struct SubMultiClassReference;
|
2012-10-04 08:54:27 +08:00
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
struct LetRecord {
|
|
|
|
std::string Name;
|
|
|
|
std::vector<unsigned> Bits;
|
2011-07-30 06:43:06 +08:00
|
|
|
Init *Value;
|
2009-06-21 11:39:35 +08:00
|
|
|
SMLoc Loc;
|
2011-07-30 06:43:06 +08:00
|
|
|
LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
|
2011-07-12 07:06:52 +08:00
|
|
|
SMLoc L)
|
2007-11-23 04:49:04 +08:00
|
|
|
: Name(N), Bits(B), Value(V), Loc(L) {
|
|
|
|
}
|
|
|
|
};
|
2012-10-04 08:54:27 +08:00
|
|
|
|
2012-02-23 00:09:41 +08:00
|
|
|
/// ForeachLoop - Record the iteration state associated with a for loop.
|
|
|
|
/// This is used to instantiate items in the loop body.
|
|
|
|
struct ForeachLoop {
|
2012-05-25 06:17:33 +08:00
|
|
|
VarInit *IterVar;
|
|
|
|
ListInit *ListValue;
|
2012-02-23 00:09:41 +08:00
|
|
|
|
2012-05-25 06:17:33 +08:00
|
|
|
ForeachLoop(VarInit *IVar, ListInit *LValue)
|
|
|
|
: IterVar(IVar), ListValue(LValue) {}
|
2012-02-23 00:09:41 +08:00
|
|
|
};
|
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
class TGParser {
|
|
|
|
TGLexer Lex;
|
|
|
|
std::vector<std::vector<LetRecord> > LetStack;
|
|
|
|
std::map<std::string, MultiClass*> MultiClasses;
|
2012-10-04 08:54:27 +08:00
|
|
|
|
2012-02-23 00:09:41 +08:00
|
|
|
/// Loops - Keep track of any foreach loops we are within.
|
|
|
|
///
|
|
|
|
typedef std::vector<ForeachLoop> LoopVector;
|
|
|
|
LoopVector Loops;
|
|
|
|
|
2012-10-04 08:54:27 +08:00
|
|
|
/// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
|
2007-11-23 04:49:04 +08:00
|
|
|
/// current value.
|
|
|
|
MultiClass *CurMultiClass;
|
2010-12-13 08:23:57 +08:00
|
|
|
|
|
|
|
// Record tracker
|
2010-12-15 12:48:22 +08:00
|
|
|
RecordKeeper &Records;
|
2011-10-19 21:04:20 +08:00
|
|
|
|
|
|
|
// A "named boolean" indicating how to parse identifiers. Usually
|
|
|
|
// identifiers map to some existing object but in special cases
|
|
|
|
// (e.g. parsing def names) no such object exists yet because we are
|
|
|
|
// in the middle of creating in. For those situations, allow the
|
|
|
|
// parser to ignore missing object errors.
|
|
|
|
enum IDParseMode {
|
2012-02-23 00:09:41 +08:00
|
|
|
ParseValueMode, // We are parsing a value we expect to look up.
|
|
|
|
ParseNameMode, // We are parsing a name of an object that does not yet
|
|
|
|
// exist.
|
|
|
|
ParseForeachMode // We are parsing a foreach init.
|
2011-10-19 21:04:20 +08:00
|
|
|
};
|
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
public:
|
2012-10-04 08:54:27 +08:00
|
|
|
TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
|
2010-12-13 08:23:57 +08:00
|
|
|
Lex(SrcMgr), CurMultiClass(0), Records(records) {}
|
2012-10-04 08:54:27 +08:00
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
/// ParseFile - Main entrypoint for parsing a tblgen file. These parser
|
|
|
|
/// routines return true on error, or false on success.
|
|
|
|
bool ParseFile();
|
2012-10-04 08:54:27 +08:00
|
|
|
|
2010-09-28 01:42:11 +08:00
|
|
|
bool Error(SMLoc L, const Twine &Msg) const {
|
2011-06-22 06:55:50 +08:00
|
|
|
PrintError(L, Msg);
|
2007-11-23 04:49:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-09-28 01:42:11 +08:00
|
|
|
bool TokError(const Twine &Msg) const {
|
2007-11-23 04:49:04 +08:00
|
|
|
return Error(Lex.getLoc(), Msg);
|
|
|
|
}
|
2011-06-01 21:10:15 +08:00
|
|
|
const std::vector<std::string> &getDependencies() const {
|
|
|
|
return Lex.getDependencies();
|
|
|
|
}
|
2012-02-23 00:09:41 +08:00
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
private: // Semantic analysis methods.
|
2009-06-21 11:39:35 +08:00
|
|
|
bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
|
2012-10-04 08:54:27 +08:00
|
|
|
bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
|
2011-07-30 06:43:06 +08:00
|
|
|
const std::vector<unsigned> &BitList, Init *V);
|
2012-10-04 08:54:27 +08:00
|
|
|
bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
|
2011-10-19 21:02:39 +08:00
|
|
|
const std::vector<unsigned> &BitList, Init *V) {
|
|
|
|
return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
|
|
|
|
}
|
2009-02-15 00:06:42 +08:00
|
|
|
bool AddSubClass(Record *Rec, SubClassReference &SubClass);
|
2009-05-01 02:26:19 +08:00
|
|
|
bool AddSubMultiClass(MultiClass *CurMC,
|
|
|
|
SubMultiClassReference &SubMultiClass);
|
2007-11-23 04:49:04 +08:00
|
|
|
|
2012-02-23 00:09:41 +08:00
|
|
|
// IterRecord: Map an iterator name to a value.
|
|
|
|
struct IterRecord {
|
2012-05-25 06:17:33 +08:00
|
|
|
VarInit *IterVar;
|
2012-02-23 00:09:41 +08:00
|
|
|
Init *IterValue;
|
2012-05-25 06:17:33 +08:00
|
|
|
IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
|
2012-02-23 00:09:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// IterSet: The set of all iterator values at some point in the
|
|
|
|
// iteration space.
|
|
|
|
typedef std::vector<IterRecord> IterSet;
|
|
|
|
|
2012-05-25 06:17:33 +08:00
|
|
|
bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
|
|
|
|
bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
|
2012-02-23 00:09:41 +08:00
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
private: // Parser methods.
|
2010-06-10 10:42:59 +08:00
|
|
|
bool ParseObjectList(MultiClass *MC = 0);
|
|
|
|
bool ParseObject(MultiClass *MC);
|
2007-11-23 04:49:04 +08:00
|
|
|
bool ParseClass();
|
|
|
|
bool ParseMultiClass();
|
2011-10-06 06:42:07 +08:00
|
|
|
Record *InstantiateMulticlassDef(MultiClass &MC,
|
|
|
|
Record *DefProto,
|
2011-10-19 21:04:31 +08:00
|
|
|
Init *DefmPrefix,
|
2011-10-06 06:42:07 +08:00
|
|
|
SMLoc DefmPrefixLoc);
|
|
|
|
bool ResolveMulticlassDefArgs(MultiClass &MC,
|
|
|
|
Record *DefProto,
|
|
|
|
SMLoc DefmPrefixLoc,
|
|
|
|
SMLoc SubClassLoc,
|
2011-10-19 21:02:42 +08:00
|
|
|
const std::vector<Init *> &TArgs,
|
2011-10-06 06:42:07 +08:00
|
|
|
std::vector<Init *> &TemplateVals,
|
|
|
|
bool DeleteArgs);
|
|
|
|
bool ResolveMulticlassDef(MultiClass &MC,
|
|
|
|
Record *CurRec,
|
|
|
|
Record *DefProto,
|
|
|
|
SMLoc DefmPrefixLoc);
|
2010-06-10 10:42:59 +08:00
|
|
|
bool ParseDefm(MultiClass *CurMultiClass);
|
|
|
|
bool ParseDef(MultiClass *CurMultiClass);
|
2012-02-23 00:09:41 +08:00
|
|
|
bool ParseForeach(MultiClass *CurMultiClass);
|
2010-06-10 10:42:59 +08:00
|
|
|
bool ParseTopLevelLet(MultiClass *CurMultiClass);
|
2007-11-23 04:49:04 +08:00
|
|
|
std::vector<LetRecord> ParseLetList();
|
|
|
|
|
|
|
|
bool ParseObjectBody(Record *CurRec);
|
|
|
|
bool ParseBody(Record *CurRec);
|
|
|
|
bool ParseBodyItem(Record *CurRec);
|
|
|
|
|
|
|
|
bool ParseTemplateArgList(Record *CurRec);
|
2011-10-19 21:02:42 +08:00
|
|
|
Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
|
2012-05-25 06:17:33 +08:00
|
|
|
VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
|
2007-11-23 04:49:04 +08:00
|
|
|
|
|
|
|
SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
|
2009-05-01 02:26:19 +08:00
|
|
|
SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
|
2007-11-23 04:49:04 +08:00
|
|
|
|
2011-10-19 21:04:20 +08:00
|
|
|
Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
|
|
|
|
Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
|
|
|
|
IDParseMode Mode = ParseValueMode);
|
|
|
|
Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
|
|
|
|
IDParseMode Mode = ParseValueMode);
|
|
|
|
Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
|
|
|
|
IDParseMode Mode = ParseValueMode);
|
2012-10-04 08:54:27 +08:00
|
|
|
std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0,
|
|
|
|
RecTy *EltTy = 0);
|
2011-07-30 06:43:06 +08:00
|
|
|
std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
|
2007-11-23 04:49:04 +08:00
|
|
|
bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
|
|
|
|
bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
|
|
|
|
std::vector<unsigned> ParseRangeList();
|
|
|
|
bool ParseRangePiece(std::vector<unsigned> &Ranges);
|
|
|
|
RecTy *ParseType();
|
2011-07-30 06:43:06 +08:00
|
|
|
Init *ParseOperation(Record *CurRec);
|
2009-05-15 04:54:48 +08:00
|
|
|
RecTy *ParseOperatorType();
|
2011-10-19 21:04:29 +08:00
|
|
|
Init *ParseObjectName(MultiClass *CurMultiClass);
|
2007-11-23 04:49:04 +08:00
|
|
|
Record *ParseClassID();
|
2009-04-23 00:42:54 +08:00
|
|
|
MultiClass *ParseMultiClassID();
|
2013-01-09 12:49:14 +08:00
|
|
|
bool ApplyLetStack(Record *CurRec);
|
2007-11-23 04:49:04 +08:00
|
|
|
};
|
2012-10-04 08:54:27 +08:00
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|