Model :upper16: and :lower16: as ARM specific MCTargetExpr. This is a step
[oota-llvm.git] / utils / TableGen / CodeGenInstruction.h
1 //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a wrapper class for the 'Instruction' TableGen class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef CODEGEN_INSTRUCTION_H
15 #define CODEGEN_INSTRUCTION_H
16
17 #include "llvm/CodeGen/ValueTypes.h"
18 #include "llvm/ADT/StringRef.h"
19 #include <string>
20 #include <vector>
21 #include <utility>
22
23 namespace llvm {
24   class Record;
25   class DagInit;
26   class CodeGenTarget;
27   class StringRef;
28   
29   class CGIOperandList {
30   public:
31     class ConstraintInfo {
32       enum { None, EarlyClobber, Tied } Kind;
33       unsigned OtherTiedOperand;
34     public:
35       ConstraintInfo() : Kind(None) {}
36       
37       static ConstraintInfo getEarlyClobber() {
38         ConstraintInfo I;
39         I.Kind = EarlyClobber;
40         I.OtherTiedOperand = 0;
41         return I;
42       }
43       
44       static ConstraintInfo getTied(unsigned Op) {
45         ConstraintInfo I;
46         I.Kind = Tied;
47         I.OtherTiedOperand = Op;
48         return I;
49       }
50       
51       bool isNone() const { return Kind == None; }
52       bool isEarlyClobber() const { return Kind == EarlyClobber; }
53       bool isTied() const { return Kind == Tied; }
54       
55       unsigned getTiedOperand() const {
56         assert(isTied());
57         return OtherTiedOperand;
58       }
59     };
60
61     /// OperandInfo - The information we keep track of for each operand in the
62     /// operand list for a tablegen instruction.
63     struct OperandInfo {
64       /// Rec - The definition this operand is declared as.
65       ///
66       Record *Rec;
67       
68       /// Name - If this operand was assigned a symbolic name, this is it,
69       /// otherwise, it's empty.
70       std::string Name;
71       
72       /// PrinterMethodName - The method used to print operands of this type in
73       /// the asmprinter.
74       std::string PrinterMethodName;
75       
76       /// EncoderMethodName - The method used to get the machine operand value
77       /// for binary encoding. "getMachineOpValue" by default.
78       std::string EncoderMethodName;
79       
80       /// MIOperandNo - Currently (this is meant to be phased out), some logical
81       /// operands correspond to multiple MachineInstr operands.  In the X86
82       /// target for example, one address operand is represented as 4
83       /// MachineOperands.  Because of this, the operand number in the
84       /// OperandList may not match the MachineInstr operand num.  Until it
85       /// does, this contains the MI operand index of this operand.
86       unsigned MIOperandNo;
87       unsigned MINumOperands;   // The number of operands.
88       
89       /// DoNotEncode - Bools are set to true in this vector for each operand in
90       /// the DisableEncoding list.  These should not be emitted by the code
91       /// emitter.
92       std::vector<bool> DoNotEncode;
93       
94       /// MIOperandInfo - Default MI operand type. Note an operand may be made
95       /// up of multiple MI operands.
96       DagInit *MIOperandInfo;
97       
98       /// Constraint info for this operand.  This operand can have pieces, so we
99       /// track constraint info for each.
100       std::vector<ConstraintInfo> Constraints;
101       
102       OperandInfo(Record *R, const std::string &N, const std::string &PMN,
103                   const std::string &EMN, unsigned MION, unsigned MINO,
104                   DagInit *MIOI)
105       : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
106         MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
107       
108       
109       /// getTiedOperand - If this operand is tied to another one, return the
110       /// other operand number.  Otherwise, return -1.
111       int getTiedRegister() const {
112         for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
113           const CGIOperandList::ConstraintInfo &CI = Constraints[j];
114           if (CI.isTied()) return CI.getTiedOperand();
115         }
116         return -1;
117       }
118     };
119     
120     CGIOperandList(Record *D);
121     
122     Record *TheDef;            // The actual record containing this OperandList.
123
124     /// NumDefs - Number of def operands declared, this is the number of
125     /// elements in the instruction's (outs) list.
126     ///
127     unsigned NumDefs;
128     
129     /// OperandList - The list of declared operands, along with their declared
130     /// type (which is a record).
131     std::vector<OperandInfo> OperandList;
132     
133     // Information gleaned from the operand list.
134     bool isPredicable;
135     bool hasOptionalDef;
136     bool isVariadic;
137     
138     // Provide transparent accessors to the operand list.
139     unsigned size() const { return OperandList.size(); }
140     const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
141     OperandInfo &operator[](unsigned i) { return OperandList[i]; }
142     OperandInfo &back() { return OperandList.back(); }
143     const OperandInfo &back() const { return OperandList.back(); }
144     
145     
146     /// getOperandNamed - Return the index of the operand with the specified
147     /// non-empty name.  If the instruction does not have an operand with the
148     /// specified name, throw an exception.
149     unsigned getOperandNamed(StringRef Name) const;
150     
151     /// hasOperandNamed - Query whether the instruction has an operand of the
152     /// given name. If so, return true and set OpIdx to the index of the
153     /// operand. Otherwise, return false.
154     bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
155       
156     /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
157     /// where $foo is a whole operand and $foo.bar refers to a suboperand.
158     /// This throws an exception if the name is invalid.  If AllowWholeOp is
159     /// true, references to operands with suboperands are allowed, otherwise
160     /// not.
161     std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
162                                                   bool AllowWholeOp = true);
163     
164     /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
165     /// flat machineinstr operand #.
166     unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
167       return OperandList[Op.first].MIOperandNo + Op.second;
168     }
169     
170     /// getSubOperandNumber - Unflatten a operand number into an
171     /// operand/suboperand pair.
172     std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
173       for (unsigned i = 0; ; ++i) {
174         assert(i < OperandList.size() && "Invalid flat operand #");
175         if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
176           return std::make_pair(i, Op-OperandList[i].MIOperandNo);
177       }
178     }
179     
180     
181     /// isFlatOperandNotEmitted - Return true if the specified flat operand #
182     /// should not be emitted with the code emitter.
183     bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
184       std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
185       if (OperandList[Op.first].DoNotEncode.size() > Op.second)
186         return OperandList[Op.first].DoNotEncode[Op.second];
187       return false;
188     }
189     
190     void ProcessDisableEncoding(std::string Value);
191   };
192   
193
194   class CodeGenInstruction {
195   public:
196     Record *TheDef;            // The actual record defining this instruction.
197     std::string Namespace;     // The namespace the instruction is in.
198
199     /// AsmString - The format string used to emit a .s file for the
200     /// instruction.
201     std::string AsmString;
202
203     /// Operands - This is information about the (ins) and (outs) list specified
204     /// to the instruction.
205     CGIOperandList Operands;
206
207     /// ImplicitDefs/ImplicitUses - These are lists of registers that are
208     /// implicitly defined and used by the instruction.
209     std::vector<Record*> ImplicitDefs, ImplicitUses;
210
211     // Various boolean values we track for the instruction.
212     bool isReturn;
213     bool isBranch;
214     bool isIndirectBranch;
215     bool isCompare;
216     bool isMoveImm;
217     bool isBarrier;
218     bool isCall;
219     bool canFoldAsLoad;
220     bool mayLoad, mayStore;
221     bool isPredicable;
222     bool isConvertibleToThreeAddress;
223     bool isCommutable;
224     bool isTerminator;
225     bool isReMaterializable;
226     bool hasDelaySlot;
227     bool usesCustomInserter;
228     bool hasCtrlDep;
229     bool isNotDuplicable;
230     bool hasSideEffects;
231     bool neverHasSideEffects;
232     bool isAsCheapAsAMove;
233     bool hasExtraSrcRegAllocReq;
234     bool hasExtraDefRegAllocReq;
235
236
237     CodeGenInstruction(Record *R);
238
239     /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
240     /// implicit def and it has a known VT, return the VT, otherwise return
241     /// MVT::Other.
242     MVT::SimpleValueType
243       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
244     
245     
246     /// FlattenAsmStringVariants - Flatten the specified AsmString to only
247     /// include text from the specified variant, returning the new string.
248     static std::string FlattenAsmStringVariants(StringRef AsmString,
249                                                 unsigned Variant);
250   };
251   
252   
253   /// CodeGenInstAlias - This represents an InstAlias definition.
254   class CodeGenInstAlias {
255   public:
256     Record *TheDef;            // The actual record defining this InstAlias.
257     
258     /// AsmString - The format string used to emit a .s file for the
259     /// instruction.
260     std::string AsmString;
261     
262     /// Result - The result instruction.
263     DagInit *Result;
264     
265     /// ResultInst - The instruction generated by the alias (decoded from
266     /// Result).
267     CodeGenInstruction *ResultInst;
268     
269     
270     struct ResultOperand {
271     private:
272       StringRef Name;
273       Record *R;
274       
275       int64_t Imm;
276     public:      
277       enum {
278         K_Record,
279         K_Imm,
280         K_Reg
281       } Kind;
282       
283       ResultOperand(StringRef N, Record *r) : Name(N), R(r), Kind(K_Record) {}
284       ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
285       ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
286
287       bool isRecord() const { return Kind == K_Record; }
288       bool isImm() const { return Kind == K_Imm; }
289       bool isReg() const { return Kind == K_Reg; }
290       
291       StringRef getName() const { assert(isRecord()); return Name; }
292       Record *getRecord() const { assert(isRecord()); return R; }
293       int64_t getImm() const { assert(isImm()); return Imm; }
294       Record *getRegister() const { assert(isReg()); return R; }
295     };
296     
297     /// ResultOperands - The decoded operands for the result instruction.
298     std::vector<ResultOperand> ResultOperands;
299     
300     CodeGenInstAlias(Record *R, CodeGenTarget &T);
301     
302     /// getResultInstOperandIndexForResultOperandIndex - Given an index into the
303     /// ResultOperands array, translate it to a valid index in ResultInst's
304     /// operand list.
305     unsigned getResultInstOperandIndexForResultOperandIndex(unsigned i) const;
306   };    
307 }
308
309 #endif