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