LLVM API Documentation
00001 //===-- llvm/InlineAsm.h - Class to represent inline asm strings-*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class represents the inline asm strings, which are Value*'s that are 00011 // used as the callee operand of call instructions. InlineAsm's are uniqued 00012 // like constants, and created via InlineAsm::get(...). 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_IR_INLINEASM_H 00017 #define LLVM_IR_INLINEASM_H 00018 00019 #include "llvm/ADT/StringRef.h" 00020 #include "llvm/IR/Value.h" 00021 #include <vector> 00022 00023 namespace llvm { 00024 00025 class PointerType; 00026 class FunctionType; 00027 class Module; 00028 struct InlineAsmKeyType; 00029 template<class ValType, class ValRefType, class TypeClass, class ConstantClass, 00030 bool HasLargeKey> 00031 class ConstantUniqueMap; 00032 template<class ConstantClass, class TypeClass, class ValType> 00033 struct ConstantCreator; 00034 00035 class InlineAsm : public Value { 00036 public: 00037 enum AsmDialect { 00038 AD_ATT, 00039 AD_Intel 00040 }; 00041 00042 private: 00043 friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>; 00044 friend class ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, 00045 PointerType, InlineAsm, false>; 00046 00047 InlineAsm(const InlineAsm &) LLVM_DELETED_FUNCTION; 00048 void operator=(const InlineAsm&) LLVM_DELETED_FUNCTION; 00049 00050 std::string AsmString, Constraints; 00051 bool HasSideEffects; 00052 bool IsAlignStack; 00053 AsmDialect Dialect; 00054 00055 InlineAsm(PointerType *Ty, const std::string &AsmString, 00056 const std::string &Constraints, bool hasSideEffects, 00057 bool isAlignStack, AsmDialect asmDialect); 00058 virtual ~InlineAsm(); 00059 00060 /// When the ConstantUniqueMap merges two types and makes two InlineAsms 00061 /// identical, it destroys one of them with this method. 00062 void destroyConstant(); 00063 public: 00064 00065 /// InlineAsm::get - Return the specified uniqued inline asm string. 00066 /// 00067 static InlineAsm *get(FunctionType *Ty, StringRef AsmString, 00068 StringRef Constraints, bool hasSideEffects, 00069 bool isAlignStack = false, 00070 AsmDialect asmDialect = AD_ATT); 00071 00072 bool hasSideEffects() const { return HasSideEffects; } 00073 bool isAlignStack() const { return IsAlignStack; } 00074 AsmDialect getDialect() const { return Dialect; } 00075 00076 /// getType - InlineAsm's are always pointers. 00077 /// 00078 PointerType *getType() const { 00079 return reinterpret_cast<PointerType*>(Value::getType()); 00080 } 00081 00082 /// getFunctionType - InlineAsm's are always pointers to functions. 00083 /// 00084 FunctionType *getFunctionType() const; 00085 00086 const std::string &getAsmString() const { return AsmString; } 00087 const std::string &getConstraintString() const { return Constraints; } 00088 00089 /// Verify - This static method can be used by the parser to check to see if 00090 /// the specified constraint string is legal for the type. This returns true 00091 /// if legal, false if not. 00092 /// 00093 static bool Verify(FunctionType *Ty, StringRef Constraints); 00094 00095 // Constraint String Parsing 00096 enum ConstraintPrefix { 00097 isInput, // 'x' 00098 isOutput, // '=x' 00099 isClobber // '~x' 00100 }; 00101 00102 typedef std::vector<std::string> ConstraintCodeVector; 00103 00104 struct SubConstraintInfo { 00105 /// MatchingInput - If this is not -1, this is an output constraint where an 00106 /// input constraint is required to match it (e.g. "0"). The value is the 00107 /// constraint number that matches this one (for example, if this is 00108 /// constraint #0 and constraint #4 has the value "0", this will be 4). 00109 signed char MatchingInput; 00110 /// Code - The constraint code, either the register name (in braces) or the 00111 /// constraint letter/number. 00112 ConstraintCodeVector Codes; 00113 /// Default constructor. 00114 SubConstraintInfo() : MatchingInput(-1) {} 00115 }; 00116 00117 typedef std::vector<SubConstraintInfo> SubConstraintInfoVector; 00118 struct ConstraintInfo; 00119 typedef std::vector<ConstraintInfo> ConstraintInfoVector; 00120 00121 struct ConstraintInfo { 00122 /// Type - The basic type of the constraint: input/output/clobber 00123 /// 00124 ConstraintPrefix Type; 00125 00126 /// isEarlyClobber - "&": output operand writes result before inputs are all 00127 /// read. This is only ever set for an output operand. 00128 bool isEarlyClobber; 00129 00130 /// MatchingInput - If this is not -1, this is an output constraint where an 00131 /// input constraint is required to match it (e.g. "0"). The value is the 00132 /// constraint number that matches this one (for example, if this is 00133 /// constraint #0 and constraint #4 has the value "0", this will be 4). 00134 signed char MatchingInput; 00135 00136 /// hasMatchingInput - Return true if this is an output constraint that has 00137 /// a matching input constraint. 00138 bool hasMatchingInput() const { return MatchingInput != -1; } 00139 00140 /// isCommutative - This is set to true for a constraint that is commutative 00141 /// with the next operand. 00142 bool isCommutative; 00143 00144 /// isIndirect - True if this operand is an indirect operand. This means 00145 /// that the address of the source or destination is present in the call 00146 /// instruction, instead of it being returned or passed in explicitly. This 00147 /// is represented with a '*' in the asm string. 00148 bool isIndirect; 00149 00150 /// Code - The constraint code, either the register name (in braces) or the 00151 /// constraint letter/number. 00152 ConstraintCodeVector Codes; 00153 00154 /// isMultipleAlternative - '|': has multiple-alternative constraints. 00155 bool isMultipleAlternative; 00156 00157 /// multipleAlternatives - If there are multiple alternative constraints, 00158 /// this array will contain them. Otherwise it will be empty. 00159 SubConstraintInfoVector multipleAlternatives; 00160 00161 /// The currently selected alternative constraint index. 00162 unsigned currentAlternativeIndex; 00163 00164 ///Default constructor. 00165 ConstraintInfo(); 00166 00167 /// Copy constructor. 00168 ConstraintInfo(const ConstraintInfo &other); 00169 00170 /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the 00171 /// fields in this structure. If the constraint string is not understood, 00172 /// return true, otherwise return false. 00173 bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar); 00174 00175 /// selectAlternative - Point this constraint to the alternative constraint 00176 /// indicated by the index. 00177 void selectAlternative(unsigned index); 00178 }; 00179 00180 /// ParseConstraints - Split up the constraint string into the specific 00181 /// constraints and their prefixes. If this returns an empty vector, and if 00182 /// the constraint string itself isn't empty, there was an error parsing. 00183 static ConstraintInfoVector ParseConstraints(StringRef ConstraintString); 00184 00185 /// ParseConstraints - Parse the constraints of this inlineasm object, 00186 /// returning them the same way that ParseConstraints(str) does. 00187 ConstraintInfoVector ParseConstraints() const { 00188 return ParseConstraints(Constraints); 00189 } 00190 00191 // Methods for support type inquiry through isa, cast, and dyn_cast: 00192 static inline bool classof(const Value *V) { 00193 return V->getValueID() == Value::InlineAsmVal; 00194 } 00195 00196 00197 // These are helper methods for dealing with flags in the INLINEASM SDNode 00198 // in the backend. 00199 00200 enum { 00201 // Fixed operands on an INLINEASM SDNode. 00202 Op_InputChain = 0, 00203 Op_AsmString = 1, 00204 Op_MDNode = 2, 00205 Op_ExtraInfo = 3, // HasSideEffects, IsAlignStack, AsmDialect. 00206 Op_FirstOperand = 4, 00207 00208 // Fixed operands on an INLINEASM MachineInstr. 00209 MIOp_AsmString = 0, 00210 MIOp_ExtraInfo = 1, // HasSideEffects, IsAlignStack, AsmDialect. 00211 MIOp_FirstOperand = 2, 00212 00213 // Interpretation of the MIOp_ExtraInfo bit field. 00214 Extra_HasSideEffects = 1, 00215 Extra_IsAlignStack = 2, 00216 Extra_AsmDialect = 4, 00217 Extra_MayLoad = 8, 00218 Extra_MayStore = 16, 00219 00220 // Inline asm operands map to multiple SDNode / MachineInstr operands. 00221 // The first operand is an immediate describing the asm operand, the low 00222 // bits is the kind: 00223 Kind_RegUse = 1, // Input register, "r". 00224 Kind_RegDef = 2, // Output register, "=r". 00225 Kind_RegDefEarlyClobber = 3, // Early-clobber output register, "=&r". 00226 Kind_Clobber = 4, // Clobbered register, "~r". 00227 Kind_Imm = 5, // Immediate. 00228 Kind_Mem = 6, // Memory operand, "m". 00229 00230 Flag_MatchingOperand = 0x80000000 00231 }; 00232 00233 static unsigned getFlagWord(unsigned Kind, unsigned NumOps) { 00234 assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!"); 00235 assert(Kind >= Kind_RegUse && Kind <= Kind_Mem && "Invalid Kind"); 00236 return Kind | (NumOps << 3); 00237 } 00238 00239 /// getFlagWordForMatchingOp - Augment an existing flag word returned by 00240 /// getFlagWord with information indicating that this input operand is tied 00241 /// to a previous output operand. 00242 static unsigned getFlagWordForMatchingOp(unsigned InputFlag, 00243 unsigned MatchedOperandNo) { 00244 assert(MatchedOperandNo <= 0x7fff && "Too big matched operand"); 00245 assert((InputFlag & ~0xffff) == 0 && "High bits already contain data"); 00246 return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16); 00247 } 00248 00249 /// getFlagWordForRegClass - Augment an existing flag word returned by 00250 /// getFlagWord with the required register class for the following register 00251 /// operands. 00252 /// A tied use operand cannot have a register class, use the register class 00253 /// from the def operand instead. 00254 static unsigned getFlagWordForRegClass(unsigned InputFlag, unsigned RC) { 00255 // Store RC + 1, reserve the value 0 to mean 'no register class'. 00256 ++RC; 00257 assert(RC <= 0x7fff && "Too large register class ID"); 00258 assert((InputFlag & ~0xffff) == 0 && "High bits already contain data"); 00259 return InputFlag | (RC << 16); 00260 } 00261 00262 static unsigned getKind(unsigned Flags) { 00263 return Flags & 7; 00264 } 00265 00266 static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;} 00267 static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; } 00268 static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; } 00269 static bool isRegDefEarlyClobberKind(unsigned Flag) { 00270 return getKind(Flag) == Kind_RegDefEarlyClobber; 00271 } 00272 static bool isClobberKind(unsigned Flag) { 00273 return getKind(Flag) == Kind_Clobber; 00274 } 00275 00276 /// getNumOperandRegisters - Extract the number of registers field from the 00277 /// inline asm operand flag. 00278 static unsigned getNumOperandRegisters(unsigned Flag) { 00279 return (Flag & 0xffff) >> 3; 00280 } 00281 00282 /// isUseOperandTiedToDef - Return true if the flag of the inline asm 00283 /// operand indicates it is an use operand that's matched to a def operand. 00284 static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) { 00285 if ((Flag & Flag_MatchingOperand) == 0) 00286 return false; 00287 Idx = (Flag & ~Flag_MatchingOperand) >> 16; 00288 return true; 00289 } 00290 00291 /// hasRegClassConstraint - Returns true if the flag contains a register 00292 /// class constraint. Sets RC to the register class ID. 00293 static bool hasRegClassConstraint(unsigned Flag, unsigned &RC) { 00294 if (Flag & Flag_MatchingOperand) 00295 return false; 00296 unsigned High = Flag >> 16; 00297 // getFlagWordForRegClass() uses 0 to mean no register class, and otherwise 00298 // stores RC + 1. 00299 if (!High) 00300 return false; 00301 RC = High - 1; 00302 return true; 00303 } 00304 00305 }; 00306 00307 } // End llvm namespace 00308 00309 #endif