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