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 | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 244 | assert(TokPercent.is(AsmToken::Identifier) && "Invalid token kind!"); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 245 | |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 246 | // FIXME: Validate register for the current architecture; we have to do |
| 247 | // validation later, so maybe there is no need for this here. |
| 248 | unsigned RegNo; |
Daniel Dunbar | b0e6abe | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 249 | |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 250 | RegNo = MatchRegisterName(Tok.getString()); |
Daniel Dunbar | b0e6abe | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 251 | if (RegNo == 0) |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 252 | return Error(Tok.getLoc(), "invalid register name"); |
| 253 | |
| 254 | Op = X86Operand::CreateReg(RegNo); |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 255 | getLexer().Lex(); // Eat identifier token. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 256 | |
| 257 | return false; |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 260 | bool X86ATTAsmParser::ParseOperand(X86Operand &Op) { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 261 | switch (getLexer().getKind()) { |
| 262 | default: |
| 263 | return ParseMemOperand(Op); |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 264 | case AsmToken::Percent: |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 265 | // FIXME: if a segment register, this could either be just the seg reg, or |
| 266 | // the start of a memory operand. |
| 267 | return ParseRegister(Op); |
| 268 | case AsmToken::Dollar: { |
| 269 | // $42 -> immediate. |
| 270 | getLexer().Lex(); |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 271 | const MCExpr *Val; |
| 272 | if (getParser().ParseExpression(Val)) |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 273 | return true; |
| 274 | Op = X86Operand::CreateImm(Val); |
| 275 | return false; |
| 276 | } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 277 | } |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 280 | /// ParseMemOperand: segment: disp(basereg, indexreg, scale) |
| 281 | bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) { |
| 282 | // FIXME: If SegReg ':' (e.g. %gs:), eat and remember. |
| 283 | unsigned SegReg = 0; |
| 284 | |
| 285 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 286 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
| 287 | // only way to do this without lookahead is to eat the ( and see what is after |
| 288 | // it. |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 289 | const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext()); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 290 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 291 | if (getParser().ParseExpression(Disp)) return true; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 292 | |
| 293 | // After parsing the base expression we could either have a parenthesized |
| 294 | // memory address or not. If not, return now. If so, eat the (. |
| 295 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 296 | // Unless we have a segment register, treat this as an immediate. |
| 297 | if (SegReg) |
| 298 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1); |
| 299 | else |
| 300 | Op = X86Operand::CreateImm(Disp); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
| 304 | // Eat the '('. |
| 305 | getLexer().Lex(); |
| 306 | } else { |
| 307 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 308 | // so we have to eat the ( to see beyond it. |
| 309 | getLexer().Lex(); // Eat the '('. |
| 310 | |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 311 | if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 312 | // Nothing to do here, fall into the code below with the '(' part of the |
| 313 | // memory operand consumed. |
| 314 | } else { |
| 315 | // It must be an parenthesized expression, parse it now. |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 316 | if (getParser().ParseParenExpression(Disp)) |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 317 | return true; |
| 318 | |
| 319 | // After parsing the base expression we could either have a parenthesized |
| 320 | // memory address or not. If not, return now. If so, eat the (. |
| 321 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 322 | // Unless we have a segment register, treat this as an immediate. |
| 323 | if (SegReg) |
| 324 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1); |
| 325 | else |
| 326 | Op = X86Operand::CreateImm(Disp); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Eat the '('. |
| 331 | getLexer().Lex(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 336 | // the rest of the memory operand. |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 337 | unsigned BaseReg = 0, IndexReg = 0, Scale = 1; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 338 | |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 339 | if (getLexer().is(AsmToken::Percent)) { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 340 | if (ParseRegister(Op)) |
| 341 | return true; |
| 342 | BaseReg = Op.getReg(); |
| 343 | } |
| 344 | |
| 345 | if (getLexer().is(AsmToken::Comma)) { |
| 346 | getLexer().Lex(); // Eat the comma. |
| 347 | |
| 348 | // Following the comma we should have either an index register, or a scale |
| 349 | // value. We don't support the later form, but we want to parse it |
| 350 | // correctly. |
| 351 | // |
| 352 | // Not that even though it would be completely consistent to support syntax |
| 353 | // like "1(%eax,,1)", the assembler doesn't. |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 354 | if (getLexer().is(AsmToken::Percent)) { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 355 | if (ParseRegister(Op)) |
| 356 | return true; |
| 357 | IndexReg = Op.getReg(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 358 | |
| 359 | if (getLexer().isNot(AsmToken::RParen)) { |
| 360 | // Parse the scale amount: |
| 361 | // ::= ',' [scale-expression] |
| 362 | if (getLexer().isNot(AsmToken::Comma)) |
| 363 | return true; |
| 364 | getLexer().Lex(); // Eat the comma. |
| 365 | |
| 366 | if (getLexer().isNot(AsmToken::RParen)) { |
| 367 | SMLoc Loc = getLexer().getTok().getLoc(); |
| 368 | |
| 369 | int64_t ScaleVal; |
| 370 | if (getParser().ParseAbsoluteExpression(ScaleVal)) |
| 371 | return true; |
| 372 | |
| 373 | // Validate the scale amount. |
| 374 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8) |
| 375 | return Error(Loc, "scale factor in address must be 1, 2, 4 or 8"); |
| 376 | Scale = (unsigned)ScaleVal; |
| 377 | } |
| 378 | } |
| 379 | } else if (getLexer().isNot(AsmToken::RParen)) { |
| 380 | // Otherwise we have the unsupported form of a scale amount without an |
| 381 | // index. |
| 382 | SMLoc Loc = getLexer().getTok().getLoc(); |
| 383 | |
| 384 | int64_t Value; |
| 385 | if (getParser().ParseAbsoluteExpression(Value)) |
| 386 | return true; |
| 387 | |
| 388 | return Error(Loc, "cannot have scale factor without index register"); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
| 393 | if (getLexer().isNot(AsmToken::RParen)) |
| 394 | return Error(getLexer().getTok().getLoc(), |
| 395 | "unexpected token in memory operand"); |
| 396 | getLexer().Lex(); // Eat the ')'. |
| 397 | |
| 398 | Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale); |
| 399 | return false; |
| 400 | } |
| 401 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 402 | bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) { |
Daniel Dunbar | 62beebc | 2009-08-07 20:33:39 +0000 | [diff] [blame] | 403 | SmallVector<X86Operand, 8> Operands; |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 404 | |
| 405 | Operands.push_back(X86Operand::CreateToken(Name)); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 406 | |
Daniel Dunbar | a54716c | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 407 | SMLoc Loc = getLexer().getTok().getLoc(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 408 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 7695367 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 409 | |
| 410 | // Parse '*' modifier. |
| 411 | if (getLexer().is(AsmToken::Star)) { |
| 412 | getLexer().Lex(); // Eat the star. |
| 413 | Operands.push_back(X86Operand::CreateToken("*")); |
| 414 | } |
| 415 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 416 | // Read the first operand. |
| 417 | Operands.push_back(X86Operand()); |
| 418 | if (ParseOperand(Operands.back())) |
| 419 | return true; |
| 420 | |
| 421 | while (getLexer().is(AsmToken::Comma)) { |
| 422 | getLexer().Lex(); // Eat the comma. |
| 423 | |
| 424 | // Parse and remember the operand. |
| 425 | Operands.push_back(X86Operand()); |
| 426 | if (ParseOperand(Operands.back())) |
| 427 | return true; |
| 428 | } |
| 429 | } |
| 430 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 431 | if (!MatchInstruction(Operands, Inst)) |
Daniel Dunbar | a54716c | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 432 | return false; |
| 433 | |
| 434 | // FIXME: We should give nicer diagnostics about the exact failure. |
| 435 | |
Daniel Dunbar | 575db96 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 436 | Error(Loc, "unrecognized instruction"); |
| 437 | return true; |
Daniel Dunbar | 4b0f4ef | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Kevin Enderby | ae90d09 | 2009-09-10 20:51:44 +0000 | [diff] [blame^] | 440 | bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 441 | StringRef IDVal = DirectiveID.getIdentifier(); |
| 442 | if (IDVal == ".word") |
| 443 | return ParseDirectiveWord(2, DirectiveID.getLoc()); |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | /// ParseDirectiveWord |
| 448 | /// ::= .word [ expression (, expression)* ] |
| 449 | bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { |
| 450 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 451 | for (;;) { |
| 452 | const MCExpr *Value; |
| 453 | if (getParser().ParseExpression(Value)) |
| 454 | return true; |
| 455 | |
| 456 | getParser().getStreamer().EmitValue(Value, Size); |
| 457 | |
| 458 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 459 | break; |
| 460 | |
| 461 | // FIXME: Improve diagnostic. |
| 462 | if (getLexer().isNot(AsmToken::Comma)) |
| 463 | return Error(L, "unexpected token in directive"); |
| 464 | getLexer().Lex(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | getLexer().Lex(); |
| 469 | return false; |
| 470 | } |
| 471 | |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 472 | // Force static initialization. |
| 473 | extern "C" void LLVMInitializeX86AsmParser() { |
Daniel Dunbar | c680b01 | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 474 | RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target); |
| 475 | RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target); |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 476 | } |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 477 | |
| 478 | #include "X86GenAsmMatcher.inc" |