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