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