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 TypeClass, class ConstantClass, bool HasLargeKey>
29 class ConstantUniqueMap;
30 template<class ConstantClass, class TypeClass, class ValType>
31 struct ConstantCreator;
33 class InlineAsm : public Value {
34 friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>;
35 friend class ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm,
38 InlineAsm(const InlineAsm &); // do not implement
39 void operator=(const InlineAsm&); // do not implement
41 std::string AsmString, Constraints;
45 InlineAsm(const PointerType *Ty, const std::string &AsmString,
46 const std::string &Constraints, bool hasSideEffects,
50 /// When the ConstantUniqueMap merges two types and makes two InlineAsms
51 /// identical, it destroys one of them with this method.
52 void destroyConstant();
55 /// InlineAsm::get - Return the specified uniqued inline asm string.
57 static InlineAsm *get(const FunctionType *Ty, StringRef AsmString,
58 StringRef Constraints, bool hasSideEffects,
59 bool isAlignStack = false);
61 bool hasSideEffects() const { return HasSideEffects; }
62 bool isAlignStack() const { return IsAlignStack; }
64 /// getType - InlineAsm's are always pointers.
66 const PointerType *getType() const {
67 return reinterpret_cast<const PointerType*>(Value::getType());
70 /// getFunctionType - InlineAsm's are always pointers to functions.
72 const FunctionType *getFunctionType() const;
74 const std::string &getAsmString() const { return AsmString; }
75 const std::string &getConstraintString() const { return Constraints; }
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.
81 static bool Verify(const FunctionType *Ty, StringRef Constraints);
83 // Constraint String Parsing
84 enum ConstraintPrefix {
90 typedef std::vector<std::string> ConstraintCodeVector;
92 struct SubConstraintInfo {
93 /// MatchingInput - If this is not -1, this is an output constraint where an
94 /// input constraint is required to match it (e.g. "0"). The value is the
95 /// constraint number that matches this one (for example, if this is
96 /// constraint #0 and constraint #4 has the value "0", this will be 4).
97 signed char MatchingInput;
98 /// Code - The constraint code, either the register name (in braces) or the
99 /// constraint letter/number.
100 ConstraintCodeVector Codes;
101 /// Default constructor.
102 SubConstraintInfo() : MatchingInput(-1) {}
105 typedef std::vector<SubConstraintInfo> SubConstraintInfoVector;
106 struct ConstraintInfo;
107 typedef std::vector<ConstraintInfo> ConstraintInfoVector;
109 struct ConstraintInfo {
110 /// Type - The basic type of the constraint: input/output/clobber
112 ConstraintPrefix Type;
114 /// isEarlyClobber - "&": output operand writes result before inputs are all
115 /// read. This is only ever set for an output operand.
118 /// MatchingInput - If this is not -1, this is an output constraint where an
119 /// input constraint is required to match it (e.g. "0"). The value is the
120 /// constraint number that matches this one (for example, if this is
121 /// constraint #0 and constraint #4 has the value "0", this will be 4).
122 signed char MatchingInput;
124 /// hasMatchingInput - Return true if this is an output constraint that has
125 /// a matching input constraint.
126 bool hasMatchingInput() const { return MatchingInput != -1; }
128 /// isCommutative - This is set to true for a constraint that is commutative
129 /// with the next operand.
132 /// isIndirect - True if this operand is an indirect operand. This means
133 /// that the address of the source or destination is present in the call
134 /// instruction, instead of it being returned or passed in explicitly. This
135 /// is represented with a '*' in the asm string.
138 /// Code - The constraint code, either the register name (in braces) or the
139 /// constraint letter/number.
140 ConstraintCodeVector Codes;
142 /// isMultipleAlternative - '|': has multiple-alternative constraints.
143 bool isMultipleAlternative;
145 /// multipleAlternatives - If there are multiple alternative constraints,
146 /// this array will contain them. Otherwise it will be empty.
147 SubConstraintInfoVector multipleAlternatives;
149 /// The currently selected alternative constraint index.
150 unsigned currentAlternativeIndex;
152 ///Default constructor.
155 /// Copy constructor.
156 ConstraintInfo(const ConstraintInfo &other);
158 /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
159 /// fields in this structure. If the constraint string is not understood,
160 /// return true, otherwise return false.
161 bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
163 /// selectAlternative - Point this constraint to the alternative constraint
164 /// indicated by the index.
165 void selectAlternative(unsigned index);
168 /// ParseConstraints - Split up the constraint string into the specific
169 /// constraints and their prefixes. If this returns an empty vector, and if
170 /// the constraint string itself isn't empty, there was an error parsing.
171 static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
173 /// ParseConstraints - Parse the constraints of this inlineasm object,
174 /// returning them the same way that ParseConstraints(str) does.
175 ConstraintInfoVector ParseConstraints() const {
176 return ParseConstraints(Constraints);
179 // Methods for support type inquiry through isa, cast, and dyn_cast:
180 static inline bool classof(const InlineAsm *) { return true; }
181 static inline bool classof(const Value *V) {
182 return V->getValueID() == Value::InlineAsmVal;
186 // These are helper methods for dealing with flags in the INLINEASM SDNode
200 Kind_RegDefEarlyClobber = 6,
202 Flag_MatchingOperand = 0x80000000
205 static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
206 assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
207 return Kind | (NumOps << 3);
210 /// getFlagWordForMatchingOp - Augment an existing flag word returned by
211 /// getFlagWord with information indicating that this input operand is tied
212 /// to a previous output operand.
213 static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
214 unsigned MatchedOperandNo) {
215 return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
218 static unsigned getKind(unsigned Flags) {
222 static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
223 static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
224 static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
225 static bool isRegDefEarlyClobberKind(unsigned Flag) {
226 return getKind(Flag) == Kind_RegDefEarlyClobber;
229 /// getNumOperandRegisters - Extract the number of registers field from the
230 /// inline asm operand flag.
231 static unsigned getNumOperandRegisters(unsigned Flag) {
232 return (Flag & 0xffff) >> 3;
235 /// isUseOperandTiedToDef - Return true if the flag of the inline asm
236 /// operand indicates it is an use operand that's matched to a def operand.
237 static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
238 if ((Flag & Flag_MatchingOperand) == 0)
240 Idx = (Flag & ~Flag_MatchingOperand) >> 16;
247 } // End llvm namespace