Propagate the AlignStack bit in InlineAsm's to the
[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 <vector>
21
22 namespace llvm {
23
24 class PointerType;
25 class FunctionType;
26 class Module;
27 struct InlineAsmKeyType;
28 template<class ValType, class TypeClass, class ConstantClass, bool HasLargeKey>
29 class ConstantUniqueMap;
30 template<class ConstantClass, class TypeClass, class ValType>
31 struct ConstantCreator;
32
33 class InlineAsm : public Value {
34   friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>;
35   friend class ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm,
36                                  false>;
37
38   InlineAsm(const InlineAsm &);             // do not implement
39   void operator=(const InlineAsm&);         // do not implement
40
41   std::string AsmString, Constraints;
42   bool HasSideEffects;
43   bool IsAlignStack;
44   
45   InlineAsm(const PointerType *Ty, const std::string &AsmString,
46             const std::string &Constraints, bool hasSideEffects,
47             bool isAlignStack);
48   virtual ~InlineAsm();
49
50   /// When the ConstantUniqueMap merges two types and makes two InlineAsms
51   /// identical, it destroys one of them with this method.
52   void destroyConstant();
53 public:
54
55   /// InlineAsm::get - Return the specified uniqued inline asm string.
56   ///
57   static InlineAsm *get(const FunctionType *Ty, StringRef AsmString,
58                         StringRef Constraints, bool hasSideEffects,
59                         bool isAlignStack = false);
60   
61   bool hasSideEffects() const { return HasSideEffects; }
62   bool isAlignStack() const { return IsAlignStack; }
63   
64   /// getType - InlineAsm's are always pointers.
65   ///
66   const PointerType *getType() const {
67     return reinterpret_cast<const PointerType*>(Value::getType());
68   }
69   
70   /// getFunctionType - InlineAsm's are always pointers to functions.
71   ///
72   const FunctionType *getFunctionType() const;
73   
74   const std::string &getAsmString() const { return AsmString; }
75   const std::string &getConstraintString() const { return Constraints; }
76
77   /// Verify - This static method can be used by the parser to check to see if
78   /// the specified constraint string is legal for the type.  This returns true
79   /// if legal, false if not.
80   ///
81   static bool Verify(const FunctionType *Ty, StringRef Constraints);
82
83   // Constraint String Parsing 
84   enum ConstraintPrefix {
85     isInput,            // 'x'
86     isOutput,           // '=x'
87     isClobber           // '~x'
88   };
89   
90   struct ConstraintInfo {
91     /// Type - The basic type of the constraint: input/output/clobber
92     ///
93     ConstraintPrefix Type;
94     
95     /// isEarlyClobber - "&": output operand writes result before inputs are all
96     /// read.  This is only ever set for an output operand.
97     bool isEarlyClobber; 
98     
99     /// MatchingInput - If this is not -1, this is an output constraint where an
100     /// input constraint is required to match it (e.g. "0").  The value is the
101     /// constraint number that matches this one (for example, if this is
102     /// constraint #0 and constraint #4 has the value "0", this will be 4).
103     signed char MatchingInput;
104     
105     /// hasMatchingInput - Return true if this is an output constraint that has
106     /// a matching input constraint.
107     bool hasMatchingInput() const { return MatchingInput != -1; }
108     
109     /// isCommutative - This is set to true for a constraint that is commutative
110     /// with the next operand.
111     bool isCommutative;
112     
113     /// isIndirect - True if this operand is an indirect operand.  This means
114     /// that the address of the source or destination is present in the call
115     /// instruction, instead of it being returned or passed in explicitly.  This
116     /// is represented with a '*' in the asm string.
117     bool isIndirect;
118     
119     /// Code - The constraint code, either the register name (in braces) or the
120     /// constraint letter/number.
121     std::vector<std::string> Codes;
122     
123     /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
124     /// fields in this structure.  If the constraint string is not understood,
125     /// return true, otherwise return false.
126     bool Parse(StringRef Str, 
127                std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar);
128   };
129   
130   /// ParseConstraints - Split up the constraint string into the specific
131   /// constraints and their prefixes.  If this returns an empty vector, and if
132   /// the constraint string itself isn't empty, there was an error parsing.
133   static std::vector<ConstraintInfo> 
134     ParseConstraints(StringRef ConstraintString);
135   
136   /// ParseConstraints - Parse the constraints of this inlineasm object, 
137   /// returning them the same way that ParseConstraints(str) does.
138   std::vector<ConstraintInfo> 
139   ParseConstraints() const {
140     return ParseConstraints(Constraints);
141   }
142   
143   // Methods for support type inquiry through isa, cast, and dyn_cast:
144   static inline bool classof(const InlineAsm *) { return true; }
145   static inline bool classof(const Value *V) {
146     return V->getValueID() == Value::InlineAsmVal;
147   }
148
149   
150   // These are helper methods for dealing with flags in the INLINEASM SDNode
151   // in the backend.
152   
153   enum {
154     Op_InputChain = 0,
155     Op_AsmString = 1,
156     Op_MDNode = 2,
157     Op_IsAlignStack = 3,
158     Op_FirstOperand = 4,
159     
160     Kind_RegUse = 1,
161     Kind_RegDef = 2,
162     Kind_Imm = 3,
163     Kind_Mem = 4,
164     Kind_RegDefEarlyClobber = 6,
165     
166     Flag_MatchingOperand = 0x80000000
167   };
168   
169   static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
170     assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
171     return Kind | (NumOps << 3);
172   }
173   
174   /// getFlagWordForMatchingOp - Augment an existing flag word returned by
175   /// getFlagWord with information indicating that this input operand is tied 
176   /// to a previous output operand.
177   static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
178                                            unsigned MatchedOperandNo) {
179     return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
180   }
181
182   static unsigned getKind(unsigned Flags) {
183     return Flags & 7;
184   }
185
186   static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
187   static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
188   static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
189   static bool isRegDefEarlyClobberKind(unsigned Flag) {
190     return getKind(Flag) == Kind_RegDefEarlyClobber;
191   }
192   
193   /// getNumOperandRegisters - Extract the number of registers field from the
194   /// inline asm operand flag.
195   static unsigned getNumOperandRegisters(unsigned Flag) {
196     return (Flag & 0xffff) >> 3;
197   }
198
199   /// isUseOperandTiedToDef - Return true if the flag of the inline asm
200   /// operand indicates it is an use operand that's matched to a def operand.
201   static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
202     if ((Flag & Flag_MatchingOperand) == 0)
203       return false;
204     Idx = (Flag & ~Flag_MatchingOperand) >> 16;
205     return true;
206   }
207
208
209 };
210
211 } // End llvm namespace
212
213 #endif