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