Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 1 | //===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===// |
| 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 tablegen backend emits a target specifier matcher for converting parsed |
| 11 | // assembly operands in the MCInst structures. |
| 12 | // |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 13 | // The input to the target specific matcher is a list of literal tokens and |
| 14 | // operands. The target specific parser should generally eliminate any syntax |
| 15 | // which is not relevant for matching; for example, comma tokens should have |
| 16 | // already been consumed and eliminated by the parser. Most instructions will |
| 17 | // end up with a single literal token (the instruction name) and some number of |
| 18 | // operands. |
| 19 | // |
| 20 | // Some example inputs, for X86: |
| 21 | // 'addl' (immediate ...) (register ...) |
| 22 | // 'add' (immediate ...) (memory ...) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 23 | // 'call' '*' %epc |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 24 | // |
| 25 | // The assembly matcher is responsible for converting this input into a precise |
| 26 | // machine instruction (i.e., an instruction with a well defined encoding). This |
| 27 | // mapping has several properties which complicate matching: |
| 28 | // |
| 29 | // - It may be ambiguous; many architectures can legally encode particular |
| 30 | // variants of an instruction in different ways (for example, using a smaller |
| 31 | // encoding for small immediates). Such ambiguities should never be |
| 32 | // arbitrarily resolved by the assembler, the assembler is always responsible |
| 33 | // for choosing the "best" available instruction. |
| 34 | // |
| 35 | // - It may depend on the subtarget or the assembler context. Instructions |
| 36 | // which are invalid for the current mode, but otherwise unambiguous (e.g., |
| 37 | // an SSE instruction in a file being assembled for i486) should be accepted |
| 38 | // and rejected by the assembler front end. However, if the proper encoding |
| 39 | // for an instruction is dependent on the assembler context then the matcher |
| 40 | // is responsible for selecting the correct machine instruction for the |
| 41 | // current mode. |
| 42 | // |
| 43 | // The core matching algorithm attempts to exploit the regularity in most |
| 44 | // instruction sets to quickly determine the set of possibly matching |
| 45 | // instructions, and the simplify the generated code. Additionally, this helps |
| 46 | // to ensure that the ambiguities are intentionally resolved by the user. |
| 47 | // |
| 48 | // The matching is divided into two distinct phases: |
| 49 | // |
| 50 | // 1. Classification: Each operand is mapped to the unique set which (a) |
| 51 | // contains it, and (b) is the largest such subset for which a single |
| 52 | // instruction could match all members. |
| 53 | // |
| 54 | // For register classes, we can generate these subgroups automatically. For |
| 55 | // arbitrary operands, we expect the user to define the classes and their |
| 56 | // relations to one another (for example, 8-bit signed immediates as a |
| 57 | // subset of 32-bit immediates). |
| 58 | // |
| 59 | // By partitioning the operands in this way, we guarantee that for any |
| 60 | // tuple of classes, any single instruction must match either all or none |
| 61 | // of the sets of operands which could classify to that tuple. |
| 62 | // |
| 63 | // In addition, the subset relation amongst classes induces a partial order |
| 64 | // on such tuples, which we use to resolve ambiguities. |
| 65 | // |
| 66 | // FIXME: What do we do if a crazy case shows up where this is the wrong |
| 67 | // resolution? |
| 68 | // |
| 69 | // 2. The input can now be treated as a tuple of classes (static tokens are |
| 70 | // simple singleton sets). Each such tuple should generally map to a single |
| 71 | // instruction (we currently ignore cases where this isn't true, whee!!!), |
| 72 | // which we can emit a simple matcher for. |
| 73 | // |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | |
| 76 | #include "AsmMatcherEmitter.h" |
| 77 | #include "CodeGenTarget.h" |
| 78 | #include "Record.h" |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 79 | #include "StringMatcher.h" |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 80 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 81 | #include "llvm/ADT/SmallPtrSet.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 82 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/StringExtras.h" |
| 85 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 86 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 87 | #include <list> |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 88 | #include <map> |
| 89 | #include <set> |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 90 | using namespace llvm; |
| 91 | |
Daniel Dunbar | 2724915 | 2009-08-07 20:33:39 +0000 | [diff] [blame] | 92 | static cl::opt<std::string> |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 93 | MatchPrefix("match-prefix", cl::init(""), |
| 94 | cl::desc("Only match instructions with the given prefix")); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 96 | |
| 97 | namespace { |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 98 | class AsmMatcherInfo; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 99 | struct SubtargetFeatureInfo; |
| 100 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 101 | /// ClassInfo - Helper class for storing the information about a particular |
| 102 | /// class of operands which can be matched. |
| 103 | struct ClassInfo { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 104 | enum ClassInfoKind { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 105 | /// Invalid kind, for use as a sentinel value. |
| 106 | Invalid = 0, |
| 107 | |
| 108 | /// The class for a particular token. |
| 109 | Token, |
| 110 | |
| 111 | /// The (first) register class, subsequent register classes are |
| 112 | /// RegisterClass0+1, and so on. |
| 113 | RegisterClass0, |
| 114 | |
| 115 | /// The (first) user defined class, subsequent user defined classes are |
| 116 | /// UserClass0+1, and so on. |
| 117 | UserClass0 = 1<<16 |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /// Kind - The class kind, which is either a predefined kind, or (UserClass0 + |
| 121 | /// N) for the Nth user defined class. |
| 122 | unsigned Kind; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 124 | /// SuperClasses - The super classes of this class. Note that for simplicities |
| 125 | /// sake user operands only record their immediate super class, while register |
| 126 | /// operands include all superclasses. |
| 127 | std::vector<ClassInfo*> SuperClasses; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 129 | /// Name - The full class name, suitable for use in an enum. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 130 | std::string Name; |
| 131 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 132 | /// ClassName - The unadorned generic name for this class (e.g., Token). |
| 133 | std::string ClassName; |
| 134 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 135 | /// ValueName - The name of the value this class represents; for a token this |
| 136 | /// is the literal token string, for an operand it is the TableGen class (or |
| 137 | /// empty if this is a derived class). |
| 138 | std::string ValueName; |
| 139 | |
| 140 | /// PredicateMethod - The name of the operand method to test whether the |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 141 | /// operand matches this class; this is not valid for Token or register kinds. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 142 | std::string PredicateMethod; |
| 143 | |
| 144 | /// RenderMethod - The name of the operand method to add this operand to an |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 145 | /// MCInst; this is not valid for Token or register kinds. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 146 | std::string RenderMethod; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 147 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 148 | /// For register classes, the records for all the registers in this class. |
| 149 | std::set<Record*> Registers; |
| 150 | |
| 151 | public: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 152 | /// isRegisterClass() - Check if this is a register class. |
| 153 | bool isRegisterClass() const { |
| 154 | return Kind >= RegisterClass0 && Kind < UserClass0; |
| 155 | } |
| 156 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 157 | /// isUserClass() - Check if this is a user defined class. |
| 158 | bool isUserClass() const { |
| 159 | return Kind >= UserClass0; |
| 160 | } |
| 161 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 162 | /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes |
| 163 | /// are related if they are in the same class hierarchy. |
| 164 | bool isRelatedTo(const ClassInfo &RHS) const { |
| 165 | // Tokens are only related to tokens. |
| 166 | if (Kind == Token || RHS.Kind == Token) |
| 167 | return Kind == Token && RHS.Kind == Token; |
| 168 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 169 | // Registers classes are only related to registers classes, and only if |
| 170 | // their intersection is non-empty. |
| 171 | if (isRegisterClass() || RHS.isRegisterClass()) { |
| 172 | if (!isRegisterClass() || !RHS.isRegisterClass()) |
| 173 | return false; |
| 174 | |
| 175 | std::set<Record*> Tmp; |
| 176 | std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 177 | std::set_intersection(Registers.begin(), Registers.end(), |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 178 | RHS.Registers.begin(), RHS.Registers.end(), |
| 179 | II); |
| 180 | |
| 181 | return !Tmp.empty(); |
| 182 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 183 | |
| 184 | // Otherwise we have two users operands; they are related if they are in the |
| 185 | // same class hierarchy. |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 186 | // |
| 187 | // FIXME: This is an oversimplification, they should only be related if they |
| 188 | // intersect, however we don't have that information. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 189 | assert(isUserClass() && RHS.isUserClass() && "Unexpected class!"); |
| 190 | const ClassInfo *Root = this; |
| 191 | while (!Root->SuperClasses.empty()) |
| 192 | Root = Root->SuperClasses.front(); |
| 193 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 194 | const ClassInfo *RHSRoot = &RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 195 | while (!RHSRoot->SuperClasses.empty()) |
| 196 | RHSRoot = RHSRoot->SuperClasses.front(); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 197 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 198 | return Root == RHSRoot; |
| 199 | } |
| 200 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 201 | /// isSubsetOf - Test whether this class is a subset of \arg RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 202 | bool isSubsetOf(const ClassInfo &RHS) const { |
| 203 | // This is a subset of RHS if it is the same class... |
| 204 | if (this == &RHS) |
| 205 | return true; |
| 206 | |
| 207 | // ... or if any of its super classes are a subset of RHS. |
| 208 | for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(), |
| 209 | ie = SuperClasses.end(); it != ie; ++it) |
| 210 | if ((*it)->isSubsetOf(RHS)) |
| 211 | return true; |
| 212 | |
| 213 | return false; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 216 | /// operator< - Compare two classes. |
| 217 | bool operator<(const ClassInfo &RHS) const { |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 218 | if (this == &RHS) |
| 219 | return false; |
| 220 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 221 | // Unrelated classes can be ordered by kind. |
| 222 | if (!isRelatedTo(RHS)) |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 223 | return Kind < RHS.Kind; |
| 224 | |
| 225 | switch (Kind) { |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 226 | case Invalid: |
| 227 | assert(0 && "Invalid kind!"); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 228 | case Token: |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 229 | // Tokens are comparable by value. |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 230 | // |
| 231 | // FIXME: Compare by enum value. |
| 232 | return ValueName < RHS.ValueName; |
| 233 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 234 | default: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 235 | // This class preceeds the RHS if it is a proper subset of the RHS. |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 236 | if (isSubsetOf(RHS)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 237 | return true; |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 238 | if (RHS.isSubsetOf(*this)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 239 | return false; |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 240 | |
| 241 | // Otherwise, order by name to ensure we have a total ordering. |
| 242 | return ValueName < RHS.ValueName; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 247 | /// MatchableInfo - Helper class for storing the necessary information for an |
| 248 | /// instruction or alias which is capable of being matched. |
| 249 | struct MatchableInfo { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 250 | struct Operand { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 251 | /// Token - This is the token that the operand came from. |
| 252 | StringRef Token; |
| 253 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 254 | /// The unique class instance this operand should match. |
| 255 | ClassInfo *Class; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 256 | |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 257 | /// The original operand this corresponds to. This is unset for singleton |
| 258 | /// registers and tokens, because they don't have a list in the ins/outs |
| 259 | /// list. If an operand is tied ($a=$b), this refers to source operand: $b. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 260 | const CGIOperandList::OperandInfo *OperandInfo; |
Chris Lattner | 4c9f4e4 | 2010-11-01 23:08:02 +0000 | [diff] [blame] | 261 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 262 | explicit Operand(StringRef T) : Token(T), Class(0), OperandInfo(0) {} |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | /// InstrName - The target name for this instruction. |
| 266 | std::string InstrName; |
| 267 | |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 268 | /// TheDef - This is the definition of the instruction or InstAlias that this |
| 269 | /// matchable came from. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 270 | Record *const TheDef; |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 271 | |
| 272 | /// OperandList - This is the operand list that came from the (ins) and (outs) |
| 273 | /// list of the alias or instruction. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 274 | const CGIOperandList &OperandList; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 275 | |
| 276 | /// AsmString - The assembly string for this instruction (with variants |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 277 | /// removed), e.g. "movsx $src, $dst". |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 278 | std::string AsmString; |
| 279 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 280 | /// Mnemonic - This is the first token of the matched instruction, its |
| 281 | /// mnemonic. |
| 282 | StringRef Mnemonic; |
| 283 | |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 284 | /// AsmOperands - The textual operands that this instruction matches, |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 285 | /// annotated with a class and where in the OperandList they were defined. |
| 286 | /// This directly corresponds to the tokenized AsmString after the mnemonic is |
| 287 | /// removed. |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 288 | SmallVector<Operand, 4> AsmOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 289 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 290 | /// Predicates - The required subtarget features to match this instruction. |
| 291 | SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures; |
| 292 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 293 | /// ConversionFnKind - The enum value which is passed to the generated |
| 294 | /// ConvertToMCInst to convert parsed operands into an MCInst for this |
| 295 | /// function. |
| 296 | std::string ConversionFnKind; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 298 | MatchableInfo(const CodeGenInstruction &CGI) |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 299 | : TheDef(CGI.TheDef), OperandList(CGI.Operands), AsmString(CGI.AsmString) { |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 300 | InstrName = TheDef->getName(); |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 303 | MatchableInfo(const CodeGenInstAlias *Alias) |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 304 | : TheDef(Alias->TheDef), OperandList(Alias->Operands), |
| 305 | AsmString(Alias->AsmString) { |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 306 | |
| 307 | // FIXME: Huge hack. |
| 308 | DefInit *DI = dynamic_cast<DefInit*>(Alias->Result->getOperator()); |
| 309 | assert(DI); |
| 310 | |
| 311 | InstrName = DI->getDef()->getName(); |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void Initialize(const AsmMatcherInfo &Info, |
| 315 | SmallPtrSet<Record*, 16> &SingletonRegisters); |
| 316 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 317 | /// Validate - Return true if this matchable is a valid thing to match against |
| 318 | /// and perform a bunch of validity checking. |
| 319 | bool Validate(StringRef CommentDelimiter, bool Hack) const; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 320 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 321 | /// getSingletonRegisterForAsmOperand - If the specified token is a singleton |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 322 | /// register, return the Record for it, otherwise return null. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 323 | Record *getSingletonRegisterForAsmOperand(unsigned i, |
| 324 | const AsmMatcherInfo &Info) const; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 326 | /// operator< - Compare two matchables. |
| 327 | bool operator<(const MatchableInfo &RHS) const { |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 328 | // The primary comparator is the instruction mnemonic. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 329 | if (Mnemonic != RHS.Mnemonic) |
| 330 | return Mnemonic < RHS.Mnemonic; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 332 | if (AsmOperands.size() != RHS.AsmOperands.size()) |
| 333 | return AsmOperands.size() < RHS.AsmOperands.size(); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 334 | |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 335 | // Compare lexicographically by operand. The matcher validates that other |
| 336 | // orderings wouldn't be ambiguous using \see CouldMatchAmiguouslyWith(). |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 337 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 338 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class) |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 339 | return true; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 340 | if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 347 | /// CouldMatchAmiguouslyWith - Check whether this matchable could |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 348 | /// ambiguously match the same set of operands as \arg RHS (without being a |
| 349 | /// strictly superior match). |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 350 | bool CouldMatchAmiguouslyWith(const MatchableInfo &RHS) { |
Chris Lattner | e66b7eb | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 351 | // The primary comparator is the instruction mnemonic. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 352 | if (Mnemonic != RHS.Mnemonic) |
Chris Lattner | e66b7eb | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 353 | return false; |
| 354 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 355 | // The number of operands is unambiguous. |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 356 | if (AsmOperands.size() != RHS.AsmOperands.size()) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 357 | return false; |
| 358 | |
Daniel Dunbar | 1402f0b | 2010-01-23 00:26:16 +0000 | [diff] [blame] | 359 | // Otherwise, make sure the ordering of the two instructions is unambiguous |
| 360 | // by checking that either (a) a token or operand kind discriminates them, |
| 361 | // or (b) the ordering among equivalent kinds is consistent. |
| 362 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 363 | // Tokens and operand kinds are unambiguous (assuming a correct target |
| 364 | // specific parser). |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 365 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) |
| 366 | if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind || |
| 367 | AsmOperands[i].Class->Kind == ClassInfo::Token) |
| 368 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class || |
| 369 | *RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 370 | return false; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 372 | // Otherwise, this operand could commute if all operands are equivalent, or |
| 373 | // there is a pair of operands that compare less than and a pair that |
| 374 | // compare greater than. |
| 375 | bool HasLT = false, HasGT = false; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 376 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 377 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 378 | HasLT = true; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 379 | if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 380 | HasGT = true; |
| 381 | } |
| 382 | |
| 383 | return !(HasLT ^ HasGT); |
| 384 | } |
| 385 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 386 | void dump(); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 387 | |
| 388 | private: |
| 389 | void TokenizeAsmString(const AsmMatcherInfo &Info); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 392 | /// SubtargetFeatureInfo - Helper class for storing information on a subtarget |
| 393 | /// feature which participates in instruction matching. |
| 394 | struct SubtargetFeatureInfo { |
| 395 | /// \brief The predicate record for this feature. |
| 396 | Record *TheDef; |
| 397 | |
| 398 | /// \brief An unique index assigned to represent this feature. |
| 399 | unsigned Index; |
| 400 | |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 401 | SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {} |
| 402 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 403 | /// \brief The name of the enumerated constant identifying this feature. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 404 | std::string getEnumName() const { |
| 405 | return "Feature_" + TheDef->getName(); |
| 406 | } |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 409 | class AsmMatcherInfo { |
| 410 | public: |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 411 | /// The tablegen AsmParser record. |
| 412 | Record *AsmParser; |
| 413 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 414 | /// Target - The target information. |
| 415 | CodeGenTarget &Target; |
| 416 | |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 417 | /// The AsmParser "RegisterPrefix" value. |
| 418 | std::string RegisterPrefix; |
| 419 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 420 | /// The classes which are needed for matching. |
| 421 | std::vector<ClassInfo*> Classes; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 423 | /// The information on the matchables to match. |
| 424 | std::vector<MatchableInfo*> Matchables; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 425 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 426 | /// Map of Register records to their class information. |
| 427 | std::map<Record*, ClassInfo*> RegisterClasses; |
| 428 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 429 | /// Map of Predicate records to their subtarget information. |
| 430 | std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures; |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 431 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 432 | private: |
| 433 | /// Map of token to class information which has already been constructed. |
| 434 | std::map<std::string, ClassInfo*> TokenClasses; |
| 435 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 436 | /// Map of RegisterClass records to their class information. |
| 437 | std::map<Record*, ClassInfo*> RegisterClassClasses; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 438 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 439 | /// Map of AsmOperandClass records to their class information. |
| 440 | std::map<Record*, ClassInfo*> AsmOperandClasses; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 442 | private: |
| 443 | /// getTokenClass - Lookup or create the class for the given token. |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 444 | ClassInfo *getTokenClass(StringRef Token); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 445 | |
| 446 | /// getOperandClass - Lookup or create the class for the given operand. |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 447 | ClassInfo *getOperandClass(StringRef Token, |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 448 | const CGIOperandList::OperandInfo &OI); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 449 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 450 | /// BuildRegisterClasses - Build the ClassInfo* instances for register |
| 451 | /// classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 452 | void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 453 | |
| 454 | /// BuildOperandClasses - Build the ClassInfo* instances for user defined |
| 455 | /// operand classes. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 456 | void BuildOperandClasses(); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 457 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 458 | public: |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 459 | AsmMatcherInfo(Record *AsmParser, CodeGenTarget &Target); |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 460 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 461 | /// BuildInfo - Construct the various tables used during matching. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 462 | void BuildInfo(); |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 463 | |
| 464 | /// getSubtargetFeature - Lookup or create the subtarget feature info for the |
| 465 | /// given operand. |
| 466 | SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const { |
| 467 | assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!"); |
| 468 | std::map<Record*, SubtargetFeatureInfo*>::const_iterator I = |
| 469 | SubtargetFeatures.find(Def); |
| 470 | return I == SubtargetFeatures.end() ? 0 : I->second; |
| 471 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 472 | }; |
| 473 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 476 | void MatchableInfo::dump() { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 477 | errs() << InstrName << " -- " << "flattened:\"" << AsmString << "\"\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 479 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 480 | Operand &Op = AsmOperands[i]; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 481 | errs() << " op[" << i << "] = " << Op.Class->ClassName << " - "; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 482 | if (Op.Class->Kind == ClassInfo::Token) { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 483 | errs() << '\"' << Op.Token << "\"\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 484 | continue; |
| 485 | } |
| 486 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 487 | if (!Op.OperandInfo) { |
| 488 | errs() << "(singleton register)\n"; |
| 489 | continue; |
| 490 | } |
| 491 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 492 | const CGIOperandList::OperandInfo &OI = *Op.OperandInfo; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 493 | errs() << OI.Name << " " << OI.Rec->getName() |
| 494 | << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n"; |
| 495 | } |
| 496 | } |
| 497 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 498 | void MatchableInfo::Initialize(const AsmMatcherInfo &Info, |
| 499 | SmallPtrSet<Record*, 16> &SingletonRegisters) { |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 500 | // TODO: Eventually support asmparser for Variant != 0. |
| 501 | AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0); |
| 502 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 503 | TokenizeAsmString(Info); |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 504 | |
| 505 | // Compute the require features. |
| 506 | std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates"); |
| 507 | for (unsigned i = 0, e = Predicates.size(); i != e; ++i) |
| 508 | if (SubtargetFeatureInfo *Feature = |
| 509 | Info.getSubtargetFeature(Predicates[i])) |
| 510 | RequiredFeatures.push_back(Feature); |
| 511 | |
| 512 | // Collect singleton registers, if used. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 513 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 514 | if (Record *Reg = getSingletonRegisterForAsmOperand(i, Info)) |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 515 | SingletonRegisters.insert(Reg); |
| 516 | } |
| 517 | } |
| 518 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 519 | /// TokenizeAsmString - Tokenize a simplified assembly string. |
| 520 | void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) { |
| 521 | StringRef String = AsmString; |
| 522 | unsigned Prev = 0; |
| 523 | bool InTok = true; |
| 524 | for (unsigned i = 0, e = String.size(); i != e; ++i) { |
| 525 | switch (String[i]) { |
| 526 | case '[': |
| 527 | case ']': |
| 528 | case '*': |
| 529 | case '!': |
| 530 | case ' ': |
| 531 | case '\t': |
| 532 | case ',': |
| 533 | if (InTok) { |
| 534 | AsmOperands.push_back(Operand(String.slice(Prev, i))); |
| 535 | InTok = false; |
| 536 | } |
| 537 | if (!isspace(String[i]) && String[i] != ',') |
| 538 | AsmOperands.push_back(Operand(String.substr(i, 1))); |
| 539 | Prev = i + 1; |
| 540 | break; |
| 541 | |
| 542 | case '\\': |
| 543 | if (InTok) { |
| 544 | AsmOperands.push_back(Operand(String.slice(Prev, i))); |
| 545 | InTok = false; |
| 546 | } |
| 547 | ++i; |
| 548 | assert(i != String.size() && "Invalid quoted character"); |
| 549 | AsmOperands.push_back(Operand(String.substr(i, 1))); |
| 550 | Prev = i + 1; |
| 551 | break; |
| 552 | |
| 553 | case '$': { |
| 554 | // If this isn't "${", treat like a normal token. |
| 555 | if (i + 1 == String.size() || String[i + 1] != '{') { |
| 556 | if (InTok) { |
| 557 | AsmOperands.push_back(Operand(String.slice(Prev, i))); |
| 558 | InTok = false; |
| 559 | } |
| 560 | Prev = i; |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | if (InTok) { |
| 565 | AsmOperands.push_back(Operand(String.slice(Prev, i))); |
| 566 | InTok = false; |
| 567 | } |
| 568 | |
| 569 | StringRef::iterator End = std::find(String.begin() + i, String.end(),'}'); |
| 570 | assert(End != String.end() && "Missing brace in operand reference!"); |
| 571 | size_t EndPos = End - String.begin(); |
| 572 | AsmOperands.push_back(Operand(String.slice(i, EndPos+1))); |
| 573 | Prev = EndPos + 1; |
| 574 | i = EndPos; |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | case '.': |
| 579 | if (InTok) |
| 580 | AsmOperands.push_back(Operand(String.slice(Prev, i))); |
| 581 | Prev = i; |
| 582 | InTok = true; |
| 583 | break; |
| 584 | |
| 585 | default: |
| 586 | InTok = true; |
| 587 | } |
| 588 | } |
| 589 | if (InTok && Prev != String.size()) |
| 590 | AsmOperands.push_back(Operand(String.substr(Prev))); |
| 591 | |
| 592 | // The first token of the instruction is the mnemonic, which must be a |
| 593 | // simple string, not a $foo variable or a singleton register. |
| 594 | assert(!AsmOperands.empty() && "Instruction has no tokens?"); |
| 595 | Mnemonic = AsmOperands[0].Token; |
| 596 | if (Mnemonic[0] == '$' || getSingletonRegisterForAsmOperand(0, Info)) |
| 597 | throw TGError(TheDef->getLoc(), |
| 598 | "Invalid instruction mnemonic '" + Mnemonic.str() + "'!"); |
| 599 | |
| 600 | // Remove the first operand, it is tracked in the mnemonic field. |
| 601 | AsmOperands.erase(AsmOperands.begin()); |
| 602 | } |
| 603 | |
| 604 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 606 | bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const { |
| 607 | // Reject matchables with no .s string. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 608 | if (AsmString.empty()) |
| 609 | throw TGError(TheDef->getLoc(), "instruction with empty asm string"); |
| 610 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 611 | // Reject any matchables with a newline in them, they should be marked |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 612 | // isCodeGenOnly if they are pseudo instructions. |
| 613 | if (AsmString.find('\n') != std::string::npos) |
| 614 | throw TGError(TheDef->getLoc(), |
| 615 | "multiline instruction is not valid for the asmparser, " |
| 616 | "mark it isCodeGenOnly"); |
| 617 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 618 | // Remove comments from the asm string. We know that the asmstring only |
| 619 | // has one line. |
| 620 | if (!CommentDelimiter.empty() && |
| 621 | StringRef(AsmString).find(CommentDelimiter) != StringRef::npos) |
| 622 | throw TGError(TheDef->getLoc(), |
| 623 | "asmstring for instruction has comment character in it, " |
| 624 | "mark it isCodeGenOnly"); |
| 625 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 626 | // Reject matchables with operand modifiers, these aren't something we can |
| 627 | /// handle, the target should be refactored to use operands instead of |
| 628 | /// modifiers. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 629 | // |
| 630 | // Also, check for instructions which reference the operand multiple times; |
| 631 | // this implies a constraint we would not honor. |
| 632 | std::set<std::string> OperandNames; |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 633 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 634 | StringRef Tok = AsmOperands[i].Token; |
| 635 | if (Tok[0] == '$' && Tok.find(':') != StringRef::npos) |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 636 | throw TGError(TheDef->getLoc(), |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 637 | "matchable with operand modifier '" + Tok.str() + |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 638 | "' not supported by asm matcher. Mark isCodeGenOnly!"); |
| 639 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 640 | // Verify that any operand is only mentioned once. |
Chris Lattner | d51257a | 2010-11-02 23:18:43 +0000 | [diff] [blame^] | 641 | // We reject aliases and ignore instructions for now. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 642 | if (Tok[0] == '$' && !OperandNames.insert(Tok).second) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 643 | if (!Hack) |
| 644 | throw TGError(TheDef->getLoc(), |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 645 | "ERROR: matchable with tied operand '" + Tok.str() + |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 646 | "' can never be matched!"); |
| 647 | // FIXME: Should reject these. The ARM backend hits this with $lane in a |
| 648 | // bunch of instructions. It is unclear what the right answer is. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 649 | DEBUG({ |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 650 | errs() << "warning: '" << InstrName << "': " |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 651 | << "ignoring instruction with tied operand '" |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 652 | << Tok.str() << "'\n"; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 653 | }); |
| 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | |
Chris Lattner | d51257a | 2010-11-02 23:18:43 +0000 | [diff] [blame^] | 658 | // Validate the operand list to ensure we can handle this instruction. |
| 659 | for (unsigned i = 0, e = OperandList.size(); i != e; ++i) { |
| 660 | const CGIOperandList::OperandInfo &OI = OperandList[i]; |
| 661 | |
| 662 | // Validate tied operands. |
| 663 | if (OI.getTiedRegister() != -1) { |
| 664 | // If we have a tied operand that consists of multiple MCOperands, reject |
| 665 | // it. We reject aliases and ignore instructions for now. |
| 666 | if (OI.MINumOperands != 1) { |
| 667 | if (!Hack) |
| 668 | throw TGError(TheDef->getLoc(), |
| 669 | "ERROR: tied operand '" + OI.Name + |
| 670 | "' has multiple MCOperands!"); |
| 671 | |
| 672 | // FIXME: Should reject these. The ARM backend hits this with $lane in a |
| 673 | // bunch of instructions. It is unclear what the right answer is. |
| 674 | DEBUG({ |
| 675 | errs() << "warning: '" << InstrName << "': " |
| 676 | << "ignoring instruction with multi-operand tied operand '" |
| 677 | << OI.Name << "'\n"; |
| 678 | }); |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | |
| 688 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 689 | /// getSingletonRegisterForAsmOperand - If the specified token is a singleton |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 690 | /// register, return the register name, otherwise return a null StringRef. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 691 | Record *MatchableInfo:: |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 692 | getSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info) const{ |
| 693 | StringRef Tok = AsmOperands[i].Token; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 694 | if (!Tok.startswith(Info.RegisterPrefix)) |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 695 | return 0; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 696 | |
| 697 | StringRef RegName = Tok.substr(Info.RegisterPrefix.size()); |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 698 | if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName)) |
| 699 | return Reg->TheDef; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 701 | // If there is no register prefix (i.e. "%" in "%eax"), then this may |
| 702 | // be some random non-register token, just ignore it. |
| 703 | if (Info.RegisterPrefix.empty()) |
| 704 | return 0; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 705 | |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 706 | // Otherwise, we have something invalid prefixed with the register prefix, |
| 707 | // such as %foo. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 708 | std::string Err = "unable to find register for '" + RegName.str() + |
| 709 | "' (which matches register prefix)"; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 710 | throw TGError(TheDef->getLoc(), Err); |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 714 | static std::string getEnumNameForToken(StringRef Str) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 715 | std::string Res; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 716 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 717 | for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) { |
| 718 | switch (*it) { |
| 719 | case '*': Res += "_STAR_"; break; |
| 720 | case '%': Res += "_PCT_"; break; |
| 721 | case ':': Res += "_COLON_"; break; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 722 | default: |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 723 | if (isalnum(*it)) |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 724 | Res += *it; |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 725 | else |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 726 | Res += "_" + utostr((unsigned) *it) + "_"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | return Res; |
| 731 | } |
| 732 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 733 | ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 734 | ClassInfo *&Entry = TokenClasses[Token]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 735 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 736 | if (!Entry) { |
| 737 | Entry = new ClassInfo(); |
| 738 | Entry->Kind = ClassInfo::Token; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 739 | Entry->ClassName = "Token"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 740 | Entry->Name = "MCK_" + getEnumNameForToken(Token); |
| 741 | Entry->ValueName = Token; |
| 742 | Entry->PredicateMethod = "<invalid>"; |
| 743 | Entry->RenderMethod = "<invalid>"; |
| 744 | Classes.push_back(Entry); |
| 745 | } |
| 746 | |
| 747 | return Entry; |
| 748 | } |
| 749 | |
| 750 | ClassInfo * |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 751 | AsmMatcherInfo::getOperandClass(StringRef Token, |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 752 | const CGIOperandList::OperandInfo &OI) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 753 | if (OI.Rec->isSubClassOf("RegisterClass")) { |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 754 | if (ClassInfo *CI = RegisterClassClasses[OI.Rec]) |
| 755 | return CI; |
| 756 | throw TGError(OI.Rec->getLoc(), "register class has no class info!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 757 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 758 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 759 | assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!"); |
| 760 | Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass"); |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 761 | if (ClassInfo *CI = AsmOperandClasses[MatchClass]) |
| 762 | return CI; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 763 | |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 764 | throw TGError(OI.Rec->getLoc(), "operand has no match class!"); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 767 | void AsmMatcherInfo:: |
| 768 | BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) { |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 769 | const std::vector<CodeGenRegister> &Registers = Target.getRegisters(); |
| 770 | const std::vector<CodeGenRegisterClass> &RegClassList = |
| 771 | Target.getRegisterClasses(); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 772 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 773 | // The register sets used for matching. |
| 774 | std::set< std::set<Record*> > RegisterSets; |
| 775 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 776 | // Gather the defined sets. |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 777 | for (std::vector<CodeGenRegisterClass>::const_iterator it = |
| 778 | RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 779 | RegisterSets.insert(std::set<Record*>(it->Elements.begin(), |
| 780 | it->Elements.end())); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 781 | |
| 782 | // Add any required singleton sets. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 783 | for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(), |
| 784 | ie = SingletonRegisters.end(); it != ie; ++it) { |
| 785 | Record *Rec = *it; |
| 786 | RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1)); |
| 787 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 788 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 789 | // Introduce derived sets where necessary (when a register does not determine |
| 790 | // a unique register set class), and build the mapping of registers to the set |
| 791 | // they should classify to. |
| 792 | std::map<Record*, std::set<Record*> > RegisterMap; |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 793 | for (std::vector<CodeGenRegister>::const_iterator it = Registers.begin(), |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 794 | ie = Registers.end(); it != ie; ++it) { |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 795 | const CodeGenRegister &CGR = *it; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 796 | // Compute the intersection of all sets containing this register. |
| 797 | std::set<Record*> ContainingSet; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 798 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 799 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 800 | ie = RegisterSets.end(); it != ie; ++it) { |
| 801 | if (!it->count(CGR.TheDef)) |
| 802 | continue; |
| 803 | |
| 804 | if (ContainingSet.empty()) { |
| 805 | ContainingSet = *it; |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 806 | continue; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 807 | } |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 808 | |
| 809 | std::set<Record*> Tmp; |
| 810 | std::swap(Tmp, ContainingSet); |
| 811 | std::insert_iterator< std::set<Record*> > II(ContainingSet, |
| 812 | ContainingSet.begin()); |
| 813 | std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | if (!ContainingSet.empty()) { |
| 817 | RegisterSets.insert(ContainingSet); |
| 818 | RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet)); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // Construct the register classes. |
| 823 | std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses; |
| 824 | unsigned Index = 0; |
| 825 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 826 | ie = RegisterSets.end(); it != ie; ++it, ++Index) { |
| 827 | ClassInfo *CI = new ClassInfo(); |
| 828 | CI->Kind = ClassInfo::RegisterClass0 + Index; |
| 829 | CI->ClassName = "Reg" + utostr(Index); |
| 830 | CI->Name = "MCK_Reg" + utostr(Index); |
| 831 | CI->ValueName = ""; |
| 832 | CI->PredicateMethod = ""; // unused |
| 833 | CI->RenderMethod = "addRegOperands"; |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 834 | CI->Registers = *it; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 835 | Classes.push_back(CI); |
| 836 | RegisterSetClasses.insert(std::make_pair(*it, CI)); |
| 837 | } |
| 838 | |
| 839 | // Find the superclasses; we could compute only the subgroup lattice edges, |
| 840 | // but there isn't really a point. |
| 841 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 842 | ie = RegisterSets.end(); it != ie; ++it) { |
| 843 | ClassInfo *CI = RegisterSetClasses[*it]; |
| 844 | for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(), |
| 845 | ie2 = RegisterSets.end(); it2 != ie2; ++it2) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 846 | if (*it != *it2 && |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 847 | std::includes(it2->begin(), it2->end(), it->begin(), it->end())) |
| 848 | CI->SuperClasses.push_back(RegisterSetClasses[*it2]); |
| 849 | } |
| 850 | |
| 851 | // Name the register classes which correspond to a user defined RegisterClass. |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 852 | for (std::vector<CodeGenRegisterClass>::const_iterator |
| 853 | it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 854 | ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->Elements.begin(), |
| 855 | it->Elements.end())]; |
| 856 | if (CI->ValueName.empty()) { |
| 857 | CI->ClassName = it->getName(); |
| 858 | CI->Name = "MCK_" + it->getName(); |
| 859 | CI->ValueName = it->getName(); |
| 860 | } else |
| 861 | CI->ValueName = CI->ValueName + "," + it->getName(); |
| 862 | |
| 863 | RegisterClassClasses.insert(std::make_pair(it->TheDef, CI)); |
| 864 | } |
| 865 | |
| 866 | // Populate the map for individual registers. |
| 867 | for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(), |
| 868 | ie = RegisterMap.end(); it != ie; ++it) |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 869 | RegisterClasses[it->first] = RegisterSetClasses[it->second]; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 870 | |
| 871 | // Name the register classes which correspond to singleton registers. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 872 | for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(), |
| 873 | ie = SingletonRegisters.end(); it != ie; ++it) { |
| 874 | Record *Rec = *it; |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 875 | ClassInfo *CI = RegisterClasses[Rec]; |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 876 | assert(CI && "Missing singleton register class info!"); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 878 | if (CI->ValueName.empty()) { |
| 879 | CI->ClassName = Rec->getName(); |
| 880 | CI->Name = "MCK_" + Rec->getName(); |
| 881 | CI->ValueName = Rec->getName(); |
| 882 | } else |
| 883 | CI->ValueName = CI->ValueName + "," + Rec->getName(); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 884 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 887 | void AsmMatcherInfo::BuildOperandClasses() { |
Chris Lattner | e66b7eb | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 888 | std::vector<Record*> AsmOperands = |
| 889 | Records.getAllDerivedDefinitions("AsmOperandClass"); |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 890 | |
| 891 | // Pre-populate AsmOperandClasses map. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 892 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 893 | ie = AsmOperands.end(); it != ie; ++it) |
| 894 | AsmOperandClasses[*it] = new ClassInfo(); |
| 895 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 896 | unsigned Index = 0; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 897 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 898 | ie = AsmOperands.end(); it != ie; ++it, ++Index) { |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 899 | ClassInfo *CI = AsmOperandClasses[*it]; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 900 | CI->Kind = ClassInfo::UserClass0 + Index; |
| 901 | |
Daniel Dunbar | 54ddf3d | 2010-05-22 21:02:29 +0000 | [diff] [blame] | 902 | ListInit *Supers = (*it)->getValueAsListInit("SuperClasses"); |
| 903 | for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) { |
| 904 | DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i)); |
| 905 | if (!DI) { |
| 906 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
| 907 | continue; |
| 908 | } |
| 909 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 910 | ClassInfo *SC = AsmOperandClasses[DI->getDef()]; |
| 911 | if (!SC) |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 912 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 913 | else |
| 914 | CI->SuperClasses.push_back(SC); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 915 | } |
| 916 | CI->ClassName = (*it)->getValueAsString("Name"); |
| 917 | CI->Name = "MCK_" + CI->ClassName; |
| 918 | CI->ValueName = (*it)->getName(); |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 919 | |
| 920 | // Get or construct the predicate method name. |
| 921 | Init *PMName = (*it)->getValueInit("PredicateMethod"); |
| 922 | if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) { |
| 923 | CI->PredicateMethod = SI->getValue(); |
| 924 | } else { |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 925 | assert(dynamic_cast<UnsetInit*>(PMName) && |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 926 | "Unexpected PredicateMethod field!"); |
| 927 | CI->PredicateMethod = "is" + CI->ClassName; |
| 928 | } |
| 929 | |
| 930 | // Get or construct the render method name. |
| 931 | Init *RMName = (*it)->getValueInit("RenderMethod"); |
| 932 | if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) { |
| 933 | CI->RenderMethod = SI->getValue(); |
| 934 | } else { |
| 935 | assert(dynamic_cast<UnsetInit*>(RMName) && |
| 936 | "Unexpected RenderMethod field!"); |
| 937 | CI->RenderMethod = "add" + CI->ClassName + "Operands"; |
| 938 | } |
| 939 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 940 | AsmOperandClasses[*it] = CI; |
| 941 | Classes.push_back(CI); |
| 942 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 945 | AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, CodeGenTarget &target) |
| 946 | : AsmParser(asmParser), Target(target), |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 947 | RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) { |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 951 | void AsmMatcherInfo::BuildInfo() { |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 952 | // Build information about all of the AssemblerPredicates. |
| 953 | std::vector<Record*> AllPredicates = |
| 954 | Records.getAllDerivedDefinitions("Predicate"); |
| 955 | for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) { |
| 956 | Record *Pred = AllPredicates[i]; |
| 957 | // Ignore predicates that are not intended for the assembler. |
| 958 | if (!Pred->getValueAsBit("AssemblerMatcherPredicate")) |
| 959 | continue; |
| 960 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 961 | if (Pred->getName().empty()) |
| 962 | throw TGError(Pred->getLoc(), "Predicate has no name!"); |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 963 | |
| 964 | unsigned FeatureNo = SubtargetFeatures.size(); |
| 965 | SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo); |
| 966 | assert(FeatureNo < 32 && "Too many subtarget features!"); |
| 967 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 968 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 969 | StringRef CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter"); |
| 970 | |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 971 | // Parse the instructions; we need to do this first so that we can gather the |
| 972 | // singleton register classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 973 | SmallPtrSet<Record*, 16> SingletonRegisters; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 974 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 975 | E = Target.inst_end(); I != E; ++I) { |
| 976 | const CodeGenInstruction &CGI = **I; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 978 | // If the tblgen -match-prefix option is specified (for tblgen hackers), |
| 979 | // filter the set of instructions we consider. |
Chris Lattner | b61e09d | 2010-03-19 00:18:23 +0000 | [diff] [blame] | 980 | if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix)) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 981 | continue; |
| 982 | |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 983 | // Ignore "codegen only" instructions. |
| 984 | if (CGI.TheDef->getValueAsBit("isCodeGenOnly")) |
| 985 | continue; |
| 986 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 987 | OwningPtr<MatchableInfo> II(new MatchableInfo(CGI)); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 988 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 989 | II->Initialize(*this, SingletonRegisters); |
| 990 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 991 | // Ignore instructions which shouldn't be matched and diagnose invalid |
| 992 | // instruction definitions with an error. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 993 | if (!II->Validate(CommentDelimiter, true)) |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 994 | continue; |
| 995 | |
| 996 | // Ignore "Int_*" and "*_Int" instructions, which are internal aliases. |
| 997 | // |
| 998 | // FIXME: This is a total hack. |
| 999 | if (StringRef(II->InstrName).startswith("Int_") || |
| 1000 | StringRef(II->InstrName).endswith("_Int")) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1001 | continue; |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1003 | Matchables.push_back(II.take()); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1004 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 1006 | // Parse all of the InstAlias definitions and stick them in the list of |
| 1007 | // matchables. |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 1008 | std::vector<Record*> AllInstAliases = |
| 1009 | Records.getAllDerivedDefinitions("InstAlias"); |
| 1010 | for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) { |
| 1011 | CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i]); |
| 1012 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1013 | OwningPtr<MatchableInfo> II(new MatchableInfo(Alias)); |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 1015 | II->Initialize(*this, SingletonRegisters); |
| 1016 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1017 | // Validate the alias definitions. |
| 1018 | II->Validate(CommentDelimiter, false); |
| 1019 | |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 1020 | Matchables.push_back(II.take()); |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 1021 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1023 | // Build info for the register classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1024 | BuildRegisterClasses(SingletonRegisters); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1025 | |
| 1026 | // Build info for the user defined assembly operand classes. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1027 | BuildOperandClasses(); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1029 | // Build the information about matchables. |
| 1030 | for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(), |
| 1031 | ie = Matchables.end(); it != ie; ++it) { |
| 1032 | MatchableInfo *II = *it; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1034 | // Parse the tokens after the mnemonic. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1035 | for (unsigned i = 0, e = II->AsmOperands.size(); i != e; ++i) { |
| 1036 | MatchableInfo::Operand &Op = II->AsmOperands[i]; |
| 1037 | StringRef Token = Op.Token; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1038 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1039 | // Check for singleton registers. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1040 | if (Record *RegRecord = II->getSingletonRegisterForAsmOperand(i, *this)) { |
| 1041 | Op.Class = RegisterClasses[RegRecord]; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1042 | assert(Op.Class && Op.Class->Registers.size() == 1 && |
| 1043 | "Unexpected class for singleton register"); |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1044 | continue; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1047 | // Check for simple tokens. |
| 1048 | if (Token[0] != '$') { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1049 | Op.Class = getTokenClass(Token); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1050 | continue; |
| 1051 | } |
| 1052 | |
| 1053 | // Otherwise this is an operand reference. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1054 | StringRef OperandName; |
| 1055 | if (Token[1] == '{') |
| 1056 | OperandName = Token.substr(2, Token.size() - 3); |
| 1057 | else |
| 1058 | OperandName = Token.substr(1); |
| 1059 | |
| 1060 | // Map this token to an operand. FIXME: Move elsewhere. |
| 1061 | unsigned Idx; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1062 | if (!II->OperandList.hasOperandNamed(OperandName, Idx)) |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1063 | throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" + |
| 1064 | OperandName.str() + "'"); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1065 | |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1066 | // FIXME: This is annoying, the named operand may be tied (e.g., |
| 1067 | // XCHG8rm). What we want is the untied operand, which we now have to |
| 1068 | // grovel for. Only worry about this for single entry operands, we have to |
| 1069 | // clean this up anyway. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1070 | const CGIOperandList::OperandInfo *OI = &II->OperandList[Idx]; |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1071 | int OITied = OI->getTiedRegister(); |
| 1072 | if (OITied != -1) { |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1073 | // The tied operand index is an MIOperand index, find the operand that |
| 1074 | // contains it. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1075 | for (unsigned i = 0, e = II->OperandList.size(); i != e; ++i) { |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1076 | if (II->OperandList[i].MIOperandNo == unsigned(OITied)) { |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1077 | OI = &II->OperandList[i]; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | assert(OI && "Unable to find tied operand target!"); |
| 1083 | } |
| 1084 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1085 | Op.Class = getOperandClass(Token, *OI); |
| 1086 | Op.OperandInfo = OI; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1087 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1088 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1089 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1090 | // Reorder classes so that classes preceed super classes. |
| 1091 | std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>()); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1094 | static void EmitConvertToMCInst(CodeGenTarget &Target, |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1095 | std::vector<MatchableInfo*> &Infos, |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1096 | raw_ostream &OS) { |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1097 | // Write the convert function to a separate stream, so we can drop it after |
| 1098 | // the enum. |
| 1099 | std::string ConvertFnBody; |
| 1100 | raw_string_ostream CvtOS(ConvertFnBody); |
| 1101 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1102 | // Function we have already generated. |
| 1103 | std::set<std::string> GeneratedFns; |
| 1104 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1105 | // Start the unified conversion function. |
| 1106 | |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 1107 | CvtOS << "static void ConvertToMCInst(ConversionKind Kind, MCInst &Inst, " |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1108 | << "unsigned Opcode,\n" |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1109 | << " const SmallVectorImpl<MCParsedAsmOperand*" |
| 1110 | << "> &Operands) {\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1111 | CvtOS << " Inst.setOpcode(Opcode);\n"; |
| 1112 | CvtOS << " switch (Kind) {\n"; |
| 1113 | CvtOS << " default:\n"; |
| 1114 | |
| 1115 | // Start the enum, which we will generate inline. |
| 1116 | |
Chris Lattner | d51257a | 2010-11-02 23:18:43 +0000 | [diff] [blame^] | 1117 | OS << "// Unified function for converting operands to MCInst instances.\n\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1118 | OS << "enum ConversionKind {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1119 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1120 | // TargetOperandClass - This is the target's operand class, like X86Operand. |
| 1121 | std::string TargetOperandClass = Target.getName() + "Operand"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1123 | /// OperandMap - This is a mapping from the MCInst operands (specified by the |
| 1124 | /// II.OperandList operands) to the AsmOperands that they filled in from. |
| 1125 | SmallVector<int, 16> OperandMap; |
| 1126 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1127 | for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(), |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1128 | ie = Infos.end(); it != ie; ++it) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1129 | MatchableInfo &II = **it; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1131 | OperandMap.clear(); |
| 1132 | OperandMap.resize(II.OperandList.size(), -1); |
| 1133 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1134 | // Order the (class) operands by the order to convert them into an MCInst. |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 1135 | for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) { |
| 1136 | MatchableInfo::Operand &Op = II.AsmOperands[i]; |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1137 | if (!Op.OperandInfo) continue; |
| 1138 | |
| 1139 | unsigned LogicalOpNum = Op.OperandInfo - &II.OperandList[0]; |
| 1140 | assert(LogicalOpNum < OperandMap.size() && "Invalid operand number"); |
| 1141 | OperandMap[LogicalOpNum] = i; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | // Build the conversion function signature. |
| 1145 | std::string Signature = "Convert"; |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1146 | std::string CaseBody; |
| 1147 | raw_string_ostream CaseOS(CaseBody); |
| 1148 | |
| 1149 | // Compute the convert enum and the case body. |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1150 | for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) { |
| 1151 | const CGIOperandList::OperandInfo &OpInfo = II.OperandList[i]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1153 | // Find out what operand from the asmparser that this MCInst operand comes |
| 1154 | // from. |
| 1155 | int SrcOperand = OperandMap[i]; |
| 1156 | if (SrcOperand != -1) { |
| 1157 | // Otherwise, this comes from something we parsed. |
| 1158 | MatchableInfo::Operand &Op = II.AsmOperands[SrcOperand]; |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1160 | // Registers are always converted the same, don't duplicate the |
| 1161 | // conversion function based on them. |
| 1162 | // |
| 1163 | // FIXME: We could generalize this based on the render method, if it |
| 1164 | // mattered. |
| 1165 | Signature += "__"; |
| 1166 | if (Op.Class->isRegisterClass()) |
| 1167 | Signature += "Reg"; |
| 1168 | else |
| 1169 | Signature += Op.Class->ClassName; |
| 1170 | Signature += utostr(Op.OperandInfo->MINumOperands); |
| 1171 | Signature += "_" + itostr(SrcOperand); |
| 1172 | |
| 1173 | CaseOS << " ((" << TargetOperandClass << "*)Operands[" |
| 1174 | << SrcOperand << "+1])->" << Op.Class->RenderMethod |
| 1175 | << "(Inst, " << Op.OperandInfo->MINumOperands << ");\n"; |
| 1176 | continue; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1177 | } |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1179 | |
| 1180 | // If this operand is tied to a previous one, just copy the MCInst operand |
| 1181 | // from the earlier one. |
| 1182 | int TiedOp = OpInfo.getTiedRegister(); |
| 1183 | if (TiedOp != -1) { |
Chris Lattner | d51257a | 2010-11-02 23:18:43 +0000 | [diff] [blame^] | 1184 | // Copy the tied operand. We can only tie single MCOperand values. |
| 1185 | assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand"); |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1186 | assert(i > unsigned(TiedOp) && "Tied operand preceeds its target!"); |
| 1187 | CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n"; |
| 1188 | Signature += "__Tie" + itostr(TiedOp); |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | // Otherwise this is some sort of dummy operand that is mentioned in the |
| 1193 | // ins/outs list but not mentioned in the asmstring, brutalize a dummy |
| 1194 | // value into the operand. |
| 1195 | // FIXME: This is a terrible hack: If an MCInst operand doesn't occur in |
| 1196 | // the asmstring, there is no way to parse something meaningful. |
| 1197 | // Just assume it is a zero register for now. |
| 1198 | CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n"; |
| 1199 | Signature += "__Imp"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1200 | } |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1201 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1202 | II.ConversionFnKind = Signature; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1203 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1204 | // Check if we have already generated this signature. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1205 | if (!GeneratedFns.insert(Signature).second) |
| 1206 | continue; |
| 1207 | |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1208 | // If not, emit it now. Add to the enum list. |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1209 | OS << " " << Signature << ",\n"; |
| 1210 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1211 | CvtOS << " case " << Signature << ":\n"; |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1212 | CvtOS << CaseOS.str(); |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 1213 | CvtOS << " return;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1214 | } |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Finish the convert function. |
| 1217 | |
| 1218 | CvtOS << " }\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1219 | CvtOS << "}\n\n"; |
| 1220 | |
| 1221 | // Finish the enum, and drop the convert function after it. |
| 1222 | |
| 1223 | OS << " NumConversionVariants\n"; |
| 1224 | OS << "};\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1225 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1226 | OS << CvtOS.str(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1229 | /// EmitMatchClassEnumeration - Emit the enumeration for match class kinds. |
| 1230 | static void EmitMatchClassEnumeration(CodeGenTarget &Target, |
| 1231 | std::vector<ClassInfo*> &Infos, |
| 1232 | raw_ostream &OS) { |
| 1233 | OS << "namespace {\n\n"; |
| 1234 | |
| 1235 | OS << "/// MatchClassKind - The kinds of classes which participate in\n" |
| 1236 | << "/// instruction matching.\n"; |
| 1237 | OS << "enum MatchClassKind {\n"; |
| 1238 | OS << " InvalidMatchClass = 0,\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1239 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1240 | ie = Infos.end(); it != ie; ++it) { |
| 1241 | ClassInfo &CI = **it; |
| 1242 | OS << " " << CI.Name << ", // "; |
| 1243 | if (CI.Kind == ClassInfo::Token) { |
| 1244 | OS << "'" << CI.ValueName << "'\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1245 | } else if (CI.isRegisterClass()) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1246 | if (!CI.ValueName.empty()) |
| 1247 | OS << "register class '" << CI.ValueName << "'\n"; |
| 1248 | else |
| 1249 | OS << "derived register class\n"; |
| 1250 | } else { |
| 1251 | OS << "user defined class '" << CI.ValueName << "'\n"; |
| 1252 | } |
| 1253 | } |
| 1254 | OS << " NumMatchClassKinds\n"; |
| 1255 | OS << "};\n\n"; |
| 1256 | |
| 1257 | OS << "}\n\n"; |
| 1258 | } |
| 1259 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1260 | /// EmitClassifyOperand - Emit the function to classify an operand. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1261 | static void EmitClassifyOperand(AsmMatcherInfo &Info, |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1262 | raw_ostream &OS) { |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1263 | OS << "static MatchClassKind ClassifyOperand(MCParsedAsmOperand *GOp) {\n" |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1264 | << " " << Info.Target.getName() << "Operand &Operand = *(" |
| 1265 | << Info.Target.getName() << "Operand*)GOp;\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1266 | |
| 1267 | // Classify tokens. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1268 | OS << " if (Operand.isToken())\n"; |
| 1269 | OS << " return MatchTokenString(Operand.getToken());\n\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1270 | |
| 1271 | // Classify registers. |
| 1272 | // |
| 1273 | // FIXME: Don't hardcode isReg, getReg. |
| 1274 | OS << " if (Operand.isReg()) {\n"; |
| 1275 | OS << " switch (Operand.getReg()) {\n"; |
| 1276 | OS << " default: return InvalidMatchClass;\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1277 | for (std::map<Record*, ClassInfo*>::iterator |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1278 | it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end(); |
| 1279 | it != ie; ++it) |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1280 | OS << " case " << Info.Target.getName() << "::" |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1281 | << it->first->getName() << ": return " << it->second->Name << ";\n"; |
| 1282 | OS << " }\n"; |
| 1283 | OS << " }\n\n"; |
| 1284 | |
| 1285 | // Classify user defined operands. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1286 | for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(), |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1287 | ie = Info.Classes.end(); it != ie; ++it) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1288 | ClassInfo &CI = **it; |
| 1289 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1290 | if (!CI.isUserClass()) |
| 1291 | continue; |
| 1292 | |
| 1293 | OS << " // '" << CI.ClassName << "' class"; |
| 1294 | if (!CI.SuperClasses.empty()) { |
| 1295 | OS << ", subclass of "; |
| 1296 | for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) { |
| 1297 | if (i) OS << ", "; |
| 1298 | OS << "'" << CI.SuperClasses[i]->ClassName << "'"; |
| 1299 | assert(CI < *CI.SuperClasses[i] && "Invalid class relation!"); |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1300 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1301 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1302 | OS << "\n"; |
| 1303 | |
| 1304 | OS << " if (Operand." << CI.PredicateMethod << "()) {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1305 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1306 | // Validate subclass relationships. |
| 1307 | if (!CI.SuperClasses.empty()) { |
| 1308 | for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) |
| 1309 | OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod |
| 1310 | << "() && \"Invalid class relationship!\");\n"; |
| 1311 | } |
| 1312 | |
| 1313 | OS << " return " << CI.Name << ";\n"; |
| 1314 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1315 | } |
| 1316 | OS << " return InvalidMatchClass;\n"; |
| 1317 | OS << "}\n\n"; |
| 1318 | } |
| 1319 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1320 | /// EmitIsSubclass - Emit the subclass predicate function. |
| 1321 | static void EmitIsSubclass(CodeGenTarget &Target, |
| 1322 | std::vector<ClassInfo*> &Infos, |
| 1323 | raw_ostream &OS) { |
| 1324 | OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n"; |
| 1325 | OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n"; |
| 1326 | OS << " if (A == B)\n"; |
| 1327 | OS << " return true;\n\n"; |
| 1328 | |
| 1329 | OS << " switch (A) {\n"; |
| 1330 | OS << " default:\n"; |
| 1331 | OS << " return false;\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1332 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1333 | ie = Infos.end(); it != ie; ++it) { |
| 1334 | ClassInfo &A = **it; |
| 1335 | |
| 1336 | if (A.Kind != ClassInfo::Token) { |
| 1337 | std::vector<StringRef> SuperClasses; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1338 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1339 | ie = Infos.end(); it != ie; ++it) { |
| 1340 | ClassInfo &B = **it; |
| 1341 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1342 | if (&A != &B && A.isSubsetOf(B)) |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1343 | SuperClasses.push_back(B.Name); |
| 1344 | } |
| 1345 | |
| 1346 | if (SuperClasses.empty()) |
| 1347 | continue; |
| 1348 | |
| 1349 | OS << "\n case " << A.Name << ":\n"; |
| 1350 | |
| 1351 | if (SuperClasses.size() == 1) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1352 | OS << " return B == " << SuperClasses.back() << ";\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1353 | continue; |
| 1354 | } |
| 1355 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1356 | OS << " switch (B) {\n"; |
| 1357 | OS << " default: return false;\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1358 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1359 | OS << " case " << SuperClasses[i] << ": return true;\n"; |
| 1360 | OS << " }\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | OS << " }\n"; |
| 1364 | OS << "}\n\n"; |
| 1365 | } |
| 1366 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1367 | |
| 1368 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1369 | /// EmitMatchTokenString - Emit the function to match a token string to the |
| 1370 | /// appropriate match class value. |
| 1371 | static void EmitMatchTokenString(CodeGenTarget &Target, |
| 1372 | std::vector<ClassInfo*> &Infos, |
| 1373 | raw_ostream &OS) { |
| 1374 | // Construct the match list. |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1375 | std::vector<StringMatcher::StringPair> Matches; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1376 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1377 | ie = Infos.end(); it != ie; ++it) { |
| 1378 | ClassInfo &CI = **it; |
| 1379 | |
| 1380 | if (CI.Kind == ClassInfo::Token) |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1381 | Matches.push_back(StringMatcher::StringPair(CI.ValueName, |
| 1382 | "return " + CI.Name + ";")); |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1385 | OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n"; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1387 | StringMatcher("Name", Matches, OS).Emit(); |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1388 | |
| 1389 | OS << " return InvalidMatchClass;\n"; |
| 1390 | OS << "}\n\n"; |
| 1391 | } |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1392 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1393 | /// EmitMatchRegisterName - Emit the function to match a string to the target |
| 1394 | /// specific register enum. |
| 1395 | static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser, |
| 1396 | raw_ostream &OS) { |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1397 | // Construct the match list. |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1398 | std::vector<StringMatcher::StringPair> Matches; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1399 | for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) { |
| 1400 | const CodeGenRegister &Reg = Target.getRegisters()[i]; |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1401 | if (Reg.TheDef->getValueAsString("AsmName").empty()) |
| 1402 | continue; |
| 1403 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1404 | Matches.push_back(StringMatcher::StringPair( |
| 1405 | Reg.TheDef->getValueAsString("AsmName"), |
| 1406 | "return " + utostr(i + 1) + ";")); |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1407 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1409 | OS << "static unsigned MatchRegisterName(StringRef Name) {\n"; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1411 | StringMatcher("Name", Matches, OS).Emit(); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1412 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1413 | OS << " return 0;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1414 | OS << "}\n\n"; |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1415 | } |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1416 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1417 | /// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag |
| 1418 | /// definitions. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1419 | static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info, |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1420 | raw_ostream &OS) { |
| 1421 | OS << "// Flags for subtarget features that participate in " |
| 1422 | << "instruction matching.\n"; |
| 1423 | OS << "enum SubtargetFeatureFlag {\n"; |
| 1424 | for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator |
| 1425 | it = Info.SubtargetFeatures.begin(), |
| 1426 | ie = Info.SubtargetFeatures.end(); it != ie; ++it) { |
| 1427 | SubtargetFeatureInfo &SFI = *it->second; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1428 | OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1429 | } |
| 1430 | OS << " Feature_None = 0\n"; |
| 1431 | OS << "};\n\n"; |
| 1432 | } |
| 1433 | |
| 1434 | /// EmitComputeAvailableFeatures - Emit the function to compute the list of |
| 1435 | /// available features given a subtarget. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1436 | static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info, |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1437 | raw_ostream &OS) { |
| 1438 | std::string ClassName = |
| 1439 | Info.AsmParser->getValueAsString("AsmParserClassName"); |
| 1440 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1441 | OS << "unsigned " << Info.Target.getName() << ClassName << "::\n" |
| 1442 | << "ComputeAvailableFeatures(const " << Info.Target.getName() |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1443 | << "Subtarget *Subtarget) const {\n"; |
| 1444 | OS << " unsigned Features = 0;\n"; |
| 1445 | for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator |
| 1446 | it = Info.SubtargetFeatures.begin(), |
| 1447 | ie = Info.SubtargetFeatures.end(); it != ie; ++it) { |
| 1448 | SubtargetFeatureInfo &SFI = *it->second; |
| 1449 | OS << " if (" << SFI.TheDef->getValueAsString("CondString") |
| 1450 | << ")\n"; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1451 | OS << " Features |= " << SFI.getEnumName() << ";\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1452 | } |
| 1453 | OS << " return Features;\n"; |
| 1454 | OS << "}\n\n"; |
| 1455 | } |
| 1456 | |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 1457 | static std::string GetAliasRequiredFeatures(Record *R, |
| 1458 | const AsmMatcherInfo &Info) { |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1459 | std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates"); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1460 | std::string Result; |
| 1461 | unsigned NumFeatures = 0; |
| 1462 | for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) { |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1463 | SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1465 | if (F == 0) |
| 1466 | throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() + |
| 1467 | "' is not marked as an AssemblerPredicate!"); |
| 1468 | |
| 1469 | if (NumFeatures) |
| 1470 | Result += '|'; |
| 1471 | |
| 1472 | Result += F->getEnumName(); |
| 1473 | ++NumFeatures; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | if (NumFeatures > 1) |
| 1477 | Result = '(' + Result + ')'; |
| 1478 | return Result; |
| 1479 | } |
| 1480 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1481 | /// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions, |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1482 | /// emit a function for them and return true, otherwise return false. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1483 | static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) { |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1484 | std::vector<Record*> Aliases = |
| 1485 | Records.getAllDerivedDefinitions("MnemonicAlias"); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1486 | if (Aliases.empty()) return false; |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | 8cc0a6b | 2010-10-30 18:57:07 +0000 | [diff] [blame] | 1488 | OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, " |
| 1489 | "unsigned Features) {\n"; |
| 1490 | |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1491 | // Keep track of all the aliases from a mnemonic. Use an std::map so that the |
| 1492 | // iteration order of the map is stable. |
| 1493 | std::map<std::string, std::vector<Record*> > AliasesFromMnemonic; |
| 1494 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1495 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
| 1496 | Record *R = Aliases[i]; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1497 | AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R); |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1498 | } |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1499 | |
| 1500 | // Process each alias a "from" mnemonic at a time, building the code executed |
| 1501 | // by the string remapper. |
| 1502 | std::vector<StringMatcher::StringPair> Cases; |
| 1503 | for (std::map<std::string, std::vector<Record*> >::iterator |
| 1504 | I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end(); |
| 1505 | I != E; ++I) { |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1506 | const std::vector<Record*> &ToVec = I->second; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Loop through each alias and emit code that handles each case. If there |
| 1509 | // are two instructions without predicates, emit an error. If there is one, |
| 1510 | // emit it last. |
| 1511 | std::string MatchCode; |
| 1512 | int AliasWithNoPredicate = -1; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1514 | for (unsigned i = 0, e = ToVec.size(); i != e; ++i) { |
| 1515 | Record *R = ToVec[i]; |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 1516 | std::string FeatureMask = GetAliasRequiredFeatures(R, Info); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1517 | |
| 1518 | // If this unconditionally matches, remember it for later and diagnose |
| 1519 | // duplicates. |
| 1520 | if (FeatureMask.empty()) { |
| 1521 | if (AliasWithNoPredicate != -1) { |
| 1522 | // We can't have two aliases from the same mnemonic with no predicate. |
| 1523 | PrintError(ToVec[AliasWithNoPredicate]->getLoc(), |
| 1524 | "two MnemonicAliases with the same 'from' mnemonic!"); |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1525 | throw TGError(R->getLoc(), "this is the other MnemonicAlias."); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | AliasWithNoPredicate = i; |
| 1529 | continue; |
| 1530 | } |
| 1531 | |
Chris Lattner | 8cf8bcc | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 1532 | if (!MatchCode.empty()) |
| 1533 | MatchCode += "else "; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1534 | MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n"; |
| 1535 | MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n"; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1538 | if (AliasWithNoPredicate != -1) { |
| 1539 | Record *R = ToVec[AliasWithNoPredicate]; |
Chris Lattner | 8cf8bcc | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 1540 | if (!MatchCode.empty()) |
| 1541 | MatchCode += "else\n "; |
| 1542 | MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n"; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | MatchCode += "return;"; |
| 1546 | |
| 1547 | Cases.push_back(std::make_pair(I->first, MatchCode)); |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1550 | |
| 1551 | StringMatcher("Mnemonic", Cases, OS).Emit(); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1552 | OS << "}\n"; |
| 1553 | |
| 1554 | return true; |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1557 | void AsmMatcherEmitter::run(raw_ostream &OS) { |
| 1558 | CodeGenTarget Target; |
| 1559 | Record *AsmParser = Target.getAsmParser(); |
| 1560 | std::string ClassName = AsmParser->getValueAsString("AsmParserClassName"); |
| 1561 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1562 | // Compute the information on the instructions to match. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1563 | AsmMatcherInfo Info(AsmParser, Target); |
| 1564 | Info.BuildInfo(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1565 | |
Daniel Dunbar | e1f6de3 | 2010-02-02 23:46:36 +0000 | [diff] [blame] | 1566 | // Sort the instruction table using the partial order on classes. We use |
| 1567 | // stable_sort to ensure that ambiguous instructions are still |
| 1568 | // deterministically ordered. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1569 | std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(), |
| 1570 | less_ptr<MatchableInfo>()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1571 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1572 | DEBUG_WITH_TYPE("instruction_info", { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1573 | for (std::vector<MatchableInfo*>::iterator |
| 1574 | it = Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1575 | it != ie; ++it) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1576 | (*it)->dump(); |
| 1577 | }); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1578 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1579 | // Check for ambiguous matchables. |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 1580 | DEBUG_WITH_TYPE("ambiguous_instrs", { |
| 1581 | unsigned NumAmbiguous = 0; |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1582 | for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) { |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1583 | for (unsigned j = i + 1; j != e; ++j) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1584 | MatchableInfo &A = *Info.Matchables[i]; |
| 1585 | MatchableInfo &B = *Info.Matchables[j]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1587 | if (A.CouldMatchAmiguouslyWith(B)) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1588 | errs() << "warning: ambiguous matchables:\n"; |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 1589 | A.dump(); |
| 1590 | errs() << "\nis incomparable with:\n"; |
| 1591 | B.dump(); |
| 1592 | errs() << "\n\n"; |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1593 | ++NumAmbiguous; |
| 1594 | } |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 1595 | } |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1596 | } |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1597 | if (NumAmbiguous) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1598 | errs() << "warning: " << NumAmbiguous |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1599 | << " ambiguous matchables!\n"; |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 1600 | }); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1601 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1602 | // Write the output. |
| 1603 | |
| 1604 | EmitSourceFileHeader("Assembly Matcher Source Fragment", OS); |
| 1605 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1606 | // Information for the class declaration. |
| 1607 | OS << "\n#ifdef GET_ASSEMBLER_HEADER\n"; |
| 1608 | OS << "#undef GET_ASSEMBLER_HEADER\n"; |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1609 | OS << " // This should be included into the middle of the declaration of \n"; |
| 1610 | OS << " // your subclasses implementation of TargetAsmParser.\n"; |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1611 | OS << " unsigned ComputeAvailableFeatures(const " << |
| 1612 | Target.getName() << "Subtarget *Subtarget) const;\n"; |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1613 | OS << " enum MatchResultTy {\n"; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1614 | OS << " Match_Success, Match_MnemonicFail, Match_InvalidOperand,\n"; |
| 1615 | OS << " Match_MissingFeature\n"; |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1616 | OS << " };\n"; |
Jim Grosbach | bb16824 | 2010-10-08 18:13:57 +0000 | [diff] [blame] | 1617 | OS << " MatchResultTy MatchInstructionImpl(const " |
| 1618 | << "SmallVectorImpl<MCParsedAsmOperand*>" |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1619 | << " &Operands, MCInst &Inst, unsigned &ErrorInfo);\n\n"; |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1620 | OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n"; |
| 1621 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1622 | |
| 1623 | |
| 1624 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1625 | OS << "\n#ifdef GET_REGISTER_MATCHER\n"; |
| 1626 | OS << "#undef GET_REGISTER_MATCHER\n\n"; |
| 1627 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1628 | // Emit the subtarget feature enumeration. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1629 | EmitSubtargetFeatureFlagEnumeration(Info, OS); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1630 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1631 | // Emit the function to match a register name to number. |
| 1632 | EmitMatchRegisterName(Target, AsmParser, OS); |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1633 | |
| 1634 | OS << "#endif // GET_REGISTER_MATCHER\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1636 | |
| 1637 | OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n"; |
| 1638 | OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n"; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1640 | // Generate the function that remaps for mnemonic aliases. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1641 | bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1642 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1643 | // Generate the unified function to convert operands into an MCInst. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1644 | EmitConvertToMCInst(Target, Info.Matchables, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1645 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1646 | // Emit the enumeration for classes which participate in matching. |
| 1647 | EmitMatchClassEnumeration(Target, Info.Classes, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1648 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1649 | // Emit the routine to match token strings to their match class. |
| 1650 | EmitMatchTokenString(Target, Info.Classes, OS); |
| 1651 | |
| 1652 | // Emit the routine to classify an operand. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1653 | EmitClassifyOperand(Info, OS); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1654 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1655 | // Emit the subclass predicate routine. |
| 1656 | EmitIsSubclass(Target, Info.Classes, OS); |
| 1657 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1658 | // Emit the available features compute function. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1659 | EmitComputeAvailableFeatures(Info, OS); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1660 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1661 | |
| 1662 | size_t MaxNumOperands = 0; |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1663 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 1664 | Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1665 | it != ie; ++it) |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 1666 | MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1667 | |
| 1668 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1669 | // Emit the static match table; unused classes get initalized to 0 which is |
| 1670 | // guaranteed to be InvalidMatchClass. |
| 1671 | // |
| 1672 | // FIXME: We can reduce the size of this table very easily. First, we change |
| 1673 | // it so that store the kinds in separate bit-fields for each index, which |
| 1674 | // only needs to be the max width used for classes at that index (we also need |
| 1675 | // to reject based on this during classification). If we then make sure to |
| 1676 | // order the match kinds appropriately (putting mnemonics last), then we |
| 1677 | // should only end up using a few bits for each class, especially the ones |
| 1678 | // following the mnemonic. |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1679 | OS << "namespace {\n"; |
| 1680 | OS << " struct MatchEntry {\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1681 | OS << " unsigned Opcode;\n"; |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1682 | OS << " const char *Mnemonic;\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1683 | OS << " ConversionKind ConvertFn;\n"; |
| 1684 | OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1685 | OS << " unsigned RequiredFeatures;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1686 | OS << " };\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1687 | |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1688 | OS << "// Predicate for searching for an opcode.\n"; |
| 1689 | OS << " struct LessOpcode {\n"; |
| 1690 | OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n"; |
| 1691 | OS << " return StringRef(LHS.Mnemonic) < RHS;\n"; |
| 1692 | OS << " }\n"; |
| 1693 | OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n"; |
| 1694 | OS << " return LHS < StringRef(RHS.Mnemonic);\n"; |
| 1695 | OS << " }\n"; |
Chris Lattner | 32c685c | 2010-09-07 06:10:48 +0000 | [diff] [blame] | 1696 | OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n"; |
| 1697 | OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n"; |
| 1698 | OS << " }\n"; |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1699 | OS << " };\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1701 | OS << "} // end anonymous namespace.\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1702 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1703 | OS << "static const MatchEntry MatchTable[" |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1704 | << Info.Matchables.size() << "] = {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1706 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 1707 | Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1708 | it != ie; ++it) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1709 | MatchableInfo &II = **it; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1711 | OS << " { " << Target.getName() << "::" << II.InstrName |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1712 | << ", \"" << II.Mnemonic << "\"" |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1713 | << ", " << II.ConversionFnKind << ", { "; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 1714 | for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) { |
| 1715 | MatchableInfo::Operand &Op = II.AsmOperands[i]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1716 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1717 | if (i) OS << ", "; |
| 1718 | OS << Op.Class->Name; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1719 | } |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1720 | OS << " }, "; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1721 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1722 | // Write the required features mask. |
| 1723 | if (!II.RequiredFeatures.empty()) { |
| 1724 | for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) { |
| 1725 | if (i) OS << "|"; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1726 | OS << II.RequiredFeatures[i]->getEnumName(); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1727 | } |
| 1728 | } else |
| 1729 | OS << "0"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1730 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1731 | OS << "},\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1732 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1734 | OS << "};\n\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1736 | // Finally, build the match function. |
| 1737 | OS << Target.getName() << ClassName << "::MatchResultTy " |
| 1738 | << Target.getName() << ClassName << "::\n" |
| 1739 | << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>" |
| 1740 | << " &Operands,\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1741 | OS << " MCInst &Inst, unsigned &ErrorInfo) {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Emit code to get the available features. |
| 1744 | OS << " // Get the current feature set.\n"; |
| 1745 | OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n"; |
| 1746 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1747 | OS << " // Get the instruction mnemonic, which is the first token.\n"; |
| 1748 | OS << " StringRef Mnemonic = ((" << Target.getName() |
| 1749 | << "Operand*)Operands[0])->getToken();\n\n"; |
| 1750 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1751 | if (HasMnemonicAliases) { |
| 1752 | OS << " // Process all MnemonicAliases to remap the mnemonic.\n"; |
| 1753 | OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n"; |
| 1754 | } |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1755 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1756 | // Emit code to compute the class list for this operand vector. |
| 1757 | OS << " // Eliminate obvious mismatches.\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1758 | OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n"; |
| 1759 | OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n"; |
| 1760 | OS << " return Match_InvalidOperand;\n"; |
| 1761 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1762 | |
| 1763 | OS << " // Compute the class list for this operand vector.\n"; |
| 1764 | OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1765 | OS << " for (unsigned i = 1, e = Operands.size(); i != e; ++i) {\n"; |
| 1766 | OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1767 | |
| 1768 | OS << " // Check for invalid operands before matching.\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1769 | OS << " if (Classes[i-1] == InvalidMatchClass) {\n"; |
| 1770 | OS << " ErrorInfo = i;\n"; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1771 | OS << " return Match_InvalidOperand;\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1772 | OS << " }\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1773 | OS << " }\n\n"; |
| 1774 | |
| 1775 | OS << " // Mark unused classes.\n"; |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1776 | OS << " for (unsigned i = Operands.size()-1, e = " << MaxNumOperands << "; " |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1777 | << "i != e; ++i)\n"; |
| 1778 | OS << " Classes[i] = InvalidMatchClass;\n\n"; |
| 1779 | |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1780 | OS << " // Some state to try to produce better error messages.\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1781 | OS << " bool HadMatchOtherThanFeatures = false;\n\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1782 | OS << " // Set ErrorInfo to the operand that mismatches if it is \n"; |
| 1783 | OS << " // wrong for all instances of the instruction.\n"; |
| 1784 | OS << " ErrorInfo = ~0U;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1785 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1786 | // Emit code to search the table. |
| 1787 | OS << " // Search the table.\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1788 | OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n"; |
| 1789 | OS << " std::equal_range(MatchTable, MatchTable+" |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1790 | << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1792 | OS << " // Return a more specific error code if no mnemonics match.\n"; |
| 1793 | OS << " if (MnemonicRange.first == MnemonicRange.second)\n"; |
| 1794 | OS << " return Match_MnemonicFail;\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1795 | |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1796 | OS << " for (const MatchEntry *it = MnemonicRange.first, " |
Chris Lattner | 80db4e5 | 2010-09-06 21:23:43 +0000 | [diff] [blame] | 1797 | << "*ie = MnemonicRange.second;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1798 | OS << " it != ie; ++it) {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1799 | |
Gabor Greif | e53ee3b | 2010-09-07 06:06:06 +0000 | [diff] [blame] | 1800 | OS << " // equal_range guarantees that instruction mnemonic matches.\n"; |
Chris Lattner | 44b0daa | 2010-09-06 21:25:43 +0000 | [diff] [blame] | 1801 | OS << " assert(Mnemonic == it->Mnemonic);\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1802 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1803 | // Emit check that the subclasses match. |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1804 | OS << " bool OperandsValid = true;\n"; |
| 1805 | OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n"; |
| 1806 | OS << " if (IsSubclass(Classes[i], it->Classes[i]))\n"; |
| 1807 | OS << " continue;\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1808 | OS << " // If this operand is broken for all of the instances of this\n"; |
| 1809 | OS << " // mnemonic, keep track of it so we can report loc info.\n"; |
| 1810 | OS << " if (it == MnemonicRange.first || ErrorInfo == i+1)\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1811 | OS << " ErrorInfo = i+1;\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1812 | OS << " else\n"; |
| 1813 | OS << " ErrorInfo = ~0U;"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1814 | OS << " // Otherwise, just reject this instance of the mnemonic.\n"; |
| 1815 | OS << " OperandsValid = false;\n"; |
| 1816 | OS << " break;\n"; |
| 1817 | OS << " }\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1818 | |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1819 | OS << " if (!OperandsValid) continue;\n"; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1820 | |
| 1821 | // Emit check that the required features are available. |
| 1822 | OS << " if ((AvailableFeatures & it->RequiredFeatures) " |
| 1823 | << "!= it->RequiredFeatures) {\n"; |
| 1824 | OS << " HadMatchOtherThanFeatures = true;\n"; |
| 1825 | OS << " continue;\n"; |
| 1826 | OS << " }\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1827 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1828 | OS << "\n"; |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 1829 | OS << " ConvertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n"; |
| 1830 | |
| 1831 | // Call the post-processing function, if used. |
| 1832 | std::string InsnCleanupFn = |
| 1833 | AsmParser->getValueAsString("AsmParserInstCleanup"); |
| 1834 | if (!InsnCleanupFn.empty()) |
| 1835 | OS << " " << InsnCleanupFn << "(Inst);\n"; |
| 1836 | |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1837 | OS << " return Match_Success;\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1838 | OS << " }\n\n"; |
| 1839 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1840 | OS << " // Okay, we had no match. Try to return a useful error code.\n"; |
| 1841 | OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n"; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1842 | OS << " return Match_InvalidOperand;\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1843 | OS << "}\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1844 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1845 | OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n"; |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 1846 | } |