X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=utils%2FTableGen%2FRecord.h;h=90e246eb4779522575d9277f0d2f5043b59bdbd9;hb=5ec9efd61bc4214c787287409498e8b78f28c922;hp=b318157e0b0d4d31f8768c162149186fb5fb9ccf;hpb=ea3e5e56fdc56e5c2ddafb36eab26676e137dfa0;p=oota-llvm.git diff --git a/utils/TableGen/Record.h b/utils/TableGen/Record.h index b318157e0b0..90e246eb477 100644 --- a/utils/TableGen/Record.h +++ b/utils/TableGen/Record.h @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include namespace llvm { @@ -42,6 +42,7 @@ class IntInit; class StringInit; class CodeInit; class ListInit; +class BinOpInit; class DefInit; class DagInit; class TypedInit; @@ -61,7 +62,8 @@ class RecordVal; struct RecTy { virtual ~RecTy() {} - virtual void print(std::ostream &OS) const = 0; + virtual std::string getAsString() const = 0; + void print(std::ostream &OS) const { OS << getAsString(); } void dump() const; /// typeIsConvertibleTo - Return true if all values of 'this' type can be @@ -75,6 +77,7 @@ public: // These methods should only be called from subclasses of Init virtual Init *convertValue( IntInit *II) { return 0; } virtual Init *convertValue(StringInit *SI) { return 0; } virtual Init *convertValue( ListInit *LI) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( CodeInit *CI) { return 0; } virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } @@ -120,11 +123,12 @@ public: virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; } virtual Init *convertValue( DefInit *DI) { return 0; } virtual Init *convertValue( DagInit *DI) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( TypedInit *TI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - void print(std::ostream &OS) const { OS << "bit"; } + std::string getAsString() const { return "bit"; } bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -147,7 +151,7 @@ public: class BitsRecTy : public RecTy { unsigned Size; public: - BitsRecTy(unsigned Sz) : Size(Sz) {} + explicit BitsRecTy(unsigned Sz) : Size(Sz) {} unsigned getNumBits() const { return Size; } @@ -161,12 +165,12 @@ public: virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } virtual Init *convertValue( DagInit *DI) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( TypedInit *TI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - - void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; } + std::string getAsString() const; bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -199,12 +203,12 @@ public: virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } virtual Init *convertValue( DagInit *DI) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( TypedInit *TI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - - void print(std::ostream &OS) const { OS << "int"; } + std::string getAsString() const { return "int"; } bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -231,6 +235,7 @@ public: virtual Init *convertValue( IntInit *II) { return 0; } virtual Init *convertValue(StringInit *SI) { return (Init*)SI; } virtual Init *convertValue( ListInit *LI) { return 0; } + virtual Init *convertValue( BinOpInit *BO); virtual Init *convertValue( CodeInit *CI) { return 0; } virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } @@ -239,7 +244,7 @@ public: virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - void print(std::ostream &OS) const { OS << "string"; } + std::string getAsString() const { return "string"; } bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -263,7 +268,7 @@ public: class ListRecTy : public RecTy { RecTy *Ty; public: - ListRecTy(RecTy *T) : Ty(T) {} + explicit ListRecTy(RecTy *T) : Ty(T) {} RecTy *getElementType() const { return Ty; } @@ -277,11 +282,12 @@ public: virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } virtual Init *convertValue( DagInit *DI) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( TypedInit *TI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - void print(std::ostream &OS) const; + std::string getAsString() const; bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -313,12 +319,12 @@ public: virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } virtual Init *convertValue( DagInit *DI) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( TypedInit *TI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - - void print(std::ostream &OS) const { OS << "code"; } + std::string getAsString() const { return "code"; } bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -346,12 +352,13 @@ public: virtual Init *convertValue( CodeInit *CI) { return 0; } virtual Init *convertValue(VarBitInit *VB) { return 0; } virtual Init *convertValue( DefInit *DI) { return 0; } + virtual Init *convertValue( BinOpInit *BO); virtual Init *convertValue( DagInit *CI) { return (Init*)CI; } virtual Init *convertValue( TypedInit *TI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - void print(std::ostream &OS) const { OS << "dag"; } + std::string getAsString() const { return "dag"; } bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -374,7 +381,7 @@ public: class RecordRecTy : public RecTy { Record *Rec; public: - RecordRecTy(Record *R) : Rec(R) {} + explicit RecordRecTy(Record *R) : Rec(R) {} Record *getRecord() const { return Rec; } @@ -386,13 +393,14 @@ public: virtual Init *convertValue( ListInit *LI) { return 0; } virtual Init *convertValue( CodeInit *CI) { return 0; } virtual Init *convertValue(VarBitInit *VB) { return 0; } + virtual Init *convertValue( BinOpInit *UI) { return 0; } virtual Init *convertValue( DefInit *DI); virtual Init *convertValue( DagInit *DI) { return 0; } virtual Init *convertValue( TypedInit *VI); virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} - void print(std::ostream &OS) const; + std::string getAsString() const; bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); @@ -421,7 +429,10 @@ struct Init { virtual bool isComplete() const { return true; } /// print - Print out this value. - virtual void print(std::ostream &OS) const = 0; + void print(std::ostream &OS) const { OS << getAsString(); } + + /// getAsString - Convert this value to a string form. + virtual std::string getAsString() const = 0; /// dump - Debugging method that may be called through a debugger, just /// invokes print on cerr. @@ -465,11 +476,6 @@ struct Init { return 0; } - enum BinaryOp { SHL, SRA, SRL }; - virtual Init *getBinaryOp(BinaryOp Op, Init *RHS) { - return 0; - } - /// resolveReferences - This method is used by classes that refer to other /// variables which may not be defined at the time they expression is formed. /// If a value is set for the variable later, this method will be called on @@ -494,7 +500,7 @@ public: } virtual bool isComplete() const { return false; } - virtual void print(std::ostream &OS) const { OS << "?"; } + virtual std::string getAsString() const { return "?"; } }; @@ -503,7 +509,7 @@ public: class BitInit : public Init { bool Value; public: - BitInit(bool V) : Value(V) {} + explicit BitInit(bool V) : Value(V) {} bool getValue() const { return Value; } @@ -511,7 +517,7 @@ public: return Ty->convertValue(this); } - virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); } + virtual std::string getAsString() const { return Value ? "1" : "0"; } }; /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value. @@ -520,7 +526,7 @@ public: class BitsInit : public Init { std::vector Bits; public: - BitsInit(unsigned Size) : Bits(Size) {} + explicit BitsInit(unsigned Size) : Bits(Size) {} unsigned getNumBits() const { return Bits.size(); } @@ -544,7 +550,7 @@ public: if (!getBit(i)->isComplete()) return false; return true; } - virtual void print(std::ostream &OS) const; + virtual std::string getAsString() const; virtual Init *resolveReferences(Record &R, const RecordVal *RV); @@ -561,7 +567,7 @@ public: class IntInit : public Init { int Value; public: - IntInit(int V) : Value(V) {} + explicit IntInit(int V) : Value(V) {} int getValue() const { return Value; } @@ -570,9 +576,7 @@ public: } virtual Init *convertInitializerBitRange(const std::vector &Bits); - virtual Init *getBinaryOp(BinaryOp Op, Init *RHS); - - virtual void print(std::ostream &OS) const { OS << Value; } + virtual std::string getAsString() const; }; @@ -581,7 +585,7 @@ public: class StringInit : public Init { std::string Value; public: - StringInit(const std::string &V) : Value(V) {} + explicit StringInit(const std::string &V) : Value(V) {} const std::string &getValue() const { return Value; } @@ -589,7 +593,7 @@ public: return Ty->convertValue(this); } - virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; } + virtual std::string getAsString() const { return "\"" + Value + "\""; } }; /// CodeInit - "[{...}]" - Represent a code fragment. @@ -597,7 +601,7 @@ public: class CodeInit : public Init { std::string Value; public: - CodeInit(const std::string &V) : Value(V) {} + explicit CodeInit(const std::string &V) : Value(V) {} const std::string getValue() const { return Value; } @@ -605,7 +609,7 @@ public: return Ty->convertValue(this); } - virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; } + virtual std::string getAsString() const { return "[{" + Value + "}]"; } }; /// ListInit - [AL, AH, CL] - Represent a list of defs @@ -613,7 +617,7 @@ public: class ListInit : public Init { std::vector Values; public: - ListInit(std::vector &Vs) { + explicit ListInit(std::vector &Vs) { Values.swap(Vs); } @@ -623,6 +627,8 @@ public: return Values[i]; } + Record *getElementAsRecord(unsigned i) const; + Init *convertInitListSlice(const std::vector &Elements); virtual Init *convertInitializerTo(RecTy *Ty) { @@ -636,17 +642,58 @@ public: /// virtual Init *resolveReferences(Record &R, const RecordVal *RV); - virtual void print(std::ostream &OS) const; + virtual std::string getAsString() const; + + typedef std::vector::iterator iterator; + typedef std::vector::const_iterator const_iterator; + + inline iterator begin() { return Values.begin(); } + inline const_iterator begin() const { return Values.begin(); } + inline iterator end () { return Values.end(); } + inline const_iterator end () const { return Values.end(); } + + inline size_t size () const { return Values.size(); } + inline bool empty() const { return Values.empty(); } +}; + +/// BinOpInit - !op (X, Y) - Combine two inits. +/// +class BinOpInit : public Init { +public: + enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT }; +private: + BinaryOp Opc; + Init *LHS, *RHS; +public: + BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) { + } + + BinaryOp getOpcode() const { return Opc; } + Init *getLHS() const { return LHS; } + Init *getRHS() const { return RHS; } + + // Fold - If possible, fold this to a simpler init. Return this if not + // possible to fold. + Init *Fold(); + + virtual Init *convertInitializerTo(RecTy *Ty) { + return Ty->convertValue(this); + } + + virtual Init *resolveReferences(Record &R, const RecordVal *RV); + + virtual std::string getAsString() const; }; + /// TypedInit - This is the common super-class of types that have a specific, /// explicit, type. /// class TypedInit : public Init { RecTy *Ty; public: - TypedInit(RecTy *T) : Ty(T) {} + explicit TypedInit(RecTy *T) : Ty(T) {} RecTy *getType() const { return Ty; } @@ -672,7 +719,8 @@ public: class VarInit : public TypedInit { std::string VarName; public: - VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {} + explicit VarInit(const std::string &VN, RecTy *T) + : TypedInit(T), VarName(VN) {} virtual Init *convertInitializerTo(RecTy *Ty) { return Ty->convertValue(this); @@ -695,7 +743,7 @@ public: /// virtual Init *resolveReferences(Record &R, const RecordVal *RV); - virtual void print(std::ostream &OS) const { OS << VarName; } + virtual std::string getAsString() const { return VarName; } }; @@ -718,9 +766,7 @@ public: TypedInit *getVariable() const { return TI; } unsigned getBitNum() const { return Bit; } - virtual void print(std::ostream &OS) const { - TI->print(OS); OS << "{" << Bit << "}"; - } + virtual std::string getAsString() const; virtual Init *resolveReferences(Record &R, const RecordVal *RV); }; @@ -753,9 +799,7 @@ public: virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt); - virtual void print(std::ostream &OS) const { - TI->print(OS); OS << "[" << Element << "]"; - } + virtual std::string getAsString() const; virtual Init *resolveReferences(Record &R, const RecordVal *RV); }; @@ -764,7 +808,7 @@ public: class DefInit : public Init { Record *Def; public: - DefInit(Record *D) : Def(D) {} + explicit DefInit(Record *D) : Def(D) {} virtual Init *convertInitializerTo(RecTy *Ty) { return Ty->convertValue(this); @@ -777,7 +821,7 @@ public: virtual RecTy *getFieldType(const std::string &FieldName) const; virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; - virtual void print(std::ostream &OS) const; + virtual std::string getAsString() const; }; @@ -803,22 +847,22 @@ public: virtual Init *resolveReferences(Record &R, const RecordVal *RV); - virtual void print(std::ostream &OS) const { - Rec->print(OS); OS << "." << FieldName; + virtual std::string getAsString() const { + return Rec->getAsString() + "." + FieldName; } }; -/// DagInit - (def a, b) - Represent a DAG tree value. DAG inits are required -/// to have Records for their first value, after that, any legal Init is -/// possible. +/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required +/// to have at least one value then a (possibly empty) list of arguments. Each +/// argument can have a name associated with it. /// class DagInit : public Init { - Record *NodeTypeDef; + Init *Val; std::vector Args; std::vector ArgNames; public: - DagInit(Record *D, const std::vector > &args) - : NodeTypeDef(D) { + DagInit(Init *V, const std::vector > &args) + : Val(V) { Args.reserve(args.size()); ArgNames.reserve(args.size()); for (unsigned i = 0, e = args.size(); i != e; ++i) { @@ -826,12 +870,16 @@ public: ArgNames.push_back(args[i].second); } } - + DagInit(Init *V, const std::vector &args, + const std::vector &argNames) + : Val(V), Args(args), ArgNames(argNames) { + } + virtual Init *convertInitializerTo(RecTy *Ty) { return Ty->convertValue(this); } - Record *getNodeType() const { return NodeTypeDef; } + Init *getOperator() const { return Val; } unsigned getNumArgs() const { return Args.size(); } Init *getArg(unsigned Num) const { @@ -847,8 +895,32 @@ public: assert(Num < Args.size() && "Arg number out of range!"); Args[Num] = I; } + + virtual Init *resolveReferences(Record &R, const RecordVal *RV); + + virtual std::string getAsString() const; + + typedef std::vector::iterator arg_iterator; + typedef std::vector::const_iterator const_arg_iterator; + typedef std::vector::iterator name_iterator; + typedef std::vector::const_iterator const_name_iterator; + + inline arg_iterator arg_begin() { return Args.begin(); } + inline const_arg_iterator arg_begin() const { return Args.begin(); } + inline arg_iterator arg_end () { return Args.end(); } + inline const_arg_iterator arg_end () const { return Args.end(); } + + inline size_t arg_size () const { return Args.size(); } + inline bool arg_empty() const { return Args.empty(); } + + inline name_iterator name_begin() { return ArgNames.begin(); } + inline const_name_iterator name_begin() const { return ArgNames.begin(); } + inline name_iterator name_end () { return ArgNames.end(); } + inline const_name_iterator name_end () const { return ArgNames.end(); } + + inline size_t name_size () const { return ArgNames.size(); } + inline bool name_empty() const { return ArgNames.empty(); } - virtual void print(std::ostream &OS) const; }; //===----------------------------------------------------------------------===// @@ -888,16 +960,17 @@ inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) { } class Record { - const std::string Name; + std::string Name; std::vector TemplateArgs; std::vector Values; std::vector SuperClasses; public: - Record(const std::string &N) : Name(N) {} + explicit Record(const std::string &N) : Name(N) {} ~Record() {} const std::string &getName() const { return Name; } + void setName(const std::string &Name); // Also updates RecordKeeper. const std::vector &getTemplateArgs() const { return TemplateArgs; } @@ -999,6 +1072,18 @@ public: /// ListInit *getValueAsListInit(const std::string &FieldName) const; + /// getValueAsListOfDefs - This method looks up the specified field and + /// returns its value as a vector of records, throwing an exception if the + /// field does not exist or if the value is not the right type. + /// + std::vector getValueAsListOfDefs(const std::string &FieldName) const; + + /// getValueAsListOfInts - This method looks up the specified field and returns + /// its value as a vector of integers, throwing an exception if the field does + /// not exist or if the value is not the right type. + /// + std::vector getValueAsListOfInts(const std::string &FieldName) const; + /// getValueAsDef - This method looks up the specified field and returns its /// value as a Record, throwing an exception if the field does not exist or if /// the value is not the right type. @@ -1022,6 +1107,12 @@ public: /// the value is not the right type. /// DagInit *getValueAsDag(const std::string &FieldName) const; + + /// getValueAsCode - This method looks up the specified field and returns + /// its value as the string data in a CodeInit, throwing an exception if the + /// field does not exist or if the value is not a code object. + /// + std::string getValueAsCode(const std::string &FieldName) const; }; std::ostream &operator<<(std::ostream &OS, const Record &R); @@ -1058,6 +1149,19 @@ public: Defs.insert(std::make_pair(R->getName(), R)); } + /// removeClass - Remove, but do not delete, the specified record. + /// + void removeClass(const std::string &Name) { + assert(Classes.count(Name) && "Class does not exist!"); + Classes.erase(Name); + } + /// removeDef - Remove, but do not delete, the specified record. + /// + void removeDef(const std::string &Name) { + assert(Defs.count(Name) && "Def does not exist!"); + Defs.erase(Name); + } + //===--------------------------------------------------------------------===// // High-level helper methods, useful for tablegen backends... @@ -1071,6 +1175,24 @@ public: void dump() const; }; +/// LessRecord - Sorting predicate to sort record pointers by name. +/// +struct LessRecord { + bool operator()(const Record *Rec1, const Record *Rec2) const { + return Rec1->getName() < Rec2->getName(); + } +}; + +/// LessRecordFieldName - Sorting predicate to sort record pointers by their +/// name field. +/// +struct LessRecordFieldName { + bool operator()(const Record *Rec1, const Record *Rec2) const { + return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name"); + } +}; + + std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK); extern RecordKeeper Records;