Allow targets to optionally specify custom binary encoder functions for
[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 <string>
19 #include <vector>
20 #include <utility>
21
22 namespace llvm {
23   class Record;
24   class DagInit;
25   class CodeGenTarget;
26
27   class CodeGenInstruction {
28   public:
29     Record *TheDef;            // The actual record defining this instruction.
30     std::string Namespace;     // The namespace the instruction is in.
31
32     /// AsmString - The format string used to emit a .s file for the
33     /// instruction.
34     std::string AsmString;
35
36     class ConstraintInfo {
37       enum { None, EarlyClobber, Tied } Kind;
38       unsigned OtherTiedOperand;
39     public:
40       ConstraintInfo() : Kind(None) {}
41
42       static ConstraintInfo getEarlyClobber() {
43         ConstraintInfo I;
44         I.Kind = EarlyClobber;
45         I.OtherTiedOperand = 0;
46         return I;
47       }
48
49       static ConstraintInfo getTied(unsigned Op) {
50         ConstraintInfo I;
51         I.Kind = Tied;
52         I.OtherTiedOperand = Op;
53         return I;
54       }
55
56       bool isNone() const { return Kind == None; }
57       bool isEarlyClobber() const { return Kind == EarlyClobber; }
58       bool isTied() const { return Kind == Tied; }
59
60       unsigned getTiedOperand() const {
61         assert(isTied());
62         return OtherTiedOperand;
63       }
64     };
65
66     /// OperandInfo - The information we keep track of for each operand in the
67     /// operand list for a tablegen instruction.
68     struct OperandInfo {
69       /// Rec - The definition this operand is declared as.
70       ///
71       Record *Rec;
72
73       /// Name - If this operand was assigned a symbolic name, this is it,
74       /// otherwise, it's empty.
75       std::string Name;
76
77       /// PrinterMethodName - The method used to print operands of this type in
78       /// the asmprinter.
79       std::string PrinterMethodName;
80
81       /// EncoderMethodName - The method used to get the machine operand value
82       /// for binary encoding. "getMachineOpValue" by default.
83       std::string EncoderMethodName;
84
85       /// MIOperandNo - Currently (this is meant to be phased out), some logical
86       /// operands correspond to multiple MachineInstr operands.  In the X86
87       /// target for example, one address operand is represented as 4
88       /// MachineOperands.  Because of this, the operand number in the
89       /// OperandList may not match the MachineInstr operand num.  Until it
90       /// does, this contains the MI operand index of this operand.
91       unsigned MIOperandNo;
92       unsigned MINumOperands;   // The number of operands.
93
94       /// DoNotEncode - Bools are set to true in this vector for each operand in
95       /// the DisableEncoding list.  These should not be emitted by the code
96       /// emitter.
97       std::vector<bool> DoNotEncode;
98
99       /// MIOperandInfo - Default MI operand type. Note an operand may be made
100       /// up of multiple MI operands.
101       DagInit *MIOperandInfo;
102
103       /// Constraint info for this operand.  This operand can have pieces, so we
104       /// track constraint info for each.
105       std::vector<ConstraintInfo> Constraints;
106
107       OperandInfo(Record *R, const std::string &N, const std::string &PMN,
108                   const std::string &EMN, unsigned MION, unsigned MINO,
109                   DagInit *MIOI)
110         : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
111           MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
112     };
113
114     /// NumDefs - Number of def operands declared, this is the number of
115     /// elements in the instruction's (outs) list.
116     ///
117     unsigned NumDefs;
118
119     /// OperandList - The list of declared operands, along with their declared
120     /// type (which is a record).
121     std::vector<OperandInfo> OperandList;
122
123     /// ImplicitDefs/ImplicitUses - These are lists of registers that are
124     /// implicitly defined and used by the instruction.
125     std::vector<Record*> ImplicitDefs, ImplicitUses;
126
127     // Various boolean values we track for the instruction.
128     bool isReturn;
129     bool isBranch;
130     bool isIndirectBranch;
131     bool isCompare;
132     bool isBarrier;
133     bool isCall;
134     bool canFoldAsLoad;
135     bool mayLoad, mayStore;
136     bool isPredicable;
137     bool isConvertibleToThreeAddress;
138     bool isCommutable;
139     bool isTerminator;
140     bool isReMaterializable;
141     bool hasDelaySlot;
142     bool usesCustomInserter;
143     bool isVariadic;
144     bool hasCtrlDep;
145     bool isNotDuplicable;
146     bool hasOptionalDef;
147     bool hasSideEffects;
148     bool neverHasSideEffects;
149     bool isAsCheapAsAMove;
150     bool hasExtraSrcRegAllocReq;
151     bool hasExtraDefRegAllocReq;
152
153     /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
154     /// where $foo is a whole operand and $foo.bar refers to a suboperand.
155     /// This throws an exception if the name is invalid.  If AllowWholeOp is
156     /// true, references to operands with suboperands are allowed, otherwise
157     /// not.
158     std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
159                                                   bool AllowWholeOp = true);
160
161     /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
162     /// flat machineinstr operand #.
163     unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
164       return OperandList[Op.first].MIOperandNo + Op.second;
165     }
166
167     /// getSubOperandNumber - Unflatten a operand number into an
168     /// operand/suboperand pair.
169     std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
170       for (unsigned i = 0; ; ++i) {
171         assert(i < OperandList.size() && "Invalid flat operand #");
172         if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
173           return std::make_pair(i, Op-OperandList[i].MIOperandNo);
174       }
175     }
176
177
178     /// isFlatOperandNotEmitted - Return true if the specified flat operand #
179     /// should not be emitted with the code emitter.
180     bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
181       std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
182       if (OperandList[Op.first].DoNotEncode.size() > Op.second)
183         return OperandList[Op.first].DoNotEncode[Op.second];
184       return false;
185     }
186
187     CodeGenInstruction(Record *R, const std::string &AsmStr);
188
189     /// getOperandNamed - Return the index of the operand with the specified
190     /// non-empty name.  If the instruction does not have an operand with the
191     /// specified name, throw an exception.
192     unsigned getOperandNamed(const std::string &Name) const;
193
194     /// hasOperandNamed - Query whether the instruction has an operand of the
195     /// given name. If so, return true and set OpIdx to the index of the
196     /// operand. Otherwise, return false.
197     bool hasOperandNamed(const std::string &Name, unsigned &OpIdx) const;
198
199     /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
200     /// implicit def and it has a known VT, return the VT, otherwise return
201     /// MVT::Other.
202     MVT::SimpleValueType
203       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
204   };
205 }
206
207 #endif