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