LLVM API Documentation

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