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 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     /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
00168     /// fields in this structure.  If the constraint string is not understood,
00169     /// return true, otherwise return false.
00170     bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
00171                
00172     /// selectAlternative - Point this constraint to the alternative constraint
00173     /// indicated by the index.
00174     void selectAlternative(unsigned index);
00175   };
00176   
00177   /// ParseConstraints - Split up the constraint string into the specific
00178   /// constraints and their prefixes.  If this returns an empty vector, and if
00179   /// the constraint string itself isn't empty, there was an error parsing.
00180   static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
00181   
00182   /// ParseConstraints - Parse the constraints of this inlineasm object, 
00183   /// returning them the same way that ParseConstraints(str) does.
00184   ConstraintInfoVector ParseConstraints() const {
00185     return ParseConstraints(Constraints);
00186   }
00187   
00188   // Methods for support type inquiry through isa, cast, and dyn_cast:
00189   static inline bool classof(const Value *V) {
00190     return V->getValueID() == Value::InlineAsmVal;
00191   }
00192 
00193   
00194   // These are helper methods for dealing with flags in the INLINEASM SDNode
00195   // in the backend.
00196   
00197   enum : uint32_t {
00198     // Fixed operands on an INLINEASM SDNode.
00199     Op_InputChain = 0,
00200     Op_AsmString = 1,
00201     Op_MDNode = 2,
00202     Op_ExtraInfo = 3,    // HasSideEffects, IsAlignStack, AsmDialect.
00203     Op_FirstOperand = 4,
00204 
00205     // Fixed operands on an INLINEASM MachineInstr.
00206     MIOp_AsmString = 0,
00207     MIOp_ExtraInfo = 1,    // HasSideEffects, IsAlignStack, AsmDialect.
00208     MIOp_FirstOperand = 2,
00209 
00210     // Interpretation of the MIOp_ExtraInfo bit field.
00211     Extra_HasSideEffects = 1,
00212     Extra_IsAlignStack = 2,
00213     Extra_AsmDialect = 4,
00214     Extra_MayLoad = 8,
00215     Extra_MayStore = 16,
00216 
00217     // Inline asm operands map to multiple SDNode / MachineInstr operands.
00218     // The first operand is an immediate describing the asm operand, the low
00219     // bits is the kind:
00220     Kind_RegUse = 1,             // Input register, "r".
00221     Kind_RegDef = 2,             // Output register, "=r".
00222     Kind_RegDefEarlyClobber = 3, // Early-clobber output register, "=&r".
00223     Kind_Clobber = 4,            // Clobbered register, "~r".
00224     Kind_Imm = 5,                // Immediate.
00225     Kind_Mem = 6,                // Memory operand, "m".
00226 
00227     Flag_MatchingOperand = 0x80000000
00228   };
00229   
00230   static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
00231     assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
00232     assert(Kind >= Kind_RegUse && Kind <= Kind_Mem && "Invalid Kind");
00233     return Kind | (NumOps << 3);
00234   }
00235   
00236   /// getFlagWordForMatchingOp - Augment an existing flag word returned by
00237   /// getFlagWord with information indicating that this input operand is tied 
00238   /// to a previous output operand.
00239   static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
00240                                            unsigned MatchedOperandNo) {
00241     assert(MatchedOperandNo <= 0x7fff && "Too big matched operand");
00242     assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
00243     return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
00244   }
00245 
00246   /// getFlagWordForRegClass - Augment an existing flag word returned by
00247   /// getFlagWord with the required register class for the following register
00248   /// operands.
00249   /// A tied use operand cannot have a register class, use the register class
00250   /// from the def operand instead.
00251   static unsigned getFlagWordForRegClass(unsigned InputFlag, unsigned RC) {
00252     // Store RC + 1, reserve the value 0 to mean 'no register class'.
00253     ++RC;
00254     assert(RC <= 0x7fff && "Too large register class ID");
00255     assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
00256     return InputFlag | (RC << 16);
00257   }
00258 
00259   static unsigned getKind(unsigned Flags) {
00260     return Flags & 7;
00261   }
00262 
00263   static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
00264   static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
00265   static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
00266   static bool isRegDefEarlyClobberKind(unsigned Flag) {
00267     return getKind(Flag) == Kind_RegDefEarlyClobber;
00268   }
00269   static bool isClobberKind(unsigned Flag) {
00270     return getKind(Flag) == Kind_Clobber;
00271   }
00272 
00273   /// getNumOperandRegisters - Extract the number of registers field from the
00274   /// inline asm operand flag.
00275   static unsigned getNumOperandRegisters(unsigned Flag) {
00276     return (Flag & 0xffff) >> 3;
00277   }
00278 
00279   /// isUseOperandTiedToDef - Return true if the flag of the inline asm
00280   /// operand indicates it is an use operand that's matched to a def operand.
00281   static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
00282     if ((Flag & Flag_MatchingOperand) == 0)
00283       return false;
00284     Idx = (Flag & ~Flag_MatchingOperand) >> 16;
00285     return true;
00286   }
00287 
00288   /// hasRegClassConstraint - Returns true if the flag contains a register
00289   /// class constraint.  Sets RC to the register class ID.
00290   static bool hasRegClassConstraint(unsigned Flag, unsigned &RC) {
00291     if (Flag & Flag_MatchingOperand)
00292       return false;
00293     unsigned High = Flag >> 16;
00294     // getFlagWordForRegClass() uses 0 to mean no register class, and otherwise
00295     // stores RC + 1.
00296     if (!High)
00297       return false;
00298     RC = High - 1;
00299     return true;
00300   }
00301 
00302 };
00303 
00304 } // End llvm namespace
00305 
00306 #endif