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