Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
11 KiB

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