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