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 | |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/X86BaseInfo.h" |
| 11 | #include "llvm/MC/MCTargetAsmParser.h" |
Chris Lattner | 33d60d5 | 2010-09-22 04:11:10 +0000 | [diff] [blame] | 12 | #include "llvm/Target/TargetRegistry.h" |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCInst.h" |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSubtargetInfo.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 18 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 19 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Benjamin Kramer | 75ca4b9 | 2011-07-08 21:06:23 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 33d60d5 | 2010-09-22 04:11:10 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/StringSwitch.h" |
| 25 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 28 | |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
Benjamin Kramer | c6b79ac | 2009-07-31 11:35:26 +0000 | [diff] [blame] | 32 | struct X86Operand; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 33 | |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 34 | class X86ATTAsmParser : public MCTargetAsmParser { |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 35 | MCSubtargetInfo &STI; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 36 | MCAsmParser &Parser; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 38 | private: |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 39 | MCAsmParser &getParser() const { return Parser; } |
| 40 | |
| 41 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 42 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 43 | bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } |
| 44 | |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 45 | X86Operand *ParseOperand(); |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 46 | X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 47 | |
| 48 | bool ParseDirectiveWord(unsigned Size, SMLoc L); |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame^] | 49 | bool ParseDirectiveCode(StringRef IDVal, SMLoc L); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 51 | bool MatchAndEmitInstruction(SMLoc IDLoc, |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 52 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 53 | MCStreamer &Out); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 54 | |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 55 | /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi) |
| 56 | /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode. |
| 57 | bool isSrcOp(X86Operand &Op); |
| 58 | |
| 59 | /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode |
| 60 | /// or %es:(%edi) in 32bit mode. |
| 61 | bool isDstOp(X86Operand &Op); |
| 62 | |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 63 | bool is64BitMode() const { |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 64 | // FIXME: Can tablegen auto-generate this? |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 65 | return (STI.getFeatureBits() & X86::Mode64Bit) != 0; |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 66 | } |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame^] | 67 | void SwitchMode() { |
| 68 | unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit)); |
| 69 | setAvailableFeatures(FB); |
| 70 | } |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 72 | /// @name Auto-generated Matcher Functions |
| 73 | /// { |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 75 | #define GET_ASSEMBLER_HEADER |
| 76 | #include "X86GenAsmMatcher.inc" |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 78 | /// } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 79 | |
| 80 | public: |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 81 | X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser) |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 82 | : MCTargetAsmParser(), STI(sti), Parser(parser) { |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 84 | // Initialize the set of available features. |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 85 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 86 | } |
Roman Divacky | bf75532 | 2011-01-27 17:14:22 +0000 | [diff] [blame] | 87 | virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 88 | |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 89 | virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 90 | SmallVectorImpl<MCParsedAsmOperand*> &Operands); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 91 | |
| 92 | virtual bool ParseDirective(AsmToken DirectiveID); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 93 | }; |
Chris Lattner | 37dfdec | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 94 | } // end anonymous namespace |
| 95 | |
Sean Callanan | e9b466d | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 96 | /// @name Auto-generated Match Functions |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 97 | /// { |
Sean Callanan | e9b466d | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 98 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 99 | static unsigned MatchRegisterName(StringRef Name); |
Sean Callanan | e9b466d | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 100 | |
| 101 | /// } |
Chris Lattner | 37dfdec | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 102 | |
| 103 | namespace { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 104 | |
| 105 | /// X86Operand - Instances of this class represent a parsed X86 machine |
| 106 | /// instruction. |
Chris Lattner | 45220a8 | 2010-01-14 21:20:55 +0000 | [diff] [blame] | 107 | struct X86Operand : public MCParsedAsmOperand { |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 108 | enum KindTy { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 109 | Token, |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 110 | Register, |
| 111 | Immediate, |
| 112 | Memory |
| 113 | } Kind; |
| 114 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 115 | SMLoc StartLoc, EndLoc; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 117 | union { |
| 118 | struct { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 119 | const char *Data; |
| 120 | unsigned Length; |
| 121 | } Tok; |
| 122 | |
| 123 | struct { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 124 | unsigned RegNo; |
| 125 | } Reg; |
| 126 | |
| 127 | struct { |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 128 | const MCExpr *Val; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 129 | } Imm; |
| 130 | |
| 131 | struct { |
| 132 | unsigned SegReg; |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 133 | const MCExpr *Disp; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 134 | unsigned BaseReg; |
| 135 | unsigned IndexReg; |
| 136 | unsigned Scale; |
| 137 | } Mem; |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 138 | }; |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 140 | X86Operand(KindTy K, SMLoc Start, SMLoc End) |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 141 | : Kind(K), StartLoc(Start), EndLoc(End) {} |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 143 | /// getStartLoc - Get the location of the first token of this operand. |
| 144 | SMLoc getStartLoc() const { return StartLoc; } |
| 145 | /// getEndLoc - Get the location of the last token of this operand. |
| 146 | SMLoc getEndLoc() const { return EndLoc; } |
| 147 | |
Jim Grosbach | b7f689b | 2011-07-13 15:34:57 +0000 | [diff] [blame] | 148 | virtual void print(raw_ostream &OS) const {} |
Daniel Dunbar | b3cb696 | 2010-08-11 06:37:04 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 150 | StringRef getToken() const { |
| 151 | assert(Kind == Token && "Invalid access!"); |
| 152 | return StringRef(Tok.Data, Tok.Length); |
| 153 | } |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 154 | void setTokenValue(StringRef Value) { |
| 155 | assert(Kind == Token && "Invalid access!"); |
| 156 | Tok.Data = Value.data(); |
| 157 | Tok.Length = Value.size(); |
| 158 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 159 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 160 | unsigned getReg() const { |
| 161 | assert(Kind == Register && "Invalid access!"); |
| 162 | return Reg.RegNo; |
| 163 | } |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 164 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 165 | const MCExpr *getImm() const { |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 166 | assert(Kind == Immediate && "Invalid access!"); |
| 167 | return Imm.Val; |
| 168 | } |
| 169 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 170 | const MCExpr *getMemDisp() const { |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 171 | assert(Kind == Memory && "Invalid access!"); |
| 172 | return Mem.Disp; |
| 173 | } |
| 174 | unsigned getMemSegReg() const { |
| 175 | assert(Kind == Memory && "Invalid access!"); |
| 176 | return Mem.SegReg; |
| 177 | } |
| 178 | unsigned getMemBaseReg() const { |
| 179 | assert(Kind == Memory && "Invalid access!"); |
| 180 | return Mem.BaseReg; |
| 181 | } |
| 182 | unsigned getMemIndexReg() const { |
| 183 | assert(Kind == Memory && "Invalid access!"); |
| 184 | return Mem.IndexReg; |
| 185 | } |
| 186 | unsigned getMemScale() const { |
| 187 | assert(Kind == Memory && "Invalid access!"); |
| 188 | return Mem.Scale; |
| 189 | } |
| 190 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 191 | bool isToken() const {return Kind == Token; } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 192 | |
| 193 | bool isImm() const { return Kind == Immediate; } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 195 | bool isImmSExti16i8() const { |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 196 | if (!isImm()) |
| 197 | return false; |
| 198 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 199 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 200 | // handle it. |
| 201 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 202 | if (!CE) |
| 203 | return true; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 204 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 205 | // Otherwise, check the value is in a range that makes sense for this |
| 206 | // extension. |
| 207 | uint64_t Value = CE->getValue(); |
| 208 | return (( Value <= 0x000000000000007FULL)|| |
| 209 | (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)|| |
| 210 | (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 212 | bool isImmSExti32i8() const { |
Daniel Dunbar | 1fe591d | 2010-05-20 20:20:39 +0000 | [diff] [blame] | 213 | if (!isImm()) |
| 214 | return false; |
| 215 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 216 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 217 | // handle it. |
| 218 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 219 | if (!CE) |
| 220 | return true; |
Daniel Dunbar | 1fe591d | 2010-05-20 20:20:39 +0000 | [diff] [blame] | 221 | |
Daniel Dunbar | 62e4c67 | 2010-05-22 21:02:33 +0000 | [diff] [blame] | 222 | // Otherwise, check the value is in a range that makes sense for this |
| 223 | // extension. |
| 224 | uint64_t Value = CE->getValue(); |
| 225 | return (( Value <= 0x000000000000007FULL)|| |
| 226 | (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)|| |
| 227 | (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
| 228 | } |
| 229 | bool isImmSExti64i8() const { |
| 230 | if (!isImm()) |
| 231 | return false; |
| 232 | |
| 233 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 234 | // handle it. |
| 235 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 236 | if (!CE) |
| 237 | return true; |
| 238 | |
| 239 | // Otherwise, check the value is in a range that makes sense for this |
| 240 | // extension. |
| 241 | uint64_t Value = CE->getValue(); |
| 242 | return (( Value <= 0x000000000000007FULL)|| |
| 243 | (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
| 244 | } |
| 245 | bool isImmSExti64i32() const { |
| 246 | if (!isImm()) |
| 247 | return false; |
| 248 | |
| 249 | // If this isn't a constant expr, just assume it fits and let relaxation |
| 250 | // handle it. |
| 251 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()); |
| 252 | if (!CE) |
| 253 | return true; |
| 254 | |
| 255 | // Otherwise, check the value is in a range that makes sense for this |
| 256 | // extension. |
| 257 | uint64_t Value = CE->getValue(); |
| 258 | return (( Value <= 0x000000007FFFFFFFULL)|| |
| 259 | (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL)); |
Daniel Dunbar | 1fe591d | 2010-05-20 20:20:39 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 262 | bool isMem() const { return Kind == Memory; } |
| 263 | |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 264 | bool isAbsMem() const { |
| 265 | return Kind == Memory && !getMemSegReg() && !getMemBaseReg() && |
Daniel Dunbar | 7b9147a | 2010-02-02 21:44:16 +0000 | [diff] [blame] | 266 | !getMemIndexReg() && getMemScale() == 1; |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 269 | bool isReg() const { return Kind == Register; } |
| 270 | |
Daniel Dunbar | 9c60f53 | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 271 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
| 272 | // Add as immediates when possible. |
| 273 | if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) |
| 274 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 275 | else |
| 276 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
| 277 | } |
| 278 | |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 279 | void addRegOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 280 | assert(N == 1 && "Invalid number of operands!"); |
| 281 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 282 | } |
| 283 | |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 284 | void addImmOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 285 | assert(N == 1 && "Invalid number of operands!"); |
Daniel Dunbar | 9c60f53 | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 286 | addExpr(Inst, getImm()); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 289 | void addMemOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | ec2b1f1 | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 290 | assert((N == 5) && "Invalid number of operands!"); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 291 | Inst.addOperand(MCOperand::CreateReg(getMemBaseReg())); |
| 292 | Inst.addOperand(MCOperand::CreateImm(getMemScale())); |
| 293 | Inst.addOperand(MCOperand::CreateReg(getMemIndexReg())); |
Daniel Dunbar | 9c60f53 | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 294 | addExpr(Inst, getMemDisp()); |
Daniel Dunbar | ec2b1f1 | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 295 | Inst.addOperand(MCOperand::CreateReg(getMemSegReg())); |
| 296 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 297 | |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 298 | void addAbsMemOperands(MCInst &Inst, unsigned N) const { |
| 299 | assert((N == 1) && "Invalid number of operands!"); |
| 300 | Inst.addOperand(MCOperand::CreateExpr(getMemDisp())); |
| 301 | } |
| 302 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 303 | static X86Operand *CreateToken(StringRef Str, SMLoc Loc) { |
| 304 | X86Operand *Res = new X86Operand(Token, Loc, Loc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 305 | Res->Tok.Data = Str.data(); |
| 306 | Res->Tok.Length = Str.size(); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 307 | return Res; |
| 308 | } |
| 309 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 310 | static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) { |
Chris Lattner | 1f19f0f | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 311 | X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 312 | Res->Reg.RegNo = RegNo; |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 313 | return Res; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 314 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 315 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 316 | static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){ |
| 317 | X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 318 | Res->Imm.Val = Val; |
| 319 | return Res; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 320 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 321 | |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 322 | /// Create an absolute memory operand. |
| 323 | static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, |
| 324 | SMLoc EndLoc) { |
| 325 | X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc); |
| 326 | Res->Mem.SegReg = 0; |
| 327 | Res->Mem.Disp = Disp; |
| 328 | Res->Mem.BaseReg = 0; |
| 329 | Res->Mem.IndexReg = 0; |
Daniel Dunbar | 7b9147a | 2010-02-02 21:44:16 +0000 | [diff] [blame] | 330 | Res->Mem.Scale = 1; |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 331 | return Res; |
| 332 | } |
| 333 | |
| 334 | /// Create a generalized memory operand. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 335 | static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp, |
| 336 | unsigned BaseReg, unsigned IndexReg, |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 337 | unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) { |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 338 | // We should never just have a displacement, that should be parsed as an |
| 339 | // absolute memory operand. |
Daniel Dunbar | c09e411 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 340 | assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!"); |
| 341 | |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 342 | // The scale should always be one of {1,2,4,8}. |
| 343 | assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) && |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 344 | "Invalid scale!"); |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 345 | X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 346 | Res->Mem.SegReg = SegReg; |
| 347 | Res->Mem.Disp = Disp; |
| 348 | Res->Mem.BaseReg = BaseReg; |
| 349 | Res->Mem.IndexReg = IndexReg; |
| 350 | Res->Mem.Scale = Scale; |
| 351 | return Res; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 352 | } |
| 353 | }; |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 37dfdec | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 355 | } // end anonymous namespace. |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 356 | |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 357 | bool X86ATTAsmParser::isSrcOp(X86Operand &Op) { |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 358 | unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI; |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 359 | |
| 360 | return (Op.isMem() && |
| 361 | (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) && |
| 362 | isa<MCConstantExpr>(Op.Mem.Disp) && |
| 363 | cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 && |
| 364 | Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0); |
| 365 | } |
| 366 | |
| 367 | bool X86ATTAsmParser::isDstOp(X86Operand &Op) { |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 368 | unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI; |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 369 | |
| 370 | return Op.isMem() && Op.Mem.SegReg == X86::ES && |
| 371 | isa<MCConstantExpr>(Op.Mem.Disp) && |
| 372 | cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 && |
| 373 | Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0; |
| 374 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 376 | bool X86ATTAsmParser::ParseRegister(unsigned &RegNo, |
| 377 | SMLoc &StartLoc, SMLoc &EndLoc) { |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 378 | RegNo = 0; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 379 | const AsmToken &TokPercent = Parser.getTok(); |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 380 | assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!"); |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 381 | StartLoc = TokPercent.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 382 | Parser.Lex(); // Eat percent token. |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 383 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 384 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 0d6cd00 | 2009-09-16 17:18:29 +0000 | [diff] [blame] | 385 | if (Tok.isNot(AsmToken::Identifier)) |
| 386 | return Error(Tok.getLoc(), "invalid register name"); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 387 | |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 388 | // FIXME: Validate register for the current architecture; we have to do |
| 389 | // validation later, so maybe there is no need for this here. |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 390 | RegNo = MatchRegisterName(Tok.getString()); |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 33d60d5 | 2010-09-22 04:11:10 +0000 | [diff] [blame] | 392 | // If the match failed, try the register name as lowercase. |
| 393 | if (RegNo == 0) |
| 394 | RegNo = MatchRegisterName(LowercaseString(Tok.getString())); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 395 | |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 396 | // FIXME: This should be done using Requires<In32BitMode> and |
| 397 | // Requires<In64BitMode> so "eiz" usage in 64-bit instructions |
| 398 | // can be also checked. |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 399 | if (RegNo == X86::RIZ && !is64BitMode()) |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 400 | return Error(Tok.getLoc(), "riz register in 64-bit mode only"); |
| 401 | |
Chris Lattner | 33d60d5 | 2010-09-22 04:11:10 +0000 | [diff] [blame] | 402 | // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens. |
| 403 | if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) { |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 404 | RegNo = X86::ST0; |
| 405 | EndLoc = Tok.getLoc(); |
| 406 | Parser.Lex(); // Eat 'st' |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 407 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 408 | // Check to see if we have '(4)' after %st. |
| 409 | if (getLexer().isNot(AsmToken::LParen)) |
| 410 | return false; |
| 411 | // Lex the paren. |
| 412 | getParser().Lex(); |
| 413 | |
| 414 | const AsmToken &IntTok = Parser.getTok(); |
| 415 | if (IntTok.isNot(AsmToken::Integer)) |
| 416 | return Error(IntTok.getLoc(), "expected stack index"); |
| 417 | switch (IntTok.getIntVal()) { |
| 418 | case 0: RegNo = X86::ST0; break; |
| 419 | case 1: RegNo = X86::ST1; break; |
| 420 | case 2: RegNo = X86::ST2; break; |
| 421 | case 3: RegNo = X86::ST3; break; |
| 422 | case 4: RegNo = X86::ST4; break; |
| 423 | case 5: RegNo = X86::ST5; break; |
| 424 | case 6: RegNo = X86::ST6; break; |
| 425 | case 7: RegNo = X86::ST7; break; |
| 426 | default: return Error(IntTok.getLoc(), "invalid stack index"); |
| 427 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 428 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 429 | if (getParser().Lex().isNot(AsmToken::RParen)) |
| 430 | return Error(Parser.getTok().getLoc(), "expected ')'"); |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 431 | |
Chris Lattner | e16b0fc | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 432 | EndLoc = Tok.getLoc(); |
| 433 | Parser.Lex(); // Eat ')' |
| 434 | return false; |
| 435 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 645b209 | 2010-06-24 07:29:18 +0000 | [diff] [blame] | 437 | // If this is "db[0-7]", match it as an alias |
| 438 | // for dr[0-7]. |
| 439 | if (RegNo == 0 && Tok.getString().size() == 3 && |
| 440 | Tok.getString().startswith("db")) { |
| 441 | switch (Tok.getString()[2]) { |
| 442 | case '0': RegNo = X86::DR0; break; |
| 443 | case '1': RegNo = X86::DR1; break; |
| 444 | case '2': RegNo = X86::DR2; break; |
| 445 | case '3': RegNo = X86::DR3; break; |
| 446 | case '4': RegNo = X86::DR4; break; |
| 447 | case '5': RegNo = X86::DR5; break; |
| 448 | case '6': RegNo = X86::DR6; break; |
| 449 | case '7': RegNo = X86::DR7; break; |
| 450 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 645b209 | 2010-06-24 07:29:18 +0000 | [diff] [blame] | 452 | if (RegNo != 0) { |
| 453 | EndLoc = Tok.getLoc(); |
| 454 | Parser.Lex(); // Eat it. |
| 455 | return false; |
| 456 | } |
| 457 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 458 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 459 | if (RegNo == 0) |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 460 | return Error(Tok.getLoc(), "invalid register name"); |
| 461 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 462 | EndLoc = Tok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 463 | Parser.Lex(); // Eat identifier token. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 464 | return false; |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 467 | X86Operand *X86ATTAsmParser::ParseOperand() { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 468 | switch (getLexer().getKind()) { |
| 469 | default: |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 470 | // Parse a memory operand with no segment register. |
| 471 | return ParseMemOperand(0, Parser.getTok().getLoc()); |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 472 | case AsmToken::Percent: { |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 473 | // Read the register. |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 474 | unsigned RegNo; |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 475 | SMLoc Start, End; |
| 476 | if (ParseRegister(RegNo, Start, End)) return 0; |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 477 | if (RegNo == X86::EIZ || RegNo == X86::RIZ) { |
| 478 | Error(Start, "eiz and riz can only be used as index registers"); |
| 479 | return 0; |
| 480 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 481 | |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 482 | // If this is a segment register followed by a ':', then this is the start |
| 483 | // of a memory reference, otherwise this is a normal register reference. |
| 484 | if (getLexer().isNot(AsmToken::Colon)) |
| 485 | return X86Operand::CreateReg(RegNo, Start, End); |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 486 | |
| 487 | |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 488 | getParser().Lex(); // Eat the colon. |
| 489 | return ParseMemOperand(RegNo, Start); |
Chris Lattner | 2307574 | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 490 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 491 | case AsmToken::Dollar: { |
| 492 | // $42 -> immediate. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 493 | SMLoc Start = Parser.getTok().getLoc(), End; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 494 | Parser.Lex(); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 495 | const MCExpr *Val; |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 496 | if (getParser().ParseExpression(Val, End)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 497 | return 0; |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 498 | return X86Operand::CreateImm(Val, Start, End); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 499 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 500 | } |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Chris Lattner | eef6d78 | 2010-04-17 18:56:34 +0000 | [diff] [blame] | 503 | /// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix |
| 504 | /// has already been parsed if present. |
| 505 | X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) { |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 506 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 507 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 508 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
Chris Lattner | 75f265f | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 509 | // only way to do this without lookahead is to eat the '(' and see what is |
| 510 | // after it. |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 511 | const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext()); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 512 | if (getLexer().isNot(AsmToken::LParen)) { |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 513 | SMLoc ExprEnd; |
| 514 | if (getParser().ParseExpression(Disp, ExprEnd)) return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 515 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 516 | // After parsing the base expression we could either have a parenthesized |
| 517 | // memory address or not. If not, return now. If so, eat the (. |
| 518 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | c09e411 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 519 | // Unless we have a segment register, treat this as an immediate. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 520 | if (SegReg == 0) |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 521 | return X86Operand::CreateMem(Disp, MemStart, ExprEnd); |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 522 | return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 523 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 524 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 525 | // Eat the '('. |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 526 | Parser.Lex(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 527 | } else { |
| 528 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 529 | // so we have to eat the ( to see beyond it. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 530 | SMLoc LParenLoc = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 531 | Parser.Lex(); // Eat the '('. |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 532 | |
Kevin Enderby | 7b4608d | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 533 | if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) { |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 534 | // Nothing to do here, fall into the code below with the '(' part of the |
| 535 | // memory operand consumed. |
| 536 | } else { |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 537 | SMLoc ExprEnd; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 538 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 539 | // It must be an parenthesized expression, parse it now. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 540 | if (getParser().ParseParenExpression(Disp, ExprEnd)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 541 | return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 542 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 543 | // After parsing the base expression we could either have a parenthesized |
| 544 | // memory address or not. If not, return now. If so, eat the (. |
| 545 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | c09e411 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 546 | // Unless we have a segment register, treat this as an immediate. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 547 | if (SegReg == 0) |
Daniel Dunbar | b834f5d | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 548 | return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd); |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 549 | return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 550 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 551 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 552 | // Eat the '('. |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 553 | Parser.Lex(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 556 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 557 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 558 | // the rest of the memory operand. |
Daniel Dunbar | 022e2a8 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 559 | unsigned BaseReg = 0, IndexReg = 0, Scale = 1; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 561 | if (getLexer().is(AsmToken::Percent)) { |
| 562 | SMLoc L; |
| 563 | if (ParseRegister(BaseReg, L, L)) return 0; |
Bruno Cardoso Lopes | 3c8e1be | 2010-07-24 00:06:39 +0000 | [diff] [blame] | 564 | if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) { |
| 565 | Error(L, "eiz and riz can only be used as index registers"); |
| 566 | return 0; |
| 567 | } |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 568 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 569 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 570 | if (getLexer().is(AsmToken::Comma)) { |
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 | // Following the comma we should have either an index register, or a scale |
| 574 | // value. We don't support the later form, but we want to parse it |
| 575 | // correctly. |
| 576 | // |
| 577 | // 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] | 578 | // 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] | 579 | if (getLexer().is(AsmToken::Percent)) { |
Chris Lattner | 29ef9a2 | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 580 | SMLoc L; |
| 581 | if (ParseRegister(IndexReg, L, L)) return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 582 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 583 | if (getLexer().isNot(AsmToken::RParen)) { |
| 584 | // Parse the scale amount: |
| 585 | // ::= ',' [scale-expression] |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 586 | if (getLexer().isNot(AsmToken::Comma)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 587 | Error(Parser.getTok().getLoc(), |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 588 | "expected comma in scale expression"); |
| 589 | return 0; |
| 590 | } |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 591 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 592 | |
| 593 | if (getLexer().isNot(AsmToken::RParen)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 594 | SMLoc Loc = Parser.getTok().getLoc(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 595 | |
| 596 | int64_t ScaleVal; |
| 597 | if (getParser().ParseAbsoluteExpression(ScaleVal)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 598 | return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 599 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 600 | // Validate the scale amount. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 601 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){ |
| 602 | Error(Loc, "scale factor in address must be 1, 2, 4 or 8"); |
| 603 | return 0; |
| 604 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 605 | Scale = (unsigned)ScaleVal; |
| 606 | } |
| 607 | } |
| 608 | } else if (getLexer().isNot(AsmToken::RParen)) { |
Daniel Dunbar | ee91025 | 2010-08-24 19:13:38 +0000 | [diff] [blame] | 609 | // A scale amount without an index is ignored. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 610 | // index. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 611 | SMLoc Loc = Parser.getTok().getLoc(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 612 | |
| 613 | int64_t Value; |
| 614 | if (getParser().ParseAbsoluteExpression(Value)) |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 615 | return 0; |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 616 | |
Daniel Dunbar | ee91025 | 2010-08-24 19:13:38 +0000 | [diff] [blame] | 617 | if (Value != 1) |
| 618 | Warning(Loc, "scale factor without index register is ignored"); |
| 619 | Scale = 1; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 622 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 623 | // 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] | 624 | if (getLexer().isNot(AsmToken::RParen)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 625 | Error(Parser.getTok().getLoc(), "unexpected token in memory operand"); |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 626 | return 0; |
| 627 | } |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 628 | SMLoc MemEnd = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 629 | Parser.Lex(); // Eat the ')'. |
Bruno Cardoso Lopes | f64a7d4 | 2010-07-23 22:15:26 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 0a3c5a5 | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 631 | return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, |
| 632 | MemStart, MemEnd); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 635 | bool X86ATTAsmParser:: |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 636 | ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 637 | SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 638 | StringRef PatchedName = Name; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 639 | |
Chris Lattner | d8f7179 | 2010-11-28 20:23:50 +0000 | [diff] [blame] | 640 | // FIXME: Hack to recognize setneb as setne. |
| 641 | if (PatchedName.startswith("set") && PatchedName.endswith("b") && |
| 642 | PatchedName != "setb" && PatchedName != "setnb") |
| 643 | PatchedName = PatchedName.substr(0, Name.size()-1); |
| 644 | |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 645 | // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}. |
| 646 | const MCExpr *ExtraImmOp = 0; |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 647 | if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) && |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 648 | (PatchedName.endswith("ss") || PatchedName.endswith("sd") || |
| 649 | PatchedName.endswith("ps") || PatchedName.endswith("pd"))) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 650 | bool IsVCMP = PatchedName.startswith("vcmp"); |
| 651 | unsigned SSECCIdx = IsVCMP ? 4 : 3; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 652 | unsigned SSEComparisonCode = StringSwitch<unsigned>( |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 653 | PatchedName.slice(SSECCIdx, PatchedName.size() - 2)) |
Bruno Cardoso Lopes | cc69e13 | 2010-07-07 22:24:03 +0000 | [diff] [blame] | 654 | .Case("eq", 0) |
| 655 | .Case("lt", 1) |
| 656 | .Case("le", 2) |
| 657 | .Case("unord", 3) |
| 658 | .Case("neq", 4) |
| 659 | .Case("nlt", 5) |
| 660 | .Case("nle", 6) |
| 661 | .Case("ord", 7) |
| 662 | .Case("eq_uq", 8) |
| 663 | .Case("nge", 9) |
| 664 | .Case("ngt", 0x0A) |
| 665 | .Case("false", 0x0B) |
| 666 | .Case("neq_oq", 0x0C) |
| 667 | .Case("ge", 0x0D) |
| 668 | .Case("gt", 0x0E) |
| 669 | .Case("true", 0x0F) |
| 670 | .Case("eq_os", 0x10) |
| 671 | .Case("lt_oq", 0x11) |
| 672 | .Case("le_oq", 0x12) |
| 673 | .Case("unord_s", 0x13) |
| 674 | .Case("neq_us", 0x14) |
| 675 | .Case("nlt_uq", 0x15) |
| 676 | .Case("nle_uq", 0x16) |
| 677 | .Case("ord_s", 0x17) |
| 678 | .Case("eq_us", 0x18) |
| 679 | .Case("nge_uq", 0x19) |
| 680 | .Case("ngt_uq", 0x1A) |
| 681 | .Case("false_os", 0x1B) |
| 682 | .Case("neq_os", 0x1C) |
| 683 | .Case("ge_oq", 0x1D) |
| 684 | .Case("gt_oq", 0x1E) |
| 685 | .Case("true_us", 0x1F) |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 686 | .Default(~0U); |
| 687 | if (SSEComparisonCode != ~0U) { |
| 688 | ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode, |
| 689 | getParser().getContext()); |
| 690 | if (PatchedName.endswith("ss")) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 691 | PatchedName = IsVCMP ? "vcmpss" : "cmpss"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 692 | } else if (PatchedName.endswith("sd")) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 693 | PatchedName = IsVCMP ? "vcmpsd" : "cmpsd"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 694 | } else if (PatchedName.endswith("ps")) { |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 695 | PatchedName = IsVCMP ? "vcmpps" : "cmpps"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 696 | } else { |
| 697 | assert(PatchedName.endswith("pd") && "Unexpected mnemonic!"); |
Bruno Cardoso Lopes | 428256b | 2010-06-23 21:10:57 +0000 | [diff] [blame] | 698 | PatchedName = IsVCMP ? "vcmppd" : "cmppd"; |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } |
Bruno Cardoso Lopes | f528d2b | 2010-07-23 18:41:12 +0000 | [diff] [blame] | 702 | |
Daniel Dunbar | 1b6c060 | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 703 | Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc)); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 704 | |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 705 | if (ExtraImmOp) |
| 706 | Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc)); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 707 | |
| 708 | |
Chris Lattner | 2544f42 | 2010-09-08 05:17:37 +0000 | [diff] [blame] | 709 | // Determine whether this is an instruction prefix. |
| 710 | bool isPrefix = |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 711 | Name == "lock" || Name == "rep" || |
| 712 | Name == "repe" || Name == "repz" || |
Rafael Espindola | beb6898 | 2010-11-23 11:23:24 +0000 | [diff] [blame] | 713 | Name == "repne" || Name == "repnz" || |
Rafael Espindola | bfd2d26 | 2010-11-27 20:29:45 +0000 | [diff] [blame] | 714 | Name == "rex64" || Name == "data16"; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 715 | |
| 716 | |
Chris Lattner | 2544f42 | 2010-09-08 05:17:37 +0000 | [diff] [blame] | 717 | // This does the actual operand parsing. Don't parse any more if we have a |
| 718 | // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we |
| 719 | // just want to parse the "lock" as the first instruction and the "incl" as |
| 720 | // the next one. |
| 721 | if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) { |
Daniel Dunbar | 0db68f4 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 722 | |
| 723 | // Parse '*' modifier. |
| 724 | if (getLexer().is(AsmToken::Star)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 725 | SMLoc Loc = Parser.getTok().getLoc(); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 726 | Operands.push_back(X86Operand::CreateToken("*", Loc)); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 727 | Parser.Lex(); // Eat the star. |
Daniel Dunbar | 0db68f4 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 730 | // Read the first operand. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 731 | if (X86Operand *Op = ParseOperand()) |
| 732 | Operands.push_back(Op); |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 733 | else { |
| 734 | Parser.EatToEndOfStatement(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 735 | return true; |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 736 | } |
Daniel Dunbar | 39e2dd7 | 2010-05-25 19:49:32 +0000 | [diff] [blame] | 737 | |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 738 | while (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 739 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 740 | |
| 741 | // Parse and remember the operand. |
Chris Lattner | 309264d | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 742 | if (X86Operand *Op = ParseOperand()) |
| 743 | Operands.push_back(Op); |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 744 | else { |
| 745 | Parser.EatToEndOfStatement(); |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 746 | return true; |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 747 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 748 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 749 | |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 750 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Chris Lattner | c146c4d | 2010-11-18 02:53:02 +0000 | [diff] [blame] | 751 | SMLoc Loc = getLexer().getLoc(); |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 752 | Parser.EatToEndOfStatement(); |
Chris Lattner | c146c4d | 2010-11-18 02:53:02 +0000 | [diff] [blame] | 753 | return Error(Loc, "unexpected token in argument list"); |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 754 | } |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 755 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 2544f42 | 2010-09-08 05:17:37 +0000 | [diff] [blame] | 757 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 758 | Parser.Lex(); // Consume the EndOfStatement |
Kevin Enderby | 7633175 | 2010-12-08 23:57:59 +0000 | [diff] [blame] | 759 | else if (isPrefix && getLexer().is(AsmToken::Slash)) |
| 760 | Parser.Lex(); // Consume the prefix separator Slash |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 762 | // This is a terrible hack to handle "out[bwl]? %al, (%dx)" -> |
| 763 | // "outb %al, %dx". Out doesn't take a memory form, but this is a widely |
| 764 | // documented form in various unofficial manuals, so a lot of code uses it. |
| 765 | if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") && |
| 766 | Operands.size() == 3) { |
| 767 | X86Operand &Op = *(X86Operand*)Operands.back(); |
| 768 | if (Op.isMem() && Op.Mem.SegReg == 0 && |
| 769 | isa<MCConstantExpr>(Op.Mem.Disp) && |
| 770 | cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 && |
| 771 | Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) { |
| 772 | SMLoc Loc = Op.getEndLoc(); |
| 773 | Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc); |
| 774 | delete &Op; |
| 775 | } |
| 776 | } |
Joerg Sonnenberger | 00743c2 | 2011-02-22 20:40:09 +0000 | [diff] [blame] | 777 | // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al". |
| 778 | if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") && |
| 779 | Operands.size() == 3) { |
| 780 | X86Operand &Op = *(X86Operand*)Operands.begin()[1]; |
| 781 | if (Op.isMem() && Op.Mem.SegReg == 0 && |
| 782 | isa<MCConstantExpr>(Op.Mem.Disp) && |
| 783 | cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 && |
| 784 | Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) { |
| 785 | SMLoc Loc = Op.getEndLoc(); |
| 786 | Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc); |
| 787 | delete &Op; |
| 788 | } |
| 789 | } |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 790 | // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]" |
| 791 | if (Name.startswith("ins") && Operands.size() == 3 && |
| 792 | (Name == "insb" || Name == "insw" || Name == "insl")) { |
| 793 | X86Operand &Op = *(X86Operand*)Operands.begin()[1]; |
| 794 | X86Operand &Op2 = *(X86Operand*)Operands.begin()[2]; |
| 795 | if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) { |
| 796 | Operands.pop_back(); |
| 797 | Operands.pop_back(); |
| 798 | delete &Op; |
| 799 | delete &Op2; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]" |
| 804 | if (Name.startswith("outs") && Operands.size() == 3 && |
| 805 | (Name == "outsb" || Name == "outsw" || Name == "outsl")) { |
| 806 | X86Operand &Op = *(X86Operand*)Operands.begin()[1]; |
| 807 | X86Operand &Op2 = *(X86Operand*)Operands.begin()[2]; |
| 808 | if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) { |
| 809 | Operands.pop_back(); |
| 810 | Operands.pop_back(); |
| 811 | delete &Op; |
| 812 | delete &Op2; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]" |
| 817 | if (Name.startswith("movs") && Operands.size() == 3 && |
| 818 | (Name == "movsb" || Name == "movsw" || Name == "movsl" || |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 819 | (is64BitMode() && Name == "movsq"))) { |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 820 | X86Operand &Op = *(X86Operand*)Operands.begin()[1]; |
| 821 | X86Operand &Op2 = *(X86Operand*)Operands.begin()[2]; |
| 822 | if (isSrcOp(Op) && isDstOp(Op2)) { |
| 823 | Operands.pop_back(); |
| 824 | Operands.pop_back(); |
| 825 | delete &Op; |
| 826 | delete &Op2; |
| 827 | } |
| 828 | } |
| 829 | // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]" |
| 830 | if (Name.startswith("lods") && Operands.size() == 3 && |
| 831 | (Name == "lods" || Name == "lodsb" || Name == "lodsw" || |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 832 | Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) { |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 833 | X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]); |
| 834 | X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]); |
| 835 | if (isSrcOp(*Op1) && Op2->isReg()) { |
| 836 | const char *ins; |
| 837 | unsigned reg = Op2->getReg(); |
| 838 | bool isLods = Name == "lods"; |
| 839 | if (reg == X86::AL && (isLods || Name == "lodsb")) |
| 840 | ins = "lodsb"; |
| 841 | else if (reg == X86::AX && (isLods || Name == "lodsw")) |
| 842 | ins = "lodsw"; |
| 843 | else if (reg == X86::EAX && (isLods || Name == "lodsl")) |
| 844 | ins = "lodsl"; |
| 845 | else if (reg == X86::RAX && (isLods || Name == "lodsq")) |
| 846 | ins = "lodsq"; |
| 847 | else |
| 848 | ins = NULL; |
| 849 | if (ins != NULL) { |
| 850 | Operands.pop_back(); |
| 851 | Operands.pop_back(); |
| 852 | delete Op1; |
| 853 | delete Op2; |
| 854 | if (Name != ins) |
| 855 | static_cast<X86Operand*>(Operands[0])->setTokenValue(ins); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]" |
| 860 | if (Name.startswith("stos") && Operands.size() == 3 && |
| 861 | (Name == "stos" || Name == "stosb" || Name == "stosw" || |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 862 | Name == "stosl" || (is64BitMode() && Name == "stosq"))) { |
Joerg Sonnenberger | 96622aa | 2011-03-18 11:59:40 +0000 | [diff] [blame] | 863 | X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]); |
| 864 | X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]); |
| 865 | if (isDstOp(*Op2) && Op1->isReg()) { |
| 866 | const char *ins; |
| 867 | unsigned reg = Op1->getReg(); |
| 868 | bool isStos = Name == "stos"; |
| 869 | if (reg == X86::AL && (isStos || Name == "stosb")) |
| 870 | ins = "stosb"; |
| 871 | else if (reg == X86::AX && (isStos || Name == "stosw")) |
| 872 | ins = "stosw"; |
| 873 | else if (reg == X86::EAX && (isStos || Name == "stosl")) |
| 874 | ins = "stosl"; |
| 875 | else if (reg == X86::RAX && (isStos || Name == "stosq")) |
| 876 | ins = "stosq"; |
| 877 | else |
| 878 | ins = NULL; |
| 879 | if (ins != NULL) { |
| 880 | Operands.pop_back(); |
| 881 | Operands.pop_back(); |
| 882 | delete Op1; |
| 883 | delete Op2; |
| 884 | if (Name != ins) |
| 885 | static_cast<X86Operand*>(Operands[0])->setTokenValue(ins); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Chris Lattner | e9e16a3 | 2010-09-15 04:33:27 +0000 | [diff] [blame] | 890 | // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to |
Chris Lattner | ee211d0 | 2010-09-11 16:32:12 +0000 | [diff] [blame] | 891 | // "shift <op>". |
Daniel Dunbar | d5e7705 | 2010-03-13 00:47:29 +0000 | [diff] [blame] | 892 | if ((Name.startswith("shr") || Name.startswith("sar") || |
Chris Lattner | 8c24b0c | 2010-11-06 21:23:40 +0000 | [diff] [blame] | 893 | Name.startswith("shl") || Name.startswith("sal") || |
| 894 | Name.startswith("rcl") || Name.startswith("rcr") || |
| 895 | Name.startswith("rol") || Name.startswith("ror")) && |
Chris Lattner | 47ab90b | 2010-09-06 18:32:06 +0000 | [diff] [blame] | 896 | Operands.size() == 3) { |
| 897 | X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]); |
| 898 | if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) && |
| 899 | cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) { |
| 900 | delete Operands[1]; |
| 901 | Operands.erase(Operands.begin() + 1); |
| 902 | } |
Daniel Dunbar | f2de13f | 2010-03-20 22:36:38 +0000 | [diff] [blame] | 903 | } |
Chris Lattner | 15f8951 | 2011-04-09 19:41:05 +0000 | [diff] [blame] | 904 | |
| 905 | // Transforms "int $3" into "int3" as a size optimization. We can't write an |
| 906 | // instalias with an immediate operand yet. |
| 907 | if (Name == "int" && Operands.size() == 2) { |
| 908 | X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]); |
| 909 | if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) && |
| 910 | cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) { |
| 911 | delete Operands[1]; |
| 912 | Operands.erase(Operands.begin() + 1); |
| 913 | static_cast<X86Operand*>(Operands[0])->setTokenValue("int3"); |
| 914 | } |
| 915 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 917 | return false; |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Chris Lattner | 2d592d1 | 2010-09-15 04:04:33 +0000 | [diff] [blame] | 920 | bool X86ATTAsmParser:: |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 921 | MatchAndEmitInstruction(SMLoc IDLoc, |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 922 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 923 | MCStreamer &Out) { |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 924 | assert(!Operands.empty() && "Unexpect empty operand list!"); |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 925 | X86Operand *Op = static_cast<X86Operand*>(Operands[0]); |
| 926 | assert(Op->isToken() && "Leading operand should always be a mnemonic!"); |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 928 | // First, handle aliases that expand to multiple instructions. |
| 929 | // FIXME: This should be replaced with a real .td file alias mechanism. |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 930 | // Also, MatchInstructionImpl should do actually *do* the EmitInstruction |
| 931 | // call. |
Andrew Trick | 0966ec0 | 2010-10-22 03:58:29 +0000 | [diff] [blame] | 932 | if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" || |
Chris Lattner | 8b260a7 | 2010-10-30 18:07:17 +0000 | [diff] [blame] | 933 | Op->getToken() == "fstsww" || Op->getToken() == "fstcww" || |
Chris Lattner | 905f2e0 | 2010-09-30 17:11:29 +0000 | [diff] [blame] | 934 | Op->getToken() == "finit" || Op->getToken() == "fsave" || |
Kevin Enderby | 5a37807 | 2010-10-27 02:53:04 +0000 | [diff] [blame] | 935 | Op->getToken() == "fstenv" || Op->getToken() == "fclex") { |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 936 | MCInst Inst; |
| 937 | Inst.setOpcode(X86::WAIT); |
| 938 | Out.EmitInstruction(Inst); |
| 939 | |
Chris Lattner | 0bb83a8 | 2010-09-30 16:39:29 +0000 | [diff] [blame] | 940 | const char *Repl = |
| 941 | StringSwitch<const char*>(Op->getToken()) |
Chris Lattner | 8b260a7 | 2010-10-30 18:07:17 +0000 | [diff] [blame] | 942 | .Case("finit", "fninit") |
| 943 | .Case("fsave", "fnsave") |
| 944 | .Case("fstcw", "fnstcw") |
| 945 | .Case("fstcww", "fnstcw") |
Chris Lattner | 905f2e0 | 2010-09-30 17:11:29 +0000 | [diff] [blame] | 946 | .Case("fstenv", "fnstenv") |
Chris Lattner | 8b260a7 | 2010-10-30 18:07:17 +0000 | [diff] [blame] | 947 | .Case("fstsw", "fnstsw") |
| 948 | .Case("fstsww", "fnstsw") |
| 949 | .Case("fclex", "fnclex") |
Chris Lattner | 0bb83a8 | 2010-09-30 16:39:29 +0000 | [diff] [blame] | 950 | .Default(0); |
| 951 | assert(Repl && "Unknown wait-prefixed instruction"); |
Benjamin Kramer | b0f96fa | 2010-10-01 12:25:27 +0000 | [diff] [blame] | 952 | delete Operands[0]; |
Chris Lattner | 0bb83a8 | 2010-09-30 16:39:29 +0000 | [diff] [blame] | 953 | Operands[0] = X86Operand::CreateToken(Repl, IDLoc); |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 954 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 955 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 956 | bool WasOriginallyInvalidOperand = false; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 957 | unsigned OrigErrorInfo; |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 958 | MCInst Inst; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 959 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 960 | // First, try a direct match. |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 961 | switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) { |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 962 | case Match_Success: |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 963 | Out.EmitInstruction(Inst); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 964 | return false; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 965 | case Match_MissingFeature: |
| 966 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 967 | return true; |
Daniel Dunbar | b412915 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 968 | case Match_ConversionFail: |
| 969 | return Error(IDLoc, "unable to convert operands to instruction"); |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 970 | case Match_InvalidOperand: |
| 971 | WasOriginallyInvalidOperand = true; |
| 972 | break; |
| 973 | case Match_MnemonicFail: |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 974 | break; |
| 975 | } |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 976 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 977 | // FIXME: Ideally, we would only attempt suffix matches for things which are |
| 978 | // valid prefixes, and we could just infer the right unambiguous |
| 979 | // type. However, that requires substantially more matcher support than the |
| 980 | // following hack. |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 981 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 982 | // Change the operand to point to a temporary token. |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 983 | StringRef Base = Op->getToken(); |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 984 | SmallString<16> Tmp; |
| 985 | Tmp += Base; |
| 986 | Tmp += ' '; |
| 987 | Op->setTokenValue(Tmp.str()); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 988 | |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 989 | // If this instruction starts with an 'f', then it is a floating point stack |
| 990 | // instruction. These come in up to three forms for 32-bit, 64-bit, and |
| 991 | // 80-bit floating point, which use the suffixes s,l,t respectively. |
| 992 | // |
| 993 | // Otherwise, we assume that this may be an integer instruction, which comes |
| 994 | // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively. |
| 995 | const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0"; |
| 996 | |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 997 | // Check for the various suffix matches. |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 998 | Tmp[Base.size()] = Suffixes[0]; |
| 999 | unsigned ErrorInfoIgnore; |
| 1000 | MatchResultTy Match1, Match2, Match3, Match4; |
| 1001 | |
| 1002 | Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore); |
| 1003 | Tmp[Base.size()] = Suffixes[1]; |
| 1004 | Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore); |
| 1005 | Tmp[Base.size()] = Suffixes[2]; |
| 1006 | Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore); |
| 1007 | Tmp[Base.size()] = Suffixes[3]; |
| 1008 | Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 1009 | |
| 1010 | // Restore the old token. |
| 1011 | Op->setTokenValue(Base); |
| 1012 | |
| 1013 | // If exactly one matched, then we treat that as a successful match (and the |
| 1014 | // instruction will already have been filled in correctly, since the failing |
| 1015 | // matches won't have modified it). |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1016 | unsigned NumSuccessfulMatches = |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 1017 | (Match1 == Match_Success) + (Match2 == Match_Success) + |
| 1018 | (Match3 == Match_Success) + (Match4 == Match_Success); |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 1019 | if (NumSuccessfulMatches == 1) { |
| 1020 | Out.EmitInstruction(Inst); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 1021 | return false; |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 1022 | } |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1024 | // Otherwise, the match failed, try to produce a decent error message. |
Daniel Dunbar | f1e29d4 | 2010-08-12 00:55:38 +0000 | [diff] [blame] | 1025 | |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 1026 | // If we had multiple suffix matches, then identify this as an ambiguous |
| 1027 | // match. |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1028 | if (NumSuccessfulMatches > 1) { |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 1029 | char MatchChars[4]; |
| 1030 | unsigned NumMatches = 0; |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 1031 | if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0]; |
| 1032 | if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1]; |
| 1033 | if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2]; |
| 1034 | if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3]; |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 1035 | |
| 1036 | SmallString<126> Msg; |
| 1037 | raw_svector_ostream OS(Msg); |
| 1038 | OS << "ambiguous instructions require an explicit suffix (could be "; |
| 1039 | for (unsigned i = 0; i != NumMatches; ++i) { |
| 1040 | if (i != 0) |
| 1041 | OS << ", "; |
| 1042 | if (i + 1 == NumMatches) |
| 1043 | OS << "or "; |
| 1044 | OS << "'" << Base << MatchChars[i] << "'"; |
| 1045 | } |
| 1046 | OS << ")"; |
| 1047 | Error(IDLoc, OS.str()); |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1048 | return true; |
Daniel Dunbar | 09062b1 | 2010-08-12 00:55:42 +0000 | [diff] [blame] | 1049 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1051 | // Okay, we know that none of the variants matched successfully. |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1053 | // If all of the instructions reported an invalid mnemonic, then the original |
| 1054 | // mnemonic was invalid. |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 1055 | if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) && |
| 1056 | (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) { |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1057 | if (!WasOriginallyInvalidOperand) { |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1058 | Error(IDLoc, "invalid instruction mnemonic '" + Base + "'"); |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1059 | return true; |
| 1060 | } |
| 1061 | |
| 1062 | // Recover location info for the operand if we know which was the problem. |
| 1063 | SMLoc ErrorLoc = IDLoc; |
| 1064 | if (OrigErrorInfo != ~0U) { |
Chris Lattner | f884012 | 2010-09-15 03:50:11 +0000 | [diff] [blame] | 1065 | if (OrigErrorInfo >= Operands.size()) |
| 1066 | return Error(IDLoc, "too few operands for instruction"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1068 | ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc(); |
| 1069 | if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc; |
| 1070 | } |
| 1071 | |
Chris Lattner | f884012 | 2010-09-15 03:50:11 +0000 | [diff] [blame] | 1072 | return Error(ErrorLoc, "invalid operand for instruction"); |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1073 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1075 | // If one instruction matched with a missing feature, report this as a |
| 1076 | // missing feature. |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 1077 | if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) + |
| 1078 | (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){ |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1079 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 1080 | return true; |
| 1081 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1083 | // If one instruction matched with an invalid operand, report this as an |
| 1084 | // operand failure. |
Chris Lattner | fb7000f | 2010-11-06 18:28:02 +0000 | [diff] [blame] | 1085 | if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) + |
| 1086 | (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){ |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1087 | Error(IDLoc, "invalid operand for instruction"); |
| 1088 | return true; |
| 1089 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1091 | // If all of these were an outright failure, report it in a useless way. |
| 1092 | // FIXME: We should give nicer diagnostics about the exact failure. |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1093 | Error(IDLoc, "unknown use of instruction mnemonic without a size suffix"); |
Daniel Dunbar | c918d60 | 2010-05-04 16:12:42 +0000 | [diff] [blame] | 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | |
Chris Lattner | 537ca84 | 2010-10-30 17:38:55 +0000 | [diff] [blame] | 1098 | bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 1099 | StringRef IDVal = DirectiveID.getIdentifier(); |
| 1100 | if (IDVal == ".word") |
| 1101 | return ParseDirectiveWord(2, DirectiveID.getLoc()); |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame^] | 1102 | else if (IDVal.startswith(".code")) |
| 1103 | return ParseDirectiveCode(IDVal, DirectiveID.getLoc()); |
Chris Lattner | 537ca84 | 2010-10-30 17:38:55 +0000 | [diff] [blame] | 1104 | return true; |
| 1105 | } |
| 1106 | |
| 1107 | /// ParseDirectiveWord |
| 1108 | /// ::= .word [ expression (, expression)* ] |
| 1109 | bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { |
| 1110 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1111 | for (;;) { |
| 1112 | const MCExpr *Value; |
| 1113 | if (getParser().ParseExpression(Value)) |
| 1114 | return true; |
| 1115 | |
| 1116 | getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/); |
| 1117 | |
| 1118 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 1119 | break; |
| 1120 | |
| 1121 | // FIXME: Improve diagnostic. |
| 1122 | if (getLexer().isNot(AsmToken::Comma)) |
| 1123 | return Error(L, "unexpected token in directive"); |
| 1124 | Parser.Lex(); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | Parser.Lex(); |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame^] | 1132 | /// ParseDirectiveCode |
| 1133 | /// ::= .code32 | .code64 |
| 1134 | bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) { |
| 1135 | if (IDVal == ".code32") { |
| 1136 | Parser.Lex(); |
| 1137 | if (is64BitMode()) { |
| 1138 | SwitchMode(); |
| 1139 | getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32); |
| 1140 | } |
| 1141 | } else if (IDVal == ".code64") { |
| 1142 | Parser.Lex(); |
| 1143 | if (!is64BitMode()) { |
| 1144 | SwitchMode(); |
| 1145 | getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64); |
| 1146 | } |
| 1147 | } else { |
| 1148 | return Error(L, "unexpected directive " + IDVal); |
| 1149 | } |
Chris Lattner | 537ca84 | 2010-10-30 17:38:55 +0000 | [diff] [blame] | 1150 | |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame^] | 1151 | return false; |
| 1152 | } |
Chris Lattner | 537ca84 | 2010-10-30 17:38:55 +0000 | [diff] [blame] | 1153 | |
| 1154 | |
Sean Callanan | e88f552 | 2010-01-23 02:43:15 +0000 | [diff] [blame] | 1155 | extern "C" void LLVMInitializeX86AsmLexer(); |
| 1156 | |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 1157 | // Force static initialization. |
| 1158 | extern "C" void LLVMInitializeX86AsmParser() { |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 1159 | RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target); |
| 1160 | RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target); |
Sean Callanan | e88f552 | 2010-01-23 02:43:15 +0000 | [diff] [blame] | 1161 | LLVMInitializeX86AsmLexer(); |
Daniel Dunbar | 092a9dd | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 1162 | } |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1164 | #define GET_REGISTER_MATCHER |
| 1165 | #define GET_MATCHER_IMPLEMENTATION |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 1166 | #include "X86GenAsmMatcher.inc" |