1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/InlineAsm.h - Class to represent inline asm strings-*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This class represents the inline asm strings, which are Value*'s that are
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// used as the callee operand of call instructions.  InlineAsm's are uniqued
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// like constants, and created via InlineAsm::get(...).
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_INLINEASM_H
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_INLINEASM_H
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Value.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <vector>
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass PointerType;
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass FunctionType;
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Module;
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct InlineAsmKeyType;
2819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate<class ValType, class ValRefType, class TypeClass, class ConstantClass,
2919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         bool HasLargeKey>
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ConstantUniqueMap;
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class ConstantClass, class TypeClass, class ValType>
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct ConstantCreator;
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass InlineAsm : public Value {
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>;
3619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  friend class ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&,
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 PointerType, InlineAsm, false>;
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InlineAsm(const InlineAsm &);             // do not implement
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void operator=(const InlineAsm&);         // do not implement
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string AsmString, Constraints;
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool HasSideEffects;
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool IsAlignStack;
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InlineAsm(PointerType *Ty, const std::string &AsmString,
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            const std::string &Constraints, bool hasSideEffects,
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            bool isAlignStack);
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual ~InlineAsm();
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// When the ConstantUniqueMap merges two types and makes two InlineAsms
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// identical, it destroys one of them with this method.
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void destroyConstant();
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// InlineAsm::get - Return the specified uniqued inline asm string.
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
5819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static InlineAsm *get(FunctionType *Ty, StringRef AsmString,
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        StringRef Constraints, bool hasSideEffects,
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        bool isAlignStack = false);
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasSideEffects() const { return HasSideEffects; }
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isAlignStack() const { return IsAlignStack; }
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getType - InlineAsm's are always pointers.
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  PointerType *getType() const {
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return reinterpret_cast<PointerType*>(Value::getType());
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getFunctionType - InlineAsm's are always pointers to functions.
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FunctionType *getFunctionType() const;
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const std::string &getAsmString() const { return AsmString; }
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const std::string &getConstraintString() const { return Constraints; }
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Verify - This static method can be used by the parser to check to see if
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the specified constraint string is legal for the type.  This returns true
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// if legal, false if not.
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static bool Verify(FunctionType *Ty, StringRef Constraints);
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Constraint String Parsing
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum ConstraintPrefix {
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isInput,            // 'x'
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isOutput,           // '=x'
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isClobber           // '~x'
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::vector<std::string> ConstraintCodeVector;
9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  struct SubConstraintInfo {
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// MatchingInput - If this is not -1, this is an output constraint where an
9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// input constraint is required to match it (e.g. "0").  The value is the
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// constraint number that matches this one (for example, if this is
9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// constraint #0 and constraint #4 has the value "0", this will be 4).
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    signed char MatchingInput;
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// Code - The constraint code, either the register name (in braces) or the
10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// constraint letter/number.
10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintCodeVector Codes;
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// Default constructor.
10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SubConstraintInfo() : MatchingInput(-1) {}
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  };
10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::vector<SubConstraintInfo> SubConstraintInfoVector;
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  struct ConstraintInfo;
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::vector<ConstraintInfo> ConstraintInfoVector;
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  struct ConstraintInfo {
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// Type - The basic type of the constraint: input/output/clobber
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ///
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstraintPrefix Type;
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// isEarlyClobber - "&": output operand writes result before inputs are all
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// read.  This is only ever set for an output operand.
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool isEarlyClobber;
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// MatchingInput - If this is not -1, this is an output constraint where an
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// input constraint is required to match it (e.g. "0").  The value is the
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// constraint number that matches this one (for example, if this is
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// constraint #0 and constraint #4 has the value "0", this will be 4).
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    signed char MatchingInput;
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// hasMatchingInput - Return true if this is an output constraint that has
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// a matching input constraint.
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool hasMatchingInput() const { return MatchingInput != -1; }
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// isCommutative - This is set to true for a constraint that is commutative
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// with the next operand.
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool isCommutative;
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// isIndirect - True if this operand is an indirect operand.  This means
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// that the address of the source or destination is present in the call
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// instruction, instead of it being returned or passed in explicitly.  This
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// is represented with a '*' in the asm string.
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool isIndirect;
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// Code - The constraint code, either the register name (in braces) or the
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// constraint letter/number.
14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintCodeVector Codes;
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// isMultipleAlternative - '|': has multiple-alternative constraints.
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool isMultipleAlternative;
14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// multipleAlternatives - If there are multiple alternative constraints,
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// this array will contain them.  Otherwise it will be empty.
14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SubConstraintInfoVector multipleAlternatives;
14919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// The currently selected alternative constraint index.
15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned currentAlternativeIndex;
15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ///Default constructor.
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintInfo();
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// Copy constructor.
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintInfo(const ConstraintInfo &other);
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// fields in this structure.  If the constraint string is not understood,
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// return true, otherwise return false.
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// selectAlternative - Point this constraint to the alternative constraint
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    /// indicated by the index.
16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    void selectAlternative(unsigned index);
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ParseConstraints - Split up the constraint string into the specific
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// constraints and their prefixes.  If this returns an empty vector, and if
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the constraint string itself isn't empty, there was an error parsing.
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ParseConstraints - Parse the constraints of this inlineasm object,
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// returning them the same way that ParseConstraints(str) does.
17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstraintInfoVector ParseConstraints() const {
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ParseConstraints(Constraints);
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Methods for support type inquiry through isa, cast, and dyn_cast:
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool classof(const InlineAsm *) { return true; }
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool classof(const Value *V) {
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return V->getValueID() == Value::InlineAsmVal;
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These are helper methods for dealing with flags in the INLINEASM SDNode
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // in the backend.
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum {
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Fixed operands on an INLINEASM SDNode.
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op_InputChain = 0,
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op_AsmString = 1,
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op_MDNode = 2,
19519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Op_ExtraInfo = 3,    // HasSideEffects, IsAlignStack
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op_FirstOperand = 4,
19719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
19819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Fixed operands on an INLINEASM MachineInstr.
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MIOp_AsmString = 0,
20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MIOp_ExtraInfo = 1,    // HasSideEffects, IsAlignStack
20119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MIOp_FirstOperand = 2,
20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Interpretation of the MIOp_ExtraInfo bit field.
20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Extra_HasSideEffects = 1,
20519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Extra_IsAlignStack = 2,
20619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
20719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Inline asm operands map to multiple SDNode / MachineInstr operands.
20819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // The first operand is an immediate describing the asm operand, the low
20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // bits is the kind:
21019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Kind_RegUse = 1,             // Input register, "r".
21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Kind_RegDef = 2,             // Output register, "=r".
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Kind_RegDefEarlyClobber = 3, // Early-clobber output register, "=&r".
21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Kind_Clobber = 4,            // Clobbered register, "~r".
21419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Kind_Imm = 5,                // Immediate.
21519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Kind_Mem = 6,                // Memory operand, "m".
21619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Flag_MatchingOperand = 0x80000000
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(Kind >= Kind_RegUse && Kind <= Kind_Mem && "Invalid Kind");
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Kind | (NumOps << 3);
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getFlagWordForMatchingOp - Augment an existing flag word returned by
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getFlagWord with information indicating that this input operand is tied
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// to a previous output operand.
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           unsigned MatchedOperandNo) {
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(MatchedOperandNo <= 0x7fff && "Too big matched operand");
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// getFlagWordForRegClass - Augment an existing flag word returned by
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// getFlagWord with the required register class for the following register
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// operands.
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// A tied use operand cannot have a register class, use the register class
24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// from the def operand instead.
24119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static unsigned getFlagWordForRegClass(unsigned InputFlag, unsigned RC) {
24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Store RC + 1, reserve the value 0 to mean 'no register class'.
24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ++RC;
24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(RC <= 0x7fff && "Too large register class ID");
24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return InputFlag | (RC << 16);
24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static unsigned getKind(unsigned Flags) {
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Flags & 7;
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool isRegDefEarlyClobberKind(unsigned Flag) {
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getKind(Flag) == Kind_RegDefEarlyClobber;
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static bool isClobberKind(unsigned Flag) {
26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getKind(Flag) == Kind_Clobber;
26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getNumOperandRegisters - Extract the number of registers field from the
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// inline asm operand flag.
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static unsigned getNumOperandRegisters(unsigned Flag) {
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (Flag & 0xffff) >> 3;
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isUseOperandTiedToDef - Return true if the flag of the inline asm
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// operand indicates it is an use operand that's matched to a def operand.
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Flag & Flag_MatchingOperand) == 0)
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Idx = (Flag & ~Flag_MatchingOperand) >> 16;
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// hasRegClassConstraint - Returns true if the flag contains a register
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// class constraint.  Sets RC to the register class ID.
28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static bool hasRegClassConstraint(unsigned Flag, unsigned &RC) {
28119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Flag & Flag_MatchingOperand)
28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned High = Flag >> 16;
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // getFlagWordForRegClass() uses 0 to mean no register class, and otherwise
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // stores RC + 1.
28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!High)
28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    RC = High - 1;
28919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
29019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
297