Daniel Dunbar | c7df3cb | 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 | |
Daniel Dunbar | 0b0441e | 2009-07-18 23:03:22 +0000 | [diff] [blame] | 10 | #include "X86.h" |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | d80432a | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmLexer.h" |
Daniel Dunbar | 4b0f4ef | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmParser.h" |
Daniel Dunbar | a54716c | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCInst.h" |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCValue.h" |
| 17 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetRegistry.h" |
| 19 | #include "llvm/Target/TargetAsmParser.h" |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
Benjamin Kramer | 264834b | 2009-07-31 11:35:26 +0000 | [diff] [blame] | 23 | struct X86Operand; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 24 | |
| 25 | class X86ATTAsmParser : public TargetAsmParser { |
| 26 | MCAsmParser &Parser; |
| 27 | |
| 28 | private: |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 29 | MCAsmParser &getParser() const { return Parser; } |
| 30 | |
| 31 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 32 | |
| 33 | void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); } |
| 34 | |
| 35 | bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } |
| 36 | |
| 37 | bool ParseRegister(X86Operand &Op); |
| 38 | |
| 39 | bool ParseOperand(X86Operand &Op); |
| 40 | |
| 41 | bool ParseMemOperand(X86Operand &Op); |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 42 | |
| 43 | /// @name Auto-generated Match Functions |
| 44 | /// { |
| 45 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 46 | bool MatchInstruction(SmallVectorImpl<X86Operand> &Operands, |
| 47 | MCInst &Inst); |
| 48 | |
Daniel Dunbar | b0e6abe | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 49 | /// MatchRegisterName - Match the given string to a register name, or 0 if |
| 50 | /// there is no match. |
| 51 | unsigned MatchRegisterName(const StringRef &Name); |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 52 | |
| 53 | /// } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | X86ATTAsmParser(const Target &T, MCAsmParser &_Parser) |
| 57 | : TargetAsmParser(T), Parser(_Parser) {} |
| 58 | |
| 59 | virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst); |
| 60 | }; |
Chris Lattner | e54532b | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 61 | |
| 62 | } // end anonymous namespace |
| 63 | |
| 64 | |
| 65 | namespace { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 66 | |
| 67 | /// X86Operand - Instances of this class represent a parsed X86 machine |
| 68 | /// instruction. |
| 69 | struct X86Operand { |
| 70 | enum { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 71 | Token, |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 72 | Register, |
| 73 | Immediate, |
| 74 | Memory |
| 75 | } Kind; |
| 76 | |
| 77 | union { |
| 78 | struct { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 79 | const char *Data; |
| 80 | unsigned Length; |
| 81 | } Tok; |
| 82 | |
| 83 | struct { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 84 | unsigned RegNo; |
| 85 | } Reg; |
| 86 | |
| 87 | struct { |
| 88 | MCValue Val; |
| 89 | } Imm; |
| 90 | |
| 91 | struct { |
| 92 | unsigned SegReg; |
| 93 | MCValue Disp; |
| 94 | unsigned BaseReg; |
| 95 | unsigned IndexReg; |
| 96 | unsigned Scale; |
| 97 | } Mem; |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 98 | }; |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 100 | StringRef getToken() const { |
| 101 | assert(Kind == Token && "Invalid access!"); |
| 102 | return StringRef(Tok.Data, Tok.Length); |
| 103 | } |
| 104 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 105 | unsigned getReg() const { |
| 106 | assert(Kind == Register && "Invalid access!"); |
| 107 | return Reg.RegNo; |
| 108 | } |
Daniel Dunbar | d80432a | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 109 | |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 110 | const MCValue &getImm() const { |
| 111 | assert(Kind == Immediate && "Invalid access!"); |
| 112 | return Imm.Val; |
| 113 | } |
| 114 | |
| 115 | const MCValue &getMemDisp() const { |
| 116 | assert(Kind == Memory && "Invalid access!"); |
| 117 | return Mem.Disp; |
| 118 | } |
| 119 | unsigned getMemSegReg() const { |
| 120 | assert(Kind == Memory && "Invalid access!"); |
| 121 | return Mem.SegReg; |
| 122 | } |
| 123 | unsigned getMemBaseReg() const { |
| 124 | assert(Kind == Memory && "Invalid access!"); |
| 125 | return Mem.BaseReg; |
| 126 | } |
| 127 | unsigned getMemIndexReg() const { |
| 128 | assert(Kind == Memory && "Invalid access!"); |
| 129 | return Mem.IndexReg; |
| 130 | } |
| 131 | unsigned getMemScale() const { |
| 132 | assert(Kind == Memory && "Invalid access!"); |
| 133 | return Mem.Scale; |
| 134 | } |
| 135 | |
Daniel Dunbar | 378bee9 | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 136 | bool isToken() const {return Kind == Token; } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 137 | |
| 138 | bool isImm() const { return Kind == Immediate; } |
| 139 | |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 140 | bool isImmSExt8() const { |
| 141 | // Accept immediates which fit in 8 bits when sign extended, and |
| 142 | // non-absolute immediates. |
| 143 | if (!isImm()) |
| 144 | return false; |
| 145 | |
| 146 | if (!getImm().isAbsolute()) |
| 147 | return true; |
| 148 | |
| 149 | int64_t Value = getImm().getConstant(); |
| 150 | return Value == (int64_t) (int8_t) Value; |
| 151 | } |
| 152 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 153 | bool isMem() const { return Kind == Memory; } |
| 154 | |
| 155 | bool isReg() const { return Kind == Register; } |
| 156 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 157 | void addRegOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 158 | assert(N == 1 && "Invalid number of operands!"); |
| 159 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 160 | } |
| 161 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 162 | void addImmOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 163 | assert(N == 1 && "Invalid number of operands!"); |
| 164 | Inst.addOperand(MCOperand::CreateMCValue(getImm())); |
| 165 | } |
| 166 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 167 | void addImmSExt8Operands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 168 | // FIXME: Support user customization of the render method. |
| 169 | assert(N == 1 && "Invalid number of operands!"); |
| 170 | Inst.addOperand(MCOperand::CreateMCValue(getImm())); |
| 171 | } |
| 172 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 173 | void addMemOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 174 | assert((N == 4 || N == 5) && "Invalid number of operands!"); |
| 175 | |
| 176 | Inst.addOperand(MCOperand::CreateReg(getMemBaseReg())); |
| 177 | Inst.addOperand(MCOperand::CreateImm(getMemScale())); |
| 178 | Inst.addOperand(MCOperand::CreateReg(getMemIndexReg())); |
| 179 | Inst.addOperand(MCOperand::CreateMCValue(getMemDisp())); |
| 180 | |
| 181 | // FIXME: What a hack. |
| 182 | if (N == 5) |
| 183 | Inst.addOperand(MCOperand::CreateReg(getMemSegReg())); |
| 184 | } |
| 185 | |
| 186 | static X86Operand CreateToken(StringRef Str) { |
| 187 | X86Operand Res; |
| 188 | Res.Kind = Token; |
| 189 | Res.Tok.Data = Str.data(); |
| 190 | Res.Tok.Length = Str.size(); |
| 191 | return Res; |
| 192 | } |
| 193 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 194 | static X86Operand CreateReg(unsigned RegNo) { |
| 195 | X86Operand Res; |
| 196 | Res.Kind = Register; |
| 197 | Res.Reg.RegNo = RegNo; |
| 198 | return Res; |
| 199 | } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 201 | static X86Operand CreateImm(MCValue Val) { |
| 202 | X86Operand Res; |
| 203 | Res.Kind = Immediate; |
| 204 | Res.Imm.Val = Val; |
| 205 | return Res; |
| 206 | } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 207 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 208 | static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg, |
| 209 | unsigned IndexReg, unsigned Scale) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 210 | // We should never just have a displacement, that would be an immediate. |
| 211 | assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!"); |
| 212 | |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 213 | // The scale should always be one of {1,2,4,8}. |
| 214 | assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) && |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 215 | "Invalid scale!"); |
| 216 | X86Operand Res; |
| 217 | Res.Kind = Memory; |
| 218 | Res.Mem.SegReg = SegReg; |
| 219 | Res.Mem.Disp = Disp; |
| 220 | Res.Mem.BaseReg = BaseReg; |
| 221 | Res.Mem.IndexReg = IndexReg; |
| 222 | Res.Mem.Scale = Scale; |
| 223 | return Res; |
| 224 | } |
| 225 | }; |
Daniel Dunbar | 4b0f4ef | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 226 | |
Chris Lattner | e54532b | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 227 | } // end anonymous namespace. |
Daniel Dunbar | d80432a | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 228 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 229 | |
| 230 | bool X86ATTAsmParser::ParseRegister(X86Operand &Op) { |
Chris Lattner | e54532b | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 231 | const AsmToken &Tok = getLexer().getTok(); |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 232 | assert(Tok.is(AsmToken::Register) && "Invalid token kind!"); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 233 | |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 234 | // FIXME: Validate register for the current architecture; we have to do |
| 235 | // validation later, so maybe there is no need for this here. |
| 236 | unsigned RegNo; |
| 237 | assert(Tok.getString().startswith("%") && "Invalid register name!"); |
Daniel Dunbar | b0e6abe | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 238 | |
| 239 | RegNo = MatchRegisterName(Tok.getString().substr(1)); |
| 240 | if (RegNo == 0) |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 241 | return Error(Tok.getLoc(), "invalid register name"); |
| 242 | |
| 243 | Op = X86Operand::CreateReg(RegNo); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 244 | getLexer().Lex(); // Eat register token. |
| 245 | |
| 246 | return false; |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 249 | bool X86ATTAsmParser::ParseOperand(X86Operand &Op) { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 250 | switch (getLexer().getKind()) { |
| 251 | default: |
| 252 | return ParseMemOperand(Op); |
| 253 | case AsmToken::Register: |
| 254 | // FIXME: if a segment register, this could either be just the seg reg, or |
| 255 | // the start of a memory operand. |
| 256 | return ParseRegister(Op); |
| 257 | case AsmToken::Dollar: { |
| 258 | // $42 -> immediate. |
| 259 | getLexer().Lex(); |
| 260 | MCValue Val; |
| 261 | if (getParser().ParseRelocatableExpression(Val)) |
| 262 | return true; |
| 263 | Op = X86Operand::CreateImm(Val); |
| 264 | return false; |
| 265 | } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 266 | } |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 269 | /// ParseMemOperand: segment: disp(basereg, indexreg, scale) |
| 270 | bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) { |
| 271 | // FIXME: If SegReg ':' (e.g. %gs:), eat and remember. |
| 272 | unsigned SegReg = 0; |
| 273 | |
| 274 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 275 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
| 276 | // only way to do this without lookahead is to eat the ( and see what is after |
| 277 | // it. |
| 278 | MCValue Disp = MCValue::get(0, 0, 0); |
| 279 | if (getLexer().isNot(AsmToken::LParen)) { |
| 280 | if (getParser().ParseRelocatableExpression(Disp)) return true; |
| 281 | |
| 282 | // After parsing the base expression we could either have a parenthesized |
| 283 | // memory address or not. If not, return now. If so, eat the (. |
| 284 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 285 | // Unless we have a segment register, treat this as an immediate. |
| 286 | if (SegReg) |
| 287 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1); |
| 288 | else |
| 289 | Op = X86Operand::CreateImm(Disp); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | |
| 293 | // Eat the '('. |
| 294 | getLexer().Lex(); |
| 295 | } else { |
| 296 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 297 | // so we have to eat the ( to see beyond it. |
| 298 | getLexer().Lex(); // Eat the '('. |
| 299 | |
| 300 | if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) { |
| 301 | // Nothing to do here, fall into the code below with the '(' part of the |
| 302 | // memory operand consumed. |
| 303 | } else { |
| 304 | // It must be an parenthesized expression, parse it now. |
| 305 | if (getParser().ParseParenRelocatableExpression(Disp)) |
| 306 | return true; |
| 307 | |
| 308 | // After parsing the base expression we could either have a parenthesized |
| 309 | // memory address or not. If not, return now. If so, eat the (. |
| 310 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 311 | // Unless we have a segment register, treat this as an immediate. |
| 312 | if (SegReg) |
| 313 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1); |
| 314 | else |
| 315 | Op = X86Operand::CreateImm(Disp); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 316 | return false; |
| 317 | } |
| 318 | |
| 319 | // Eat the '('. |
| 320 | getLexer().Lex(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 325 | // the rest of the memory operand. |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 326 | unsigned BaseReg = 0, IndexReg = 0, Scale = 1; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 327 | |
| 328 | if (getLexer().is(AsmToken::Register)) { |
| 329 | if (ParseRegister(Op)) |
| 330 | return true; |
| 331 | BaseReg = Op.getReg(); |
| 332 | } |
| 333 | |
| 334 | if (getLexer().is(AsmToken::Comma)) { |
| 335 | getLexer().Lex(); // Eat the comma. |
| 336 | |
| 337 | // Following the comma we should have either an index register, or a scale |
| 338 | // value. We don't support the later form, but we want to parse it |
| 339 | // correctly. |
| 340 | // |
| 341 | // Not that even though it would be completely consistent to support syntax |
| 342 | // like "1(%eax,,1)", the assembler doesn't. |
| 343 | if (getLexer().is(AsmToken::Register)) { |
| 344 | if (ParseRegister(Op)) |
| 345 | return true; |
| 346 | IndexReg = Op.getReg(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 347 | |
| 348 | if (getLexer().isNot(AsmToken::RParen)) { |
| 349 | // Parse the scale amount: |
| 350 | // ::= ',' [scale-expression] |
| 351 | if (getLexer().isNot(AsmToken::Comma)) |
| 352 | return true; |
| 353 | getLexer().Lex(); // Eat the comma. |
| 354 | |
| 355 | if (getLexer().isNot(AsmToken::RParen)) { |
| 356 | SMLoc Loc = getLexer().getTok().getLoc(); |
| 357 | |
| 358 | int64_t ScaleVal; |
| 359 | if (getParser().ParseAbsoluteExpression(ScaleVal)) |
| 360 | return true; |
| 361 | |
| 362 | // Validate the scale amount. |
| 363 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8) |
| 364 | return Error(Loc, "scale factor in address must be 1, 2, 4 or 8"); |
| 365 | Scale = (unsigned)ScaleVal; |
| 366 | } |
| 367 | } |
| 368 | } else if (getLexer().isNot(AsmToken::RParen)) { |
| 369 | // Otherwise we have the unsupported form of a scale amount without an |
| 370 | // index. |
| 371 | SMLoc Loc = getLexer().getTok().getLoc(); |
| 372 | |
| 373 | int64_t Value; |
| 374 | if (getParser().ParseAbsoluteExpression(Value)) |
| 375 | return true; |
| 376 | |
| 377 | return Error(Loc, "cannot have scale factor without index register"); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
| 382 | if (getLexer().isNot(AsmToken::RParen)) |
| 383 | return Error(getLexer().getTok().getLoc(), |
| 384 | "unexpected token in memory operand"); |
| 385 | getLexer().Lex(); // Eat the ')'. |
| 386 | |
| 387 | Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale); |
| 388 | return false; |
| 389 | } |
| 390 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 391 | bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) { |
Daniel Dunbar | 62beebc | 2009-08-07 20:33:39 +0000 | [diff] [blame] | 392 | SmallVector<X86Operand, 8> Operands; |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 393 | |
| 394 | Operands.push_back(X86Operand::CreateToken(Name)); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 395 | |
Daniel Dunbar | a54716c | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 396 | SMLoc Loc = getLexer().getTok().getLoc(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 397 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 7695367 | 2009-08-11 05:00:25 +0000 | [diff] [blame^] | 398 | |
| 399 | // Parse '*' modifier. |
| 400 | if (getLexer().is(AsmToken::Star)) { |
| 401 | getLexer().Lex(); // Eat the star. |
| 402 | Operands.push_back(X86Operand::CreateToken("*")); |
| 403 | } |
| 404 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 405 | // Read the first operand. |
| 406 | Operands.push_back(X86Operand()); |
| 407 | if (ParseOperand(Operands.back())) |
| 408 | return true; |
| 409 | |
| 410 | while (getLexer().is(AsmToken::Comma)) { |
| 411 | getLexer().Lex(); // Eat the comma. |
| 412 | |
| 413 | // Parse and remember the operand. |
| 414 | Operands.push_back(X86Operand()); |
| 415 | if (ParseOperand(Operands.back())) |
| 416 | return true; |
| 417 | } |
| 418 | } |
| 419 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 420 | if (!MatchInstruction(Operands, Inst)) |
Daniel Dunbar | a54716c | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 421 | return false; |
| 422 | |
| 423 | // FIXME: We should give nicer diagnostics about the exact failure. |
| 424 | |
| 425 | // FIXME: For now we just treat unrecognized instructions as "warnings". |
| 426 | Warning(Loc, "unrecognized instruction"); |
| 427 | |
| 428 | return false; |
Daniel Dunbar | 4b0f4ef | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 431 | // Force static initialization. |
| 432 | extern "C" void LLVMInitializeX86AsmParser() { |
Daniel Dunbar | c680b01 | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 433 | RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target); |
| 434 | RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target); |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 435 | } |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 436 | |
| 437 | #include "X86GenAsmMatcher.inc" |