1 //===-- llvm/InlineAsm.h - Class to represent inline asm strings-*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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(...).
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INLINEASM_H
17 #define LLVM_INLINEASM_H
19 #include "llvm/Value.h"
27 struct InlineAsmKeyType;
28 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
30 class ConstantUniqueMap;
31 template<class ConstantClass, class TypeClass, class ValType>
32 struct ConstantCreator;
34 class InlineAsm : public Value {
35 friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>;
36 friend class ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&,
37 PointerType, InlineAsm, false>;
39 InlineAsm(const InlineAsm &); // do not implement
40 void operator=(const InlineAsm&); // do not implement
42 std::string AsmString, Constraints;
46 InlineAsm(PointerType *Ty, const std::string &AsmString,
47 const std::string &Constraints, bool hasSideEffects,
51 /// When the ConstantUniqueMap merges two types and makes two InlineAsms
52 /// identical, it destroys one of them with this method.
53 void destroyConstant();
56 /// InlineAsm::get - Return the specified uniqued inline asm string.
58 static InlineAsm *get(FunctionType *Ty, StringRef AsmString,
59 StringRef Constraints, bool hasSideEffects,
60 bool isAlignStack = false);
62 bool hasSideEffects() const { return HasSideEffects; }
63 bool isAlignStack() const { return IsAlignStack; }
65 /// getType - InlineAsm's are always pointers.
67 PointerType *getType() const {
68 return reinterpret_cast<PointerType*>(Value::getType());
71 /// getFunctionType - InlineAsm's are always pointers to functions.
73 FunctionType *getFunctionType() const;
75 const std::string &getAsmString() const { return AsmString; }
76 const std::string &getConstraintString() const { return Constraints; }
78 /// Verify - This static method can be used by the parser to check to see if
79 /// the specified constraint string is legal for the type. This returns true
80 /// if legal, false if not.
82 static bool Verify(FunctionType *Ty, StringRef Constraints);
84 // Constraint String Parsing
85 enum ConstraintPrefix {
91 typedef std::vector<std::string> ConstraintCodeVector;
93 struct SubConstraintInfo {
94 /// MatchingInput - If this is not -1, this is an output constraint where an
95 /// input constraint is required to match it (e.g. "0"). The value is the
96 /// constraint number that matches this one (for example, if this is
97 /// constraint #0 and constraint #4 has the value "0", this will be 4).
98 signed char MatchingInput;
99 /// Code - The constraint code, either the register name (in braces) or the
100 /// constraint letter/number.
101 ConstraintCodeVector Codes;
102 /// Default constructor.
103 SubConstraintInfo() : MatchingInput(-1) {}
106 typedef std::vector<SubConstraintInfo> SubConstraintInfoVector;
107 struct ConstraintInfo;
108 typedef std::vector<ConstraintInfo> ConstraintInfoVector;
110 struct ConstraintInfo {
111 /// Type - The basic type of the constraint: input/output/clobber
113 ConstraintPrefix Type;
115 /// isEarlyClobber - "&": output operand writes result before inputs are all
116 /// read. This is only ever set for an output operand.
119 /// MatchingInput - If this is not -1, this is an output constraint where an
120 /// input constraint is required to match it (e.g. "0"). The value is the
121 /// constraint number that matches this one (for example, if this is
122 /// constraint #0 and constraint #4 has the value "0", this will be 4).
123 signed char MatchingInput;
125 /// hasMatchingInput - Return true if this is an output constraint that has
126 /// a matching input constraint.
127 bool hasMatchingInput() const { return MatchingInput != -1; }
129 /// isCommutative - This is set to true for a constraint that is commutative
130 /// with the next operand.
133 /// isIndirect - True if this operand is an indirect operand. This means
134 /// that the address of the source or destination is present in the call
135 /// instruction, instead of it being returned or passed in explicitly. This
136 /// is represented with a '*' in the asm string.
139 /// Code - The constraint code, either the register name (in braces) or the
140 /// constraint letter/number.
141 ConstraintCodeVector Codes;
143 /// isMultipleAlternative - '|': has multiple-alternative constraints.
144 bool isMultipleAlternative;
146 /// multipleAlternatives - If there are multiple alternative constraints,
147 /// this array will contain them. Otherwise it will be empty.
148 SubConstraintInfoVector multipleAlternatives;
150 /// The currently selected alternative constraint index.
151 unsigned currentAlternativeIndex;
153 ///Default constructor.
156 /// Copy constructor.
157 ConstraintInfo(const ConstraintInfo &other);
159 /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
160 /// fields in this structure. If the constraint string is not understood,
161 /// return true, otherwise return false.
162 bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
164 /// selectAlternative - Point this constraint to the alternative constraint
165 /// indicated by the index.
166 void selectAlternative(unsigned index);
169 /// ParseConstraints - Split up the constraint string into the specific
170 /// constraints and their prefixes. If this returns an empty vector, and if
171 /// the constraint string itself isn't empty, there was an error parsing.
172 static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
174 /// ParseConstraints - Parse the constraints of this inlineasm object,
175 /// returning them the same way that ParseConstraints(str) does.
176 ConstraintInfoVector ParseConstraints() const {
177 return ParseConstraints(Constraints);
180 // Methods for support type inquiry through isa, cast, and dyn_cast:
181 static inline bool classof(const InlineAsm *) { return true; }
182 static inline bool classof(const Value *V) {
183 return V->getValueID() == Value::InlineAsmVal;
187 // These are helper methods for dealing with flags in the INLINEASM SDNode
191 // Fixed operands on an INLINEASM SDNode.
195 Op_ExtraInfo = 3, // HasSideEffects, IsAlignStack
198 // Fixed operands on an INLINEASM MachineInstr.
200 MIOp_ExtraInfo = 1, // HasSideEffects, IsAlignStack
201 MIOp_FirstOperand = 2,
203 // Interpretation of the MIOp_ExtraInfo bit field.
204 Extra_HasSideEffects = 1,
205 Extra_IsAlignStack = 2,
207 // Inline asm operands map to multiple SDNode / MachineInstr operands.
208 // The first operand is an immediate describing the asm operand, the low
210 Kind_RegUse = 1, // Input register, "r".
211 Kind_RegDef = 2, // Output register, "=r".
212 Kind_RegDefEarlyClobber = 3, // Early-clobber output register, "=&r".
213 Kind_Clobber = 4, // Clobbered register, "~r".
214 Kind_Imm = 5, // Immediate.
215 Kind_Mem = 6, // Memory operand, "m".
217 Flag_MatchingOperand = 0x80000000
220 static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
221 assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
222 return Kind | (NumOps << 3);
225 /// getFlagWordForMatchingOp - Augment an existing flag word returned by
226 /// getFlagWord with information indicating that this input operand is tied
227 /// to a previous output operand.
228 static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
229 unsigned MatchedOperandNo) {
230 return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
233 static unsigned getKind(unsigned Flags) {
237 static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
238 static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
239 static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
240 static bool isRegDefEarlyClobberKind(unsigned Flag) {
241 return getKind(Flag) == Kind_RegDefEarlyClobber;
243 static bool isClobberKind(unsigned Flag) {
244 return getKind(Flag) == Kind_Clobber;
247 /// getNumOperandRegisters - Extract the number of registers field from the
248 /// inline asm operand flag.
249 static unsigned getNumOperandRegisters(unsigned Flag) {
250 return (Flag & 0xffff) >> 3;
253 /// isUseOperandTiedToDef - Return true if the flag of the inline asm
254 /// operand indicates it is an use operand that's matched to a def operand.
255 static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
256 if ((Flag & Flag_MatchingOperand) == 0)
258 Idx = (Flag & ~Flag_MatchingOperand) >> 16;
265 } // End llvm namespace