[ms-inline asm] Add the inline assembly dialect, AsmDialect, to the InlineAsm
[oota-llvm.git] / include / llvm / InlineAsm.h
1 //===-- llvm/InlineAsm.h - Class to represent inline asm strings-*- 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 class represents the inline asm strings, which are Value*'s that are
11 // used as the callee operand of call instructions.  InlineAsm's are uniqued
12 // like constants, and created via InlineAsm::get(...).
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INLINEASM_H
17 #define LLVM_INLINEASM_H
18
19 #include "llvm/Value.h"
20 #include "llvm/ADT/StringRef.h"
21 #include <vector>
22
23 namespace llvm {
24
25 class PointerType;
26 class FunctionType;
27 class Module;
28 struct InlineAsmKeyType;
29 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
30          bool HasLargeKey>
31 class ConstantUniqueMap;
32 template<class ConstantClass, class TypeClass, class ValType>
33 struct ConstantCreator;
34
35 class InlineAsm : public Value {
36   friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>;
37   friend class ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&,
38                                  PointerType, InlineAsm, false>;
39
40   InlineAsm(const InlineAsm &);             // do not implement
41   void operator=(const InlineAsm&);         // do not implement
42
43   std::string AsmString, Constraints;
44   bool HasSideEffects;
45   bool IsAlignStack;
46   /// AsmDialect - 0 is AT&T (default) and 1 is the Intel dialect.
47   unsigned AsmDialect;
48
49   InlineAsm(PointerType *Ty, const std::string &AsmString,
50             const std::string &Constraints, bool hasSideEffects,
51             bool isAlignStack, unsigned asmDialect);
52   virtual ~InlineAsm();
53
54   /// When the ConstantUniqueMap merges two types and makes two InlineAsms
55   /// identical, it destroys one of them with this method.
56   void destroyConstant();
57 public:
58
59   /// InlineAsm::get - Return the specified uniqued inline asm string.
60   ///
61   static InlineAsm *get(FunctionType *Ty, StringRef AsmString,
62                         StringRef Constraints, bool hasSideEffects,
63                         bool isAlignStack = false, unsigned asmDialect = 0);
64   
65   bool hasSideEffects() const { return HasSideEffects; }
66   bool isAlignStack() const { return IsAlignStack; }
67   unsigned getDialect() const { return AsmDialect; }
68
69   /// getType - InlineAsm's are always pointers.
70   ///
71   PointerType *getType() const {
72     return reinterpret_cast<PointerType*>(Value::getType());
73   }
74   
75   /// getFunctionType - InlineAsm's are always pointers to functions.
76   ///
77   FunctionType *getFunctionType() const;
78   
79   const std::string &getAsmString() const { return AsmString; }
80   const std::string &getConstraintString() const { return Constraints; }
81
82   /// Verify - This static method can be used by the parser to check to see if
83   /// the specified constraint string is legal for the type.  This returns true
84   /// if legal, false if not.
85   ///
86   static bool Verify(FunctionType *Ty, StringRef Constraints);
87
88   // Constraint String Parsing 
89   enum ConstraintPrefix {
90     isInput,            // 'x'
91     isOutput,           // '=x'
92     isClobber           // '~x'
93   };
94   
95   typedef std::vector<std::string> ConstraintCodeVector;
96   
97   struct SubConstraintInfo {
98     /// MatchingInput - If this is not -1, this is an output constraint where an
99     /// input constraint is required to match it (e.g. "0").  The value is the
100     /// constraint number that matches this one (for example, if this is
101     /// constraint #0 and constraint #4 has the value "0", this will be 4).
102     signed char MatchingInput;
103     /// Code - The constraint code, either the register name (in braces) or the
104     /// constraint letter/number.
105     ConstraintCodeVector Codes;
106     /// Default constructor.
107     SubConstraintInfo() : MatchingInput(-1) {}
108   };
109
110   typedef std::vector<SubConstraintInfo> SubConstraintInfoVector;
111   struct ConstraintInfo;
112   typedef std::vector<ConstraintInfo> ConstraintInfoVector;
113   
114   struct ConstraintInfo {
115     /// Type - The basic type of the constraint: input/output/clobber
116     ///
117     ConstraintPrefix Type;
118     
119     /// isEarlyClobber - "&": output operand writes result before inputs are all
120     /// read.  This is only ever set for an output operand.
121     bool isEarlyClobber; 
122     
123     /// MatchingInput - If this is not -1, this is an output constraint where an
124     /// input constraint is required to match it (e.g. "0").  The value is the
125     /// constraint number that matches this one (for example, if this is
126     /// constraint #0 and constraint #4 has the value "0", this will be 4).
127     signed char MatchingInput;
128     
129     /// hasMatchingInput - Return true if this is an output constraint that has
130     /// a matching input constraint.
131     bool hasMatchingInput() const { return MatchingInput != -1; }
132     
133     /// isCommutative - This is set to true for a constraint that is commutative
134     /// with the next operand.
135     bool isCommutative;
136     
137     /// isIndirect - True if this operand is an indirect operand.  This means
138     /// that the address of the source or destination is present in the call
139     /// instruction, instead of it being returned or passed in explicitly.  This
140     /// is represented with a '*' in the asm string.
141     bool isIndirect;
142     
143     /// Code - The constraint code, either the register name (in braces) or the
144     /// constraint letter/number.
145     ConstraintCodeVector Codes;
146     
147     /// isMultipleAlternative - '|': has multiple-alternative constraints.
148     bool isMultipleAlternative;
149     
150     /// multipleAlternatives - If there are multiple alternative constraints,
151     /// this array will contain them.  Otherwise it will be empty.
152     SubConstraintInfoVector multipleAlternatives;
153     
154     /// The currently selected alternative constraint index.
155     unsigned currentAlternativeIndex;
156     
157     ///Default constructor.
158     ConstraintInfo();
159     
160     /// Copy constructor.
161     ConstraintInfo(const ConstraintInfo &other);
162     
163     /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
164     /// fields in this structure.  If the constraint string is not understood,
165     /// return true, otherwise return false.
166     bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
167                
168     /// selectAlternative - Point this constraint to the alternative constraint
169     /// indicated by the index.
170     void selectAlternative(unsigned index);
171   };
172   
173   /// ParseConstraints - Split up the constraint string into the specific
174   /// constraints and their prefixes.  If this returns an empty vector, and if
175   /// the constraint string itself isn't empty, there was an error parsing.
176   static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
177   
178   /// ParseConstraints - Parse the constraints of this inlineasm object, 
179   /// returning them the same way that ParseConstraints(str) does.
180   ConstraintInfoVector ParseConstraints() const {
181     return ParseConstraints(Constraints);
182   }
183   
184   // Methods for support type inquiry through isa, cast, and dyn_cast:
185   static inline bool classof(const InlineAsm *) { return true; }
186   static inline bool classof(const Value *V) {
187     return V->getValueID() == Value::InlineAsmVal;
188   }
189
190   
191   // These are helper methods for dealing with flags in the INLINEASM SDNode
192   // in the backend.
193   
194   enum {
195     // Fixed operands on an INLINEASM SDNode.
196     Op_InputChain = 0,
197     Op_AsmString = 1,
198     Op_MDNode = 2,
199     Op_ExtraInfo = 3,    // HasSideEffects, IsAlignStack
200     Op_FirstOperand = 4,
201
202     // Fixed operands on an INLINEASM MachineInstr.
203     MIOp_AsmString = 0,
204     MIOp_ExtraInfo = 1,    // HasSideEffects, IsAlignStack
205     MIOp_FirstOperand = 2,
206
207     // Interpretation of the MIOp_ExtraInfo bit field.
208     Extra_HasSideEffects = 1,
209     Extra_IsAlignStack = 2,
210
211     // Inline asm operands map to multiple SDNode / MachineInstr operands.
212     // The first operand is an immediate describing the asm operand, the low
213     // bits is the kind:
214     Kind_RegUse = 1,             // Input register, "r".
215     Kind_RegDef = 2,             // Output register, "=r".
216     Kind_RegDefEarlyClobber = 3, // Early-clobber output register, "=&r".
217     Kind_Clobber = 4,            // Clobbered register, "~r".
218     Kind_Imm = 5,                // Immediate.
219     Kind_Mem = 6,                // Memory operand, "m".
220
221     Flag_MatchingOperand = 0x80000000
222   };
223   
224   static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
225     assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
226     assert(Kind >= Kind_RegUse && Kind <= Kind_Mem && "Invalid Kind");
227     return Kind | (NumOps << 3);
228   }
229   
230   /// getFlagWordForMatchingOp - Augment an existing flag word returned by
231   /// getFlagWord with information indicating that this input operand is tied 
232   /// to a previous output operand.
233   static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
234                                            unsigned MatchedOperandNo) {
235     assert(MatchedOperandNo <= 0x7fff && "Too big matched operand");
236     assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
237     return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
238   }
239
240   /// getFlagWordForRegClass - Augment an existing flag word returned by
241   /// getFlagWord with the required register class for the following register
242   /// operands.
243   /// A tied use operand cannot have a register class, use the register class
244   /// from the def operand instead.
245   static unsigned getFlagWordForRegClass(unsigned InputFlag, unsigned RC) {
246     // Store RC + 1, reserve the value 0 to mean 'no register class'.
247     ++RC;
248     assert(RC <= 0x7fff && "Too large register class ID");
249     assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
250     return InputFlag | (RC << 16);
251   }
252
253   static unsigned getKind(unsigned Flags) {
254     return Flags & 7;
255   }
256
257   static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
258   static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
259   static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
260   static bool isRegDefEarlyClobberKind(unsigned Flag) {
261     return getKind(Flag) == Kind_RegDefEarlyClobber;
262   }
263   static bool isClobberKind(unsigned Flag) {
264     return getKind(Flag) == Kind_Clobber;
265   }
266
267   /// getNumOperandRegisters - Extract the number of registers field from the
268   /// inline asm operand flag.
269   static unsigned getNumOperandRegisters(unsigned Flag) {
270     return (Flag & 0xffff) >> 3;
271   }
272
273   /// isUseOperandTiedToDef - Return true if the flag of the inline asm
274   /// operand indicates it is an use operand that's matched to a def operand.
275   static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
276     if ((Flag & Flag_MatchingOperand) == 0)
277       return false;
278     Idx = (Flag & ~Flag_MatchingOperand) >> 16;
279     return true;
280   }
281
282   /// hasRegClassConstraint - Returns true if the flag contains a register
283   /// class constraint.  Sets RC to the register class ID.
284   static bool hasRegClassConstraint(unsigned Flag, unsigned &RC) {
285     if (Flag & Flag_MatchingOperand)
286       return false;
287     unsigned High = Flag >> 16;
288     // getFlagWordForRegClass() uses 0 to mean no register class, and otherwise
289     // stores RC + 1.
290     if (!High)
291       return false;
292     RC = High - 1;
293     return true;
294   }
295
296 };
297
298 } // End llvm namespace
299
300 #endif