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