Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 1 | //===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===// |
| 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 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 10 | #include "llvm/Target/TargetAsmParser.h" |
Daniel Dunbar | 4cb1e13 | 2009-07-18 23:03:22 +0000 | [diff] [blame] | 11 | #include "X86.h" |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 12 | #include "X86Subtarget.h" |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 1b6c060 | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInst.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 21 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 22 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegistry.h" |
| 26 | #include "llvm/Target/TargetAsmParser.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
Benjamin Kramer | c6b79ac | 2009-07-31 11:35:26 +0000 | [diff] [blame] | 30 | struct X86Operand; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 31 | |
| 32 | class X86ATTAsmParser : public TargetAsmParser { |
| 33 | MCAsmParser &Parser; |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 34 | TargetMachine &TM; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 35 | |
Daniel Dunbar | f98bc63 | 2010-03-18 20:06:02 +0000 | [diff] [blame] | 36 | protected: |
| 37 | unsigned Is64Bit : 1; |
| 38 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 39 | private: |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 40 | MCAsmParser &getParser() const { return Parser; } |
| 41 | |
| 42 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 43 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 44 | bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } |
| 45 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 46 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 48 | X86Operand *ParseOperand(); |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 49 | X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 50 | |
| 51 | bool ParseDirectiveWord(unsigned Size, SMLoc L); |
| 52 | |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 53 | bool MatchInstruction(SMLoc IDLoc, |
| 54 | const SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 55 | MCInst &Inst); |
| 56 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 57 | /// @name Auto-generated Matcher Functions |
| 58 | /// { |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 59 | |
| 60 | #define GET_ASSEMBLER_HEADER |
| 61 | #include "X86GenAsmMatcher.inc" |
| 62 | |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 63 | /// } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 66 | X86ATTAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &TM) |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 67 | : TargetAsmParser(T), Parser(_Parser), TM(TM) { |
| 68 | |
| 69 | // Initialize the set of available features. |
| 70 | setAvailableFeatures(ComputeAvailableFeatures( |
| 71 | &TM.getSubtarget<X86Subtarget>())); |
| 72 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 73 | |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 74 | virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 75 | SmallVectorImpl<MCParsedAsmOperand*> &Operands); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 76 | |
| 77 | virtual bool ParseDirective(AsmToken DirectiveID); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 78 | }; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 79 | |
Daniel Dunbar | f98bc63 | 2010-03-18 20:06:02 +0000 | [diff] [blame] | 80 | class X86_32ATTAsmParser : public X86ATTAsmParser { |
| 81 | public: |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 82 | X86_32ATTAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &TM) |
| 83 | : X86ATTAsmParser(T, _Parser, TM) { |
Daniel Dunbar | f98bc63 | 2010-03-18 20:06:02 +0000 | [diff] [blame] | 84 | Is64Bit = false; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | class X86_64ATTAsmParser : public X86ATTAsmParser { |
| 89 | public: |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 90 | X86_64ATTAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &TM) |
| 91 | : X86ATTAsmParser(T, _Parser, TM) { |
Daniel Dunbar | f98bc63 | 2010-03-18 20:06:02 +0000 | [diff] [blame] | 92 | Is64Bit = true; |
| 93 | } |
| 94 | }; |
| 95 | |
Chris Lattner | 37dfdec | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 96 | } // end anonymous namespace |
| 97 | |
Sean Callanan | e9b466d | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 98 | /// @name Auto-generated Match Functions |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 99 | /// { |
Sean Callanan | e9b466d | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 100 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 101 | static unsigned MatchRegisterName(StringRef Name); |
Sean Callanan | e9b466d | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 102 | |
| 103 | /// } |
Chris Lattner | 37dfdec | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 104 | |
| 105 | namespace { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 106 | |
| 107 | /// X86Operand - Instances of this class represent a parsed X86 machine |
| 108 | /// instruction. |
Chris Lattner | 45220a8 | 2010-01-14 21:20:55 +0000 | [diff] [blame] | 109 | struct X86Operand : public MCParsedAsmOperand { |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 110 | enum KindTy { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 111 | Token, |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 112 | Register, |
| 113 | Immediate, |
| 114 | Memory |
| 115 | } Kind; |
| 116 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 117 | SMLoc StartLoc, EndLoc; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 119 | union { |
| 120 | struct { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 121 | const char *Data; |
| 122 | unsigned Length; |
| 123 | } Tok; |
| 124 | |
| 125 | struct { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 126 | unsigned RegNo; |
| 127 | } Reg; |
| 128 | |
| 129 | struct { |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 130 | const MCExpr *Val; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 131 | } Imm; |
| 132 | |
| 133 | struct { |
| 134 | unsigned SegReg; |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 135 | const MCExpr *Disp; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 136 | unsigned BaseReg; |
| 137 | unsigned IndexReg; |
| 138 | unsigned Scale; |
| 139 | } Mem; |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 140 | }; |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 142 | X86Operand(KindTy K, SMLoc Start, SMLoc End) |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 143 | : Kind(K), StartLoc(Start), EndLoc(End) {} |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 145 | /// getStartLoc - Get the location of the first token of this operand. |
| 146 | SMLoc getStartLoc() const { return StartLoc; } |
| 147 | /// getEndLoc - Get the location of the last token of this operand. |
| 148 | SMLoc getEndLoc() const { return EndLoc; } |
| 149 | |
Daniel Dunbar | b3cb696 | 2010-08-11 06:37:04 +0000 | [diff] [blame] | 150 | virtual void dump(raw_ostream &OS) const {} |
| 151 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 152 | StringRef getToken() const { |
| 153 | assert(Kind == Token && "Invalid access!"); |
| 154 | return StringRef(Tok.Data, Tok.Length); |
| 155 | } |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 156 | void setTokenValue(StringRef Value) { |
| 157 | assert(Kind == Token && "Invalid access!"); |
| 158 | Tok.Data = Value.data(); |
| 159 | Tok.Length = Value.size(); |
| 160 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 161 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 162 | unsigned getReg() const { |
| 163 | assert(Kind == Register && "Invalid access!"); |
| 164 | return Reg.RegNo; |
| 165 | } |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 167 | const MCExpr *getImm() const { |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 168 | assert(Kind == Immediate && "Invalid access!"); |
| 169 | return Imm.Val; |
| 170 | } |
| 171 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 172 | const MCExpr *getMemDisp() const { |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 173 | assert(Kind == Memory && "Invalid access!"); |
| 174 | return Mem.Disp; |
| 175 | } |
| 176 | unsigned getMemSegReg() const { |
| 177 | assert(Kind == Memory && "Invalid access!"); |
| 178 | return Mem.SegReg; |
| 179 | } |
| 180 | unsigned getMemBaseReg() const { |
| 181 | assert(Kind == Memory && "Invalid access!"); |
| 182 | return Mem.BaseReg; |
| 183 | } |
| 184 | unsigned getMemIndexReg() const { |
| 185 | assert(Kind == Memory && "Invalid access!"); |
| 186 | return Mem.IndexReg; |
| 187 | } |
| 188 | unsigned getMemScale() const { |
| 189 | assert(Kind == Memory && "Invalid access!"); |
| 190 | return Mem.Scale; |
| 191 | } |
| 192 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 193 | bool isToken() const {return Kind == Token; } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 194 | |
| 195 | bool isImm() const { return Kind == Immediate; } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 197 | bool isImmSExti16i8() const { |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 198 | if (!isImm()) |
| 199 | return false; |
| 200 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 201 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 202 | // handle it. |
| 203 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 204 | if (!CE) |
| 205 | return true; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 206 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 207 | // Otherwise, check the value is in a range that makes sense for this |
| 208 | // extension. |
| 209 | uint64_t Value = CE->getValue(); |
| 210 | return (( Value <= 0x000000000000007FULL)|| |
| 211 | (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)|| |
| 212 | (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 213 | } |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 214 | bool isImmSExti32i8() const { |
Daniel Dunbar | 1fe591d | 2010-05-20 20:20:39 +0000 | [diff] [blame] | 215 | if (!isImm()) |
| 216 | return false; |
| 217 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 218 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 219 | // handle it. |
| 220 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 221 | if (!CE) |
| 222 | return true; |
Daniel Dunbar | 1fe591d | 2010-05-20 20:20:39 +0000 | [diff] [blame] | 223 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 224 | // Otherwise, check the value is in a range that makes sense for this |
| 225 | // extension. |
| 226 | uint64_t Value = CE->getValue(); |
| 227 | return (( Value <= 0x000000000000007FULL)|| |
| 228 | (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)|| |
| 229 | (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
| 230 | } |
| 231 | bool isImmSExti64i8() const { |
| 232 | if (!isImm()) |
| 233 | return false; |
| 234 | |
| 235 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 236 | // handle it. |
| 237 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 238 | if (!CE) |
| 239 | return true; |
| 240 | |
| 241 | // Otherwise, check the value is in a range that makes sense for this |
| 242 | // extension. |
| 243 | uint64_t Value = CE->getValue(); |
| 244 | return (( Value <= 0x000000000000007FULL)|| |
| 245 | (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
| 246 | } |
| 247 | bool isImmSExti64i32() const { |
| 248 | if (!isImm()) |
| 249 | return false; |
| 250 | |
| 251 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 252 | // handle it. |
| 253 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 254 | if (!CE) |
| 255 | return true; |
| 256 | |
| 257 | // Otherwise, check the value is in a range that makes sense for this |
| 258 | // extension. |
| 259 | uint64_t Value = CE->getValue(); |
| 260 | return (( Value <= 0x000000007FFFFFFFULL)|| |
| 261 | (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
Daniel Dunbar | 1fe591d | 2010-05-20 20:20:39 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 264 | bool isMem() const { return Kind == Memory; } |
| 265 | |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 266 | bool isAbsMem() const { |
| 267 | return Kind == Memory && !getMemSegReg() && !getMemBaseReg() && |
Daniel Dunbar | 7b9147a | 2010-02-02 21:44:16 +0000 | [diff] [blame] | 268 | !getMemIndexReg() && getMemScale() == 1; |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 271 | bool isReg() const { return Kind == Register; } |
| 272 | |
Daniel Dunbar | 9c60f53 | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 273 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
| 274 | // Add as immediates when possible. |
| 275 | if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) |
| 276 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 277 | else |
| 278 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
| 279 | } |
| 280 | |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 281 | void addRegOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 282 | assert(N == 1 && "Invalid number of operands!"); |
| 283 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 284 | } |
| 285 | |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 286 | void addImmOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 287 | assert(N == 1 && "Invalid number of operands!"); |
Daniel Dunbar | 9c60f53 | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 288 | addExpr(Inst, getImm()); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 291 | void addMemOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | ec2b1f1 | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 292 | assert((N == 5) && "Invalid number of operands!"); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 293 | Inst.addOperand(MCOperand::CreateReg(getMemBaseReg())); |
| 294 | Inst.addOperand(MCOperand::CreateImm(getMemScale())); |
| 295 | Inst.addOperand(MCOperand::CreateReg(getMemIndexReg())); |
Daniel Dunbar | 9c60f53 | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 296 | addExpr(Inst, getMemDisp()); |
Daniel Dunbar | ec2b1f1 | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 297 | Inst.addOperand(MCOperand::CreateReg(getMemSegReg())); |
| 298 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 299 | |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 300 | void addAbsMemOperands(MCInst &Inst, unsigned N) const { |
| 301 | assert((N == 1) && "Invalid number of operands!"); |
| 302 | Inst.addOperand(MCOperand::CreateExpr(getMemDisp())); |
| 303 | } |
| 304 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 305 | static X86Operand *CreateToken(StringRef Str, SMLoc Loc) { |
| 306 | X86Operand *Res = new X86Operand(Token, Loc, Loc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 307 | Res->Tok.Data = Str.data(); |
| 308 | Res->Tok.Length = Str.size(); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 309 | return Res; |
| 310 | } |
| 311 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 312 | static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) { |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 313 | X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 314 | Res->Reg.RegNo = RegNo; |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 315 | return Res; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 316 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 317 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 318 | static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){ |
| 319 | X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 320 | Res->Imm.Val = Val; |
| 321 | return Res; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 322 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 324 | /// Create an absolute memory operand. |
| 325 | static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, |
| 326 | SMLoc EndLoc) { |
| 327 | X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc); |
| 328 | Res->Mem.SegReg = 0; |
| 329 | Res->Mem.Disp = Disp; |
| 330 | Res->Mem.BaseReg = 0; |
| 331 | Res->Mem.IndexReg = 0; |
Daniel Dunbar | 7b9147a | 2010-02-02 21:44:16 +0000 | [diff] [blame] | 332 | Res->Mem.Scale = 1; |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 333 | return Res; |
| 334 | } |
| 335 | |
| 336 | /// Create a generalized memory operand. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 337 | static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp, |
| 338 | unsigned BaseReg, unsigned IndexReg, |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 339 | unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) { |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 340 | // We should never just have a displacement, that should be parsed as an |
| 341 | // absolute memory operand. |
Daniel Dunbar | c09e411 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 342 | assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!"); |
| 343 | |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 344 | // The scale should always be one of {1,2,4,8}. |
| 345 | assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) && |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 346 | "Invalid scale!"); |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 347 | X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 348 | Res->Mem.SegReg = SegReg; |
| 349 | Res->Mem.Disp = Disp; |
| 350 | Res->Mem.BaseReg = BaseReg; |
| 351 | Res->Mem.IndexReg = IndexReg; |
| 352 | Res->Mem.Scale = Scale; |
| 353 | return Res; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 354 | } |
| 355 | }; |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 37dfdec | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 357 | } // end anonymous namespace. |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 358 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 360 | bool X86ATTAsmParser::ParseRegister(unsigned &RegNo, |
| 361 | SMLoc &StartLoc, SMLoc &EndLoc) { |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 362 | RegNo = 0; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 363 | const AsmToken &TokPercent = Parser.getTok(); |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 364 | assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!"); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 365 | StartLoc = TokPercent.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 366 | Parser.Lex(); // Eat percent token. |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 367 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 368 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 0d6cd00 | 2009-09-16 17:18:29 +0000 | [diff] [blame] | 369 | if (Tok.isNot(AsmToken::Identifier)) |
| 370 | return Error(Tok.getLoc(), "invalid register name"); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 372 | // FIXME: Validate register for the current architecture; we have to do |
| 373 | // validation later, so maybe there is no need for this here. |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 374 | RegNo = MatchRegisterName(Tok.getString()); |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 375 | |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 376 | // FIXME: This should be done using Requires<In32BitMode> and |
| 377 | // Requires<In64BitMode> so "eiz" usage in 64-bit instructions |
| 378 | // can be also checked. |
| 379 | if (RegNo == X86::RIZ && !Is64Bit) |
| 380 | return Error(Tok.getLoc(), "riz register in 64-bit mode only"); |
| 381 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 382 | // Parse %st(1) and "%st" as "%st(0)" |
| 383 | if (RegNo == 0 && Tok.getString() == "st") { |
| 384 | RegNo = X86::ST0; |
| 385 | EndLoc = Tok.getLoc(); |
| 386 | Parser.Lex(); // Eat 'st' |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 387 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 388 | // Check to see if we have '(4)' after %st. |
| 389 | if (getLexer().isNot(AsmToken::LParen)) |
| 390 | return false; |
| 391 | // Lex the paren. |
| 392 | getParser().Lex(); |
| 393 | |
| 394 | const AsmToken &IntTok = Parser.getTok(); |
| 395 | if (IntTok.isNot(AsmToken::Integer)) |
| 396 | return Error(IntTok.getLoc(), "expected stack index"); |
| 397 | switch (IntTok.getIntVal()) { |
| 398 | case 0: RegNo = X86::ST0; break; |
| 399 | case 1: RegNo = X86::ST1; break; |
| 400 | case 2: RegNo = X86::ST2; break; |
| 401 | case 3: RegNo = X86::ST3; break; |
| 402 | case 4: RegNo = X86::ST4; break; |
| 403 | case 5: RegNo = X86::ST5; break; |
| 404 | case 6: RegNo = X86::ST6; break; |
| 405 | case 7: RegNo = X86::ST7; break; |
| 406 | default: return Error(IntTok.getLoc(), "invalid stack index"); |
| 407 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 408 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 409 | if (getParser().Lex().isNot(AsmToken::RParen)) |
| 410 | return Error(Parser.getTok().getLoc(), "expected ')'"); |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 411 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 412 | EndLoc = Tok.getLoc(); |
| 413 | Parser.Lex(); // Eat ')' |
| 414 | return false; |
| 415 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 645b209 | 2010-06-24 07:29:18 +0000 | [diff] [blame] | 417 | // If this is "db[0-7]", match it as an alias |
| 418 | // for dr[0-7]. |
| 419 | if (RegNo == 0 && Tok.getString().size() == 3 && |
| 420 | Tok.getString().startswith("db")) { |
| 421 | switch (Tok.getString()[2]) { |
| 422 | case '0': RegNo = X86::DR0; break; |
| 423 | case '1': RegNo = X86::DR1; break; |
| 424 | case '2': RegNo = X86::DR2; break; |
| 425 | case '3': RegNo = X86::DR3; break; |
| 426 | case '4': RegNo = X86::DR4; break; |
| 427 | case '5': RegNo = X86::DR5; break; |
| 428 | case '6': RegNo = X86::DR6; break; |
| 429 | case '7': RegNo = X86::DR7; break; |
| 430 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 645b209 | 2010-06-24 07:29:18 +0000 | [diff] [blame] | 432 | if (RegNo != 0) { |
| 433 | EndLoc = Tok.getLoc(); |
| 434 | Parser.Lex(); // Eat it. |
| 435 | return false; |
| 436 | } |
| 437 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 438 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 439 | if (RegNo == 0) |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 440 | return Error(Tok.getLoc(), "invalid register name"); |
| 441 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 442 | EndLoc = Tok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 443 | Parser.Lex(); // Eat identifier token. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 444 | return false; |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 447 | X86Operand *X86ATTAsmParser::ParseOperand() { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 448 | switch (getLexer().getKind()) { |
| 449 | default: |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 450 | // Parse a memory operand with no segment register. |
| 451 | return ParseMemOperand(0, Parser.getTok().getLoc()); |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 452 | case AsmToken::Percent: { |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 453 | // Read the register. |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 454 | unsigned RegNo; |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 455 | SMLoc Start, End; |
| 456 | if (ParseRegister(RegNo, Start, End)) return 0; |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 457 | if (RegNo == X86::EIZ || RegNo == X86::RIZ) { |
| 458 | Error(Start, "eiz and riz can only be used as index registers"); |
| 459 | return 0; |
| 460 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 461 | |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 462 | // If this is a segment register followed by a ':', then this is the start |
| 463 | // of a memory reference, otherwise this is a normal register reference. |
| 464 | if (getLexer().isNot(AsmToken::Colon)) |
| 465 | return X86Operand::CreateReg(RegNo, Start, End); |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 466 | |
| 467 | |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 468 | getParser().Lex(); // Eat the colon. |
| 469 | return ParseMemOperand(RegNo, Start); |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 470 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 471 | case AsmToken::Dollar: { |
| 472 | // $42 -> immediate. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 473 | SMLoc Start = Parser.getTok().getLoc(), End; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 474 | Parser.Lex(); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 475 | const MCExpr *Val; |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 476 | if (getParser().ParseExpression(Val, End)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 477 | return 0; |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 478 | return X86Operand::CreateImm(Val, Start, End); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 479 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 480 | } |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 483 | /// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix |
| 484 | /// has already been parsed if present. |
| 485 | X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) { |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 486 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 487 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 488 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
Chris Lattner | 75f265f | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 489 | // only way to do this without lookahead is to eat the '(' and see what is |
| 490 | // after it. |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 491 | const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext()); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 492 | if (getLexer().isNot(AsmToken::LParen)) { |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 493 | SMLoc ExprEnd; |
| 494 | if (getParser().ParseExpression(Disp, ExprEnd)) return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 495 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 496 | // After parsing the base expression we could either have a parenthesized |
| 497 | // memory address or not. If not, return now. If so, eat the (. |
| 498 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | c09e411 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 499 | // Unless we have a segment register, treat this as an immediate. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 500 | if (SegReg == 0) |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 501 | return X86Operand::CreateMem(Disp, MemStart, ExprEnd); |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 502 | return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 503 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 504 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 505 | // Eat the '('. |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 506 | Parser.Lex(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 507 | } else { |
| 508 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 509 | // so we have to eat the ( to see beyond it. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 510 | SMLoc LParenLoc = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 511 | Parser.Lex(); // Eat the '('. |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 512 | |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 513 | if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 514 | // Nothing to do here, fall into the code below with the '(' part of the |
| 515 | // memory operand consumed. |
| 516 | } else { |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 517 | SMLoc ExprEnd; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 518 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 519 | // It must be an parenthesized expression, parse it now. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 520 | if (getParser().ParseParenExpression(Disp, ExprEnd)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 521 | return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 522 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 523 | // After parsing the base expression we could either have a parenthesized |
| 524 | // memory address or not. If not, return now. If so, eat the (. |
| 525 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | c09e411 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 526 | // Unless we have a segment register, treat this as an immediate. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 527 | if (SegReg == 0) |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 528 | return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd); |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 529 | return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 530 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 531 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 532 | // Eat the '('. |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 533 | Parser.Lex(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 536 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 537 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 538 | // the rest of the memory operand. |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 539 | unsigned BaseReg = 0, IndexReg = 0, Scale = 1; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 541 | if (getLexer().is(AsmToken::Percent)) { |
| 542 | SMLoc L; |
| 543 | if (ParseRegister(BaseReg, L, L)) return 0; |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 544 | if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) { |
| 545 | Error(L, "eiz and riz can only be used as index registers"); |
| 546 | return 0; |
| 547 | } |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 548 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 549 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 550 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 551 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 552 | |
| 553 | // Following the comma we should have either an index register, or a scale |
| 554 | // value. We don't support the later form, but we want to parse it |
| 555 | // correctly. |
| 556 | // |
| 557 | // Not that even though it would be completely consistent to support syntax |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 558 | // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this. |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 559 | if (getLexer().is(AsmToken::Percent)) { |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 560 | SMLoc L; |
| 561 | if (ParseRegister(IndexReg, L, L)) return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 562 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 563 | if (getLexer().isNot(AsmToken::RParen)) { |
| 564 | // Parse the scale amount: |
| 565 | // ::= ',' [scale-expression] |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 566 | if (getLexer().isNot(AsmToken::Comma)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 567 | Error(Parser.getTok().getLoc(), |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 568 | "expected comma in scale expression"); |
| 569 | return 0; |
| 570 | } |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 571 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 572 | |
| 573 | if (getLexer().isNot(AsmToken::RParen)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 574 | SMLoc Loc = Parser.getTok().getLoc(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 575 | |
| 576 | int64_t ScaleVal; |
| 577 | if (getParser().ParseAbsoluteExpression(ScaleVal)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 578 | return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 579 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 580 | // Validate the scale amount. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 581 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){ |
| 582 | Error(Loc, "scale factor in address must be 1, 2, 4 or 8"); |
| 583 | return 0; |
| 584 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 585 | Scale = (unsigned)ScaleVal; |
| 586 | } |
| 587 | } |
| 588 | } else if (getLexer().isNot(AsmToken::RParen)) { |
Daniel Dunbar | ee91025 | 2010-08-24 19:13:38 +0000 | [diff] [blame] | 589 | // A scale amount without an index is ignored. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 590 | // index. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 591 | SMLoc Loc = Parser.getTok().getLoc(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 592 | |
| 593 | int64_t Value; |
| 594 | if (getParser().ParseAbsoluteExpression(Value)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 595 | return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | ee91025 | 2010-08-24 19:13:38 +0000 | [diff] [blame] | 597 | if (Value != 1) |
| 598 | Warning(Loc, "scale factor without index register is ignored"); |
| 599 | Scale = 1; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 602 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 603 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 604 | if (getLexer().isNot(AsmToken::RParen)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 605 | Error(Parser.getTok().getLoc(), "unexpected token in memory operand"); |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 606 | return 0; |
| 607 | } |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 608 | SMLoc MemEnd = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 609 | Parser.Lex(); // Eat the ')'. |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 611 | return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, |
| 612 | MemStart, MemEnd); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 615 | bool X86ATTAsmParser:: |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 616 | ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 617 | SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Daniel Dunbar | 1b6c060 | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 618 | // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to |
| 619 | // represent alternative syntaxes in the .td file, without requiring |
| 620 | // instruction duplication. |
| 621 | StringRef PatchedName = StringSwitch<StringRef>(Name) |
| 622 | .Case("sal", "shl") |
| 623 | .Case("salb", "shlb") |
| 624 | .Case("sall", "shll") |
| 625 | .Case("salq", "shlq") |
| 626 | .Case("salw", "shlw") |
| 627 | .Case("repe", "rep") |
| 628 | .Case("repz", "rep") |
| 629 | .Case("repnz", "repne") |
Chris Lattner | ba8cea4 | 2010-09-08 05:38:31 +0000 | [diff] [blame] | 630 | .Case("iret", "iretl") |
Chris Lattner | ba8e81c | 2010-09-08 05:45:34 +0000 | [diff] [blame] | 631 | .Case("sysret", "sysretl") |
Chris Lattner | d68c474 | 2010-09-06 23:40:56 +0000 | [diff] [blame] | 632 | .Case("push", Is64Bit ? "pushq" : "pushl") |
Chris Lattner | 373c458 | 2010-09-08 22:13:08 +0000 | [diff] [blame] | 633 | .Case("pop", Is64Bit ? "popq" : "popl") |
Dan Gohman | e5e4ff9 | 2010-05-20 16:16:00 +0000 | [diff] [blame] | 634 | .Case("pushf", Is64Bit ? "pushfq" : "pushfl") |
| 635 | .Case("popf", Is64Bit ? "popfq" : "popfl") |
Kevin Enderby | 9d31d79 | 2010-05-21 23:01:38 +0000 | [diff] [blame] | 636 | .Case("retl", Is64Bit ? "retl" : "ret") |
| 637 | .Case("retq", Is64Bit ? "ret" : "retq") |
Daniel Dunbar | 4c36197 | 2010-05-22 06:37:33 +0000 | [diff] [blame] | 638 | .Case("setz", "sete") |
| 639 | .Case("setnz", "setne") |
| 640 | .Case("jz", "je") |
| 641 | .Case("jnz", "jne") |
Kevin Enderby | bd65891 | 2010-05-27 21:33:19 +0000 | [diff] [blame] | 642 | .Case("jc", "jb") |
Kevin Enderby | bd65891 | 2010-05-27 21:33:19 +0000 | [diff] [blame] | 643 | .Case("jna", "jbe") |
| 644 | .Case("jnae", "jb") |
| 645 | .Case("jnb", "jae") |
| 646 | .Case("jnbe", "ja") |
| 647 | .Case("jnc", "jae") |
| 648 | .Case("jng", "jle") |
| 649 | .Case("jnge", "jl") |
| 650 | .Case("jnl", "jge") |
| 651 | .Case("jnle", "jg") |
| 652 | .Case("jpe", "jp") |
| 653 | .Case("jpo", "jnp") |
Chris Lattner | e9e0fc5 | 2010-09-07 00:05:45 +0000 | [diff] [blame] | 654 | // Condition code aliases for 16-bit, 32-bit, 64-bit and unspec operands. |
| 655 | .Case("cmovcw", "cmovbw") .Case("cmovcl", "cmovbl") |
| 656 | .Case("cmovcq", "cmovbq") .Case("cmovc", "cmovb") |
| 657 | .Case("cmovnaw", "cmovbew").Case("cmovnal", "cmovbel") |
| 658 | .Case("cmovnaq", "cmovbeq").Case("cmovna", "cmovbe") |
| 659 | .Case("cmovnbw", "cmovaew").Case("cmovnbl", "cmovael") |
| 660 | .Case("cmovnbq", "cmovaeq").Case("cmovnb", "cmovae") |
| 661 | .Case("cmovnbew","cmovaw") .Case("cmovnbel","cmoval") |
| 662 | .Case("cmovnbeq","cmovaq") .Case("cmovnbe", "cmova") |
| 663 | .Case("cmovncw", "cmovaew").Case("cmovncl", "cmovael") |
| 664 | .Case("cmovncq", "cmovaeq").Case("cmovnc", "cmovae") |
| 665 | .Case("cmovngw", "cmovlew").Case("cmovngl", "cmovlel") |
| 666 | .Case("cmovngq", "cmovleq").Case("cmovng", "cmovle") |
| 667 | .Case("cmovnw", "cmovgew").Case("cmovnl", "cmovgel") |
| 668 | .Case("cmovnq", "cmovgeq").Case("cmovn", "cmovge") |
| 669 | .Case("cmovngw", "cmovlew").Case("cmovngl", "cmovlel") |
| 670 | .Case("cmovngq", "cmovleq").Case("cmovng", "cmovle") |
| 671 | .Case("cmovngew","cmovlw") .Case("cmovngel","cmovll") |
| 672 | .Case("cmovngeq","cmovlq") .Case("cmovnge", "cmovl") |
| 673 | .Case("cmovnlw", "cmovgew").Case("cmovnll", "cmovgel") |
| 674 | .Case("cmovnlq", "cmovgeq").Case("cmovnl", "cmovge") |
| 675 | .Case("cmovnlew","cmovgw") .Case("cmovnlel","cmovgl") |
| 676 | .Case("cmovnleq","cmovgq") .Case("cmovnle", "cmovg") |
| 677 | .Case("cmovnzw", "cmovnew").Case("cmovnzl", "cmovnel") |
| 678 | .Case("cmovnzq", "cmovneq").Case("cmovnz", "cmovne") |
| 679 | .Case("cmovzw", "cmovew") .Case("cmovzl", "cmovel") |
| 680 | .Case("cmovzq", "cmoveq") .Case("cmovz", "cmove") |
Kevin Enderby | 5e39442 | 2010-05-28 20:59:10 +0000 | [diff] [blame] | 681 | .Case("fwait", "wait") |
Kevin Enderby | 31cc965 | 2010-05-28 21:20:21 +0000 | [diff] [blame] | 682 | .Case("movzx", "movzb") |
Daniel Dunbar | 1b6c060 | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 683 | .Default(Name); |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 684 | |
| 685 | // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}. |
| 686 | const MCExpr *ExtraImmOp = 0; |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 687 | if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) && |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 688 | (PatchedName.endswith("ss") || PatchedName.endswith("sd") || |
| 689 | PatchedName.endswith("ps") || PatchedName.endswith("pd"))) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 690 | bool IsVCMP = PatchedName.startswith("vcmp"); |
| 691 | unsigned SSECCIdx = IsVCMP ? 4 : 3; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 692 | unsigned SSEComparisonCode = StringSwitch<unsigned>( |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 693 | PatchedName.slice(SSECCIdx, PatchedName.size() - 2)) |
Bruno Cardoso Lopes | cc69e13 | 2010-07-07 22:24:03 +0000 | [diff] [blame] | 694 | .Case("eq", 0) |
| 695 | .Case("lt", 1) |
| 696 | .Case("le", 2) |
| 697 | .Case("unord", 3) |
| 698 | .Case("neq", 4) |
| 699 | .Case("nlt", 5) |
| 700 | .Case("nle", 6) |
| 701 | .Case("ord", 7) |
| 702 | .Case("eq_uq", 8) |
| 703 | .Case("nge", 9) |
| 704 | .Case("ngt", 0x0A) |
| 705 | .Case("false", 0x0B) |
| 706 | .Case("neq_oq", 0x0C) |
| 707 | .Case("ge", 0x0D) |
| 708 | .Case("gt", 0x0E) |
| 709 | .Case("true", 0x0F) |
| 710 | .Case("eq_os", 0x10) |
| 711 | .Case("lt_oq", 0x11) |
| 712 | .Case("le_oq", 0x12) |
| 713 | .Case("unord_s", 0x13) |
| 714 | .Case("neq_us", 0x14) |
| 715 | .Case("nlt_uq", 0x15) |
| 716 | .Case("nle_uq", 0x16) |
| 717 | .Case("ord_s", 0x17) |
| 718 | .Case("eq_us", 0x18) |
| 719 | .Case("nge_uq", 0x19) |
| 720 | .Case("ngt_uq", 0x1A) |
| 721 | .Case("false_os", 0x1B) |
| 722 | .Case("neq_os", 0x1C) |
| 723 | .Case("ge_oq", 0x1D) |
| 724 | .Case("gt_oq", 0x1E) |
| 725 | .Case("true_us", 0x1F) |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 726 | .Default(~0U); |
| 727 | if (SSEComparisonCode != ~0U) { |
| 728 | ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode, |
| 729 | getParser().getContext()); |
| 730 | if (PatchedName.endswith("ss")) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 731 | PatchedName = IsVCMP ? "vcmpss" : "cmpss"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 732 | } else if (PatchedName.endswith("sd")) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 733 | PatchedName = IsVCMP ? "vcmpsd" : "cmpsd"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 734 | } else if (PatchedName.endswith("ps")) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 735 | PatchedName = IsVCMP ? "vcmpps" : "cmpps"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 736 | } else { |
| 737 | assert(PatchedName.endswith("pd") && "Unexpected mnemonic!"); |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 738 | PatchedName = IsVCMP ? "vcmppd" : "cmppd"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
Bruno Cardoso Lopes | f528d2b | 2010-07-23 18:41:12 +0000 | [diff] [blame] | 742 | |
| 743 | // FIXME: Hack to recognize vpclmul<src1_quadword, src2_quadword>dq |
| 744 | if (PatchedName.startswith("vpclmul")) { |
| 745 | unsigned CLMULQuadWordSelect = StringSwitch<unsigned>( |
| 746 | PatchedName.slice(7, PatchedName.size() - 2)) |
| 747 | .Case("lqlq", 0x00) // src1[63:0], src2[63:0] |
| 748 | .Case("hqlq", 0x01) // src1[127:64], src2[63:0] |
| 749 | .Case("lqhq", 0x10) // src1[63:0], src2[127:64] |
| 750 | .Case("hqhq", 0x11) // src1[127:64], src2[127:64] |
| 751 | .Default(~0U); |
| 752 | if (CLMULQuadWordSelect != ~0U) { |
| 753 | ExtraImmOp = MCConstantExpr::Create(CLMULQuadWordSelect, |
| 754 | getParser().getContext()); |
| 755 | assert(PatchedName.endswith("dq") && "Unexpected mnemonic!"); |
| 756 | PatchedName = "vpclmulqdq"; |
| 757 | } |
| 758 | } |
Chris Lattner | 9607c40 | 2010-09-08 04:53:27 +0000 | [diff] [blame] | 759 | |
Daniel Dunbar | 1b6c060 | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 760 | Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc)); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 761 | |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 762 | if (ExtraImmOp) |
| 763 | Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc)); |
Chris Lattner | 47ab90b | 2010-09-06 18:32:06 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 2544f42 | 2010-09-08 05:17:37 +0000 | [diff] [blame] | 765 | |
| 766 | // Determine whether this is an instruction prefix. |
| 767 | bool isPrefix = |
| 768 | PatchedName == "lock" || PatchedName == "rep" || |
| 769 | PatchedName == "repne"; |
| 770 | |
| 771 | |
| 772 | // This does the actual operand parsing. Don't parse any more if we have a |
| 773 | // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we |
| 774 | // just want to parse the "lock" as the first instruction and the "incl" as |
| 775 | // the next one. |
| 776 | if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) { |
Daniel Dunbar | 0db68f4 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 777 | |
| 778 | // Parse '*' modifier. |
| 779 | if (getLexer().is(AsmToken::Star)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 780 | SMLoc Loc = Parser.getTok().getLoc(); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 781 | Operands.push_back(X86Operand::CreateToken("*", Loc)); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 782 | Parser.Lex(); // Eat the star. |
Daniel Dunbar | 0db68f4 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 785 | // Read the first operand. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 786 | if (X86Operand *Op = ParseOperand()) |
| 787 | Operands.push_back(Op); |
| 788 | else |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 789 | return true; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 790 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 791 | while (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 792 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 793 | |
| 794 | // Parse and remember the operand. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 795 | if (X86Operand *Op = ParseOperand()) |
| 796 | Operands.push_back(Op); |
| 797 | else |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 798 | return true; |
| 799 | } |
Chris Lattner | 2544f42 | 2010-09-08 05:17:37 +0000 | [diff] [blame] | 800 | |
| 801 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 802 | return TokError("unexpected token in argument list"); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | 34e5314 | 2010-09-08 05:10:46 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 2544f42 | 2010-09-08 05:17:37 +0000 | [diff] [blame] | 805 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 806 | Parser.Lex(); // Consume the EndOfStatement |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 807 | |
Daniel Dunbar | d5e7705 | 2010-03-13 00:47:29 +0000 | [diff] [blame] | 808 | // FIXME: Hack to handle recognizing s{hr,ar,hl}? $1. |
| 809 | if ((Name.startswith("shr") || Name.startswith("sar") || |
| 810 | Name.startswith("shl")) && |
Chris Lattner | 47ab90b | 2010-09-06 18:32:06 +0000 | [diff] [blame] | 811 | Operands.size() == 3) { |
| 812 | X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]); |
| 813 | if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) && |
| 814 | cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) { |
| 815 | delete Operands[1]; |
| 816 | Operands.erase(Operands.begin() + 1); |
| 817 | } |
Daniel Dunbar | f2de13f | 2010-03-20 22:36:38 +0000 | [diff] [blame] | 818 | } |
Daniel Dunbar | d5e7705 | 2010-03-13 00:47:29 +0000 | [diff] [blame] | 819 | |
Kevin Enderby | cf50a53 | 2010-05-25 20:52:34 +0000 | [diff] [blame] | 820 | // FIXME: Hack to handle "f{mul*,add*,sub*,div*} $op, st(0)" the same as |
| 821 | // "f{mul*,add*,sub*,div*} $op" |
| 822 | if ((Name.startswith("fmul") || Name.startswith("fadd") || |
| 823 | Name.startswith("fsub") || Name.startswith("fdiv")) && |
| 824 | Operands.size() == 3 && |
| 825 | static_cast<X86Operand*>(Operands[2])->isReg() && |
| 826 | static_cast<X86Operand*>(Operands[2])->getReg() == X86::ST0) { |
| 827 | delete Operands[2]; |
| 828 | Operands.erase(Operands.begin() + 2); |
| 829 | } |
| 830 | |
Daniel Dunbar | fba88d4 | 2010-08-24 19:37:56 +0000 | [diff] [blame] | 831 | // FIXME: Hack to handle "imul <imm>, B" which is an alias for "imul <imm>, B, |
| 832 | // B". |
Daniel Dunbar | ae528f6 | 2010-08-24 19:24:18 +0000 | [diff] [blame] | 833 | if (Name.startswith("imul") && Operands.size() == 3 && |
Daniel Dunbar | fba88d4 | 2010-08-24 19:37:56 +0000 | [diff] [blame] | 834 | static_cast<X86Operand*>(Operands[1])->isImm() && |
Daniel Dunbar | ae528f6 | 2010-08-24 19:24:18 +0000 | [diff] [blame] | 835 | static_cast<X86Operand*>(Operands.back())->isReg()) { |
| 836 | X86Operand *Op = static_cast<X86Operand*>(Operands.back()); |
| 837 | Operands.push_back(X86Operand::CreateReg(Op->getReg(), Op->getStartLoc(), |
| 838 | Op->getEndLoc())); |
| 839 | } |
Chris Lattner | c5cebeb | 2010-09-06 23:51:44 +0000 | [diff] [blame] | 840 | |
| 841 | // 'sldt <mem>' can be encoded with either sldtw or sldtq with the same |
| 842 | // effect (both store to a 16-bit mem). Force to sldtw to avoid ambiguity |
| 843 | // errors, since its encoding is the most compact. |
| 844 | if (Name == "sldt" && Operands.size() == 2 && |
Benjamin Kramer | aceeb3a | 2010-09-07 14:40:58 +0000 | [diff] [blame] | 845 | static_cast<X86Operand*>(Operands[1])->isMem()) { |
| 846 | delete Operands[0]; |
Chris Lattner | c5cebeb | 2010-09-06 23:51:44 +0000 | [diff] [blame] | 847 | Operands[0] = X86Operand::CreateToken("sldtw", NameLoc); |
Benjamin Kramer | aceeb3a | 2010-09-07 14:40:58 +0000 | [diff] [blame] | 848 | } |
Chris Lattner | 9607c40 | 2010-09-08 04:53:27 +0000 | [diff] [blame] | 849 | |
| 850 | // The assembler accepts "xchgX <reg>, <mem>" and "xchgX <mem>, <reg>" as |
| 851 | // synonyms. Our tables only have the "<reg>, <mem>" form, so if we see the |
| 852 | // other operand order, swap them. |
Chris Lattner | 90b5454 | 2010-09-08 22:27:05 +0000 | [diff] [blame^] | 853 | if (Name == "xchgb" || Name == "xchgw" || Name == "xchgl" || Name == "xchgq"|| |
| 854 | Name == "xchg") |
Chris Lattner | 9607c40 | 2010-09-08 04:53:27 +0000 | [diff] [blame] | 855 | if (Operands.size() == 3 && |
| 856 | static_cast<X86Operand*>(Operands[1])->isMem() && |
| 857 | static_cast<X86Operand*>(Operands[2])->isReg()) { |
| 858 | std::swap(Operands[1], Operands[2]); |
| 859 | } |
Daniel Dunbar | ae528f6 | 2010-08-24 19:24:18 +0000 | [diff] [blame] | 860 | |
Chris Lattner | c8ae35a | 2010-09-08 05:51:12 +0000 | [diff] [blame] | 861 | // The assembler accepts "testX <reg>, <mem>" and "testX <mem>, <reg>" as |
| 862 | // synonyms. Our tables only have the "<mem>, <reg>" form, so if we see the |
| 863 | // other operand order, swap them. |
Chris Lattner | 90b5454 | 2010-09-08 22:27:05 +0000 | [diff] [blame^] | 864 | if (Name == "testb" || Name == "testw" || Name == "testl" || Name == "testq"|| |
| 865 | Name == "test") |
Chris Lattner | c8ae35a | 2010-09-08 05:51:12 +0000 | [diff] [blame] | 866 | if (Operands.size() == 3 && |
| 867 | static_cast<X86Operand*>(Operands[1])->isReg() && |
| 868 | static_cast<X86Operand*>(Operands[2])->isMem()) { |
| 869 | std::swap(Operands[1], Operands[2]); |
| 870 | } |
| 871 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 872 | return false; |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 875 | bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 876 | StringRef IDVal = DirectiveID.getIdentifier(); |
| 877 | if (IDVal == ".word") |
| 878 | return ParseDirectiveWord(2, DirectiveID.getLoc()); |
| 879 | return true; |
| 880 | } |
| 881 | |
| 882 | /// ParseDirectiveWord |
| 883 | /// ::= .word [ expression (, expression)* ] |
| 884 | bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { |
| 885 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 886 | for (;;) { |
| 887 | const MCExpr *Value; |
| 888 | if (getParser().ParseExpression(Value)) |
| 889 | return true; |
| 890 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 891 | getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 892 | |
| 893 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 894 | break; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 895 | |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 896 | // FIXME: Improve diagnostic. |
| 897 | if (getLexer().isNot(AsmToken::Comma)) |
| 898 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 899 | Parser.Lex(); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 903 | Parser.Lex(); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 904 | return false; |
| 905 | } |
| 906 | |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 907 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 908 | bool |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 909 | X86ATTAsmParser::MatchInstruction(SMLoc IDLoc, |
| 910 | const SmallVectorImpl<MCParsedAsmOperand*> |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 911 | &Operands, |
| 912 | MCInst &Inst) { |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 913 | assert(!Operands.empty() && "Unexpect empty operand list!"); |
| 914 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 915 | bool WasOriginallyInvalidOperand = false; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 916 | unsigned OrigErrorInfo; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 917 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 918 | // First, try a direct match. |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 919 | switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) { |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 920 | case Match_Success: |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 921 | return false; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 922 | case Match_MissingFeature: |
| 923 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 924 | return true; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 925 | case Match_InvalidOperand: |
| 926 | WasOriginallyInvalidOperand = true; |
| 927 | break; |
| 928 | case Match_MnemonicFail: |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 929 | break; |
| 930 | } |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 931 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 932 | // FIXME: Ideally, we would only attempt suffix matches for things which are |
| 933 | // valid prefixes, and we could just infer the right unambiguous |
| 934 | // type. However, that requires substantially more matcher support than the |
| 935 | // following hack. |
| 936 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 937 | X86Operand *Op = static_cast<X86Operand*>(Operands[0]); |
| 938 | assert(Op->isToken() && "Leading operand should always be a mnemonic!"); |
| 939 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 940 | // Change the operand to point to a temporary token. |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 941 | StringRef Base = Op->getToken(); |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 942 | SmallString<16> Tmp; |
| 943 | Tmp += Base; |
| 944 | Tmp += ' '; |
| 945 | Op->setTokenValue(Tmp.str()); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 946 | |
| 947 | // Check for the various suffix matches. |
| 948 | Tmp[Base.size()] = 'b'; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 949 | unsigned BErrorInfo, WErrorInfo, LErrorInfo, QErrorInfo; |
| 950 | MatchResultTy MatchB = MatchInstructionImpl(Operands, Inst, BErrorInfo); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 951 | Tmp[Base.size()] = 'w'; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 952 | MatchResultTy MatchW = MatchInstructionImpl(Operands, Inst, WErrorInfo); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 953 | Tmp[Base.size()] = 'l'; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 954 | MatchResultTy MatchL = MatchInstructionImpl(Operands, Inst, LErrorInfo); |
Daniel Dunbar | 0481449 | 2010-05-12 00:54:20 +0000 | [diff] [blame] | 955 | Tmp[Base.size()] = 'q'; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 956 | MatchResultTy MatchQ = MatchInstructionImpl(Operands, Inst, QErrorInfo); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 957 | |
| 958 | // Restore the old token. |
| 959 | Op->setTokenValue(Base); |
| 960 | |
| 961 | // If exactly one matched, then we treat that as a successful match (and the |
| 962 | // instruction will already have been filled in correctly, since the failing |
| 963 | // matches won't have modified it). |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 964 | unsigned NumSuccessfulMatches = |
| 965 | (MatchB == Match_Success) + (MatchW == Match_Success) + |
| 966 | (MatchL == Match_Success) + (MatchQ == Match_Success); |
| 967 | if (NumSuccessfulMatches == 1) |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 968 | return false; |
| 969 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 970 | // Otherwise, the match failed, try to produce a decent error message. |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 971 | |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 972 | // If we had multiple suffix matches, then identify this as an ambiguous |
| 973 | // match. |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 974 | if (NumSuccessfulMatches > 1) { |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 975 | char MatchChars[4]; |
| 976 | unsigned NumMatches = 0; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 977 | if (MatchB == Match_Success) |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 978 | MatchChars[NumMatches++] = 'b'; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 979 | if (MatchW == Match_Success) |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 980 | MatchChars[NumMatches++] = 'w'; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 981 | if (MatchL == Match_Success) |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 982 | MatchChars[NumMatches++] = 'l'; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 983 | if (MatchQ == Match_Success) |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 984 | MatchChars[NumMatches++] = 'q'; |
| 985 | |
| 986 | SmallString<126> Msg; |
| 987 | raw_svector_ostream OS(Msg); |
| 988 | OS << "ambiguous instructions require an explicit suffix (could be "; |
| 989 | for (unsigned i = 0; i != NumMatches; ++i) { |
| 990 | if (i != 0) |
| 991 | OS << ", "; |
| 992 | if (i + 1 == NumMatches) |
| 993 | OS << "or "; |
| 994 | OS << "'" << Base << MatchChars[i] << "'"; |
| 995 | } |
| 996 | OS << ")"; |
| 997 | Error(IDLoc, OS.str()); |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 998 | return true; |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 999 | } |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1001 | // Okay, we know that none of the variants matched successfully. |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1003 | // If all of the instructions reported an invalid mnemonic, then the original |
| 1004 | // mnemonic was invalid. |
| 1005 | if ((MatchB == Match_MnemonicFail) && (MatchW == Match_MnemonicFail) && |
| 1006 | (MatchL == Match_MnemonicFail) && (MatchQ == Match_MnemonicFail)) { |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1007 | if (!WasOriginallyInvalidOperand) { |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1008 | Error(IDLoc, "invalid instruction mnemonic '" + Base + "'"); |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | // Recover location info for the operand if we know which was the problem. |
| 1013 | SMLoc ErrorLoc = IDLoc; |
| 1014 | if (OrigErrorInfo != ~0U) { |
| 1015 | ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc(); |
| 1016 | if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc; |
| 1017 | } |
| 1018 | |
| 1019 | Error(ErrorLoc, "invalid operand for instruction"); |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1020 | return true; |
| 1021 | } |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1022 | |
| 1023 | // If one instruction matched with a missing feature, report this as a |
| 1024 | // missing feature. |
| 1025 | if ((MatchB == Match_MissingFeature) + (MatchW == Match_MissingFeature) + |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1026 | (MatchL == Match_MissingFeature) + (MatchQ == Match_MissingFeature) == 1){ |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1027 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1031 | // If one instruction matched with an invalid operand, report this as an |
| 1032 | // operand failure. |
| 1033 | if ((MatchB == Match_InvalidOperand) + (MatchW == Match_InvalidOperand) + |
| 1034 | (MatchL == Match_InvalidOperand) + (MatchQ == Match_InvalidOperand) == 1){ |
| 1035 | Error(IDLoc, "invalid operand for instruction"); |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1039 | // If all of these were an outright failure, report it in a useless way. |
| 1040 | // FIXME: We should give nicer diagnostics about the exact failure. |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1041 | Error(IDLoc, "unknown use of instruction mnemonic without a size suffix"); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 1042 | return true; |
| 1043 | } |
| 1044 | |
| 1045 | |
Sean Callanan | e88f552 | 2010-01-23 02:43:15 +0000 | [diff] [blame] | 1046 | extern "C" void LLVMInitializeX86AsmLexer(); |
| 1047 | |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 1048 | // Force static initialization. |
| 1049 | extern "C" void LLVMInitializeX86AsmParser() { |
Daniel Dunbar | f98bc63 | 2010-03-18 20:06:02 +0000 | [diff] [blame] | 1050 | RegisterAsmParser<X86_32ATTAsmParser> X(TheX86_32Target); |
| 1051 | RegisterAsmParser<X86_64ATTAsmParser> Y(TheX86_64Target); |
Sean Callanan | e88f552 | 2010-01-23 02:43:15 +0000 | [diff] [blame] | 1052 | LLVMInitializeX86AsmLexer(); |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 1053 | } |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1055 | #define GET_REGISTER_MATCHER |
| 1056 | #define GET_MATCHER_IMPLEMENTATION |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 1057 | #include "X86GenAsmMatcher.inc" |