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 | 6b3888b | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Twine.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 | 291d669 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 19 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 20 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetRegistry.h" |
| 23 | #include "llvm/Target/TargetAsmParser.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
Benjamin Kramer | 264834b | 2009-07-31 11:35:26 +0000 | [diff] [blame] | 27 | struct X86Operand; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 28 | |
| 29 | class X86ATTAsmParser : public TargetAsmParser { |
| 30 | MCAsmParser &Parser; |
| 31 | |
| 32 | private: |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 33 | MCAsmParser &getParser() const { return Parser; } |
| 34 | |
| 35 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 36 | |
| 37 | void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); } |
| 38 | |
| 39 | bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } |
| 40 | |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 41 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 42 | |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 43 | X86Operand *ParseOperand(); |
| 44 | X86Operand *ParseMemOperand(); |
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 | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 54 | /// } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 55 | |
| 56 | public: |
| 57 | X86ATTAsmParser(const Target &T, MCAsmParser &_Parser) |
| 58 | : TargetAsmParser(T), Parser(_Parser) {} |
| 59 | |
Chris Lattner | f66e4eb | 2010-01-14 21:32:45 +0000 | [diff] [blame] | 60 | virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc, |
Chris Lattner | 22f480d | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 61 | SmallVectorImpl<MCParsedAsmOperand*> &Operands); |
Kevin Enderby | ae90d09 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 62 | |
| 63 | virtual bool ParseDirective(AsmToken DirectiveID); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 64 | }; |
Chris Lattner | e54532b | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 65 | |
| 66 | } // end anonymous namespace |
| 67 | |
Sean Callanan | ef372de | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 68 | /// @name Auto-generated Match Functions |
| 69 | /// { |
| 70 | |
Chris Lattner | 8b38200 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 71 | static unsigned MatchRegisterName(StringRef Name); |
Sean Callanan | ef372de | 2010-01-23 00:40:33 +0000 | [diff] [blame] | 72 | |
| 73 | /// } |
Chris Lattner | e54532b | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 74 | |
| 75 | namespace { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 76 | |
| 77 | /// X86Operand - Instances of this class represent a parsed X86 machine |
| 78 | /// instruction. |
Chris Lattner | 0c119a7 | 2010-01-14 21:20:55 +0000 | [diff] [blame] | 79 | struct X86Operand : public MCParsedAsmOperand { |
Chris Lattner | 61cd2c3 | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 80 | enum KindTy { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 81 | Token, |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 82 | Register, |
| 83 | Immediate, |
| 84 | Memory |
| 85 | } Kind; |
| 86 | |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 87 | SMLoc StartLoc, EndLoc; |
| 88 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 89 | union { |
| 90 | struct { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 91 | const char *Data; |
| 92 | unsigned Length; |
| 93 | } Tok; |
| 94 | |
| 95 | struct { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 96 | unsigned RegNo; |
| 97 | } Reg; |
| 98 | |
| 99 | struct { |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 100 | const MCExpr *Val; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 101 | } Imm; |
| 102 | |
| 103 | struct { |
| 104 | unsigned SegReg; |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 105 | const MCExpr *Disp; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 106 | unsigned BaseReg; |
| 107 | unsigned IndexReg; |
| 108 | unsigned Scale; |
| 109 | } Mem; |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 110 | }; |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 112 | X86Operand(KindTy K, SMLoc Start, SMLoc End) |
Chris Lattner | 61cd2c3 | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 113 | : Kind(K), StartLoc(Start), EndLoc(End) {} |
| 114 | |
| 115 | /// getStartLoc - Get the location of the first token of this operand. |
| 116 | SMLoc getStartLoc() const { return StartLoc; } |
| 117 | /// getEndLoc - Get the location of the last token of this operand. |
| 118 | SMLoc getEndLoc() const { return EndLoc; } |
| 119 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 120 | StringRef getToken() const { |
| 121 | assert(Kind == Token && "Invalid access!"); |
| 122 | return StringRef(Tok.Data, Tok.Length); |
| 123 | } |
| 124 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 125 | unsigned getReg() const { |
| 126 | assert(Kind == Register && "Invalid access!"); |
| 127 | return Reg.RegNo; |
| 128 | } |
Daniel Dunbar | d80432a | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 129 | |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 130 | const MCExpr *getImm() const { |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 131 | assert(Kind == Immediate && "Invalid access!"); |
| 132 | return Imm.Val; |
| 133 | } |
| 134 | |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 135 | const MCExpr *getMemDisp() const { |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 136 | assert(Kind == Memory && "Invalid access!"); |
| 137 | return Mem.Disp; |
| 138 | } |
| 139 | unsigned getMemSegReg() const { |
| 140 | assert(Kind == Memory && "Invalid access!"); |
| 141 | return Mem.SegReg; |
| 142 | } |
| 143 | unsigned getMemBaseReg() const { |
| 144 | assert(Kind == Memory && "Invalid access!"); |
| 145 | return Mem.BaseReg; |
| 146 | } |
| 147 | unsigned getMemIndexReg() const { |
| 148 | assert(Kind == Memory && "Invalid access!"); |
| 149 | return Mem.IndexReg; |
| 150 | } |
| 151 | unsigned getMemScale() const { |
| 152 | assert(Kind == Memory && "Invalid access!"); |
| 153 | return Mem.Scale; |
| 154 | } |
| 155 | |
Daniel Dunbar | 378bee9 | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 156 | bool isToken() const {return Kind == Token; } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 157 | |
| 158 | bool isImm() const { return Kind == Immediate; } |
| 159 | |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 160 | bool isImmSExt8() const { |
| 161 | // Accept immediates which fit in 8 bits when sign extended, and |
| 162 | // non-absolute immediates. |
| 163 | if (!isImm()) |
| 164 | return false; |
| 165 | |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 166 | if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) { |
| 167 | int64_t Value = CE->getValue(); |
| 168 | return Value == (int64_t) (int8_t) Value; |
| 169 | } |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 171 | return true; |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 174 | bool isMem() const { return Kind == Memory; } |
| 175 | |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 176 | bool isAbsMem() const { |
| 177 | return Kind == Memory && !getMemSegReg() && !getMemBaseReg() && |
Daniel Dunbar | 08a1aae | 2010-02-02 21:44:16 +0000 | [diff] [blame] | 178 | !getMemIndexReg() && getMemScale() == 1; |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Daniel Dunbar | fc1b32a | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 181 | bool isNoSegMem() const { |
| 182 | return Kind == Memory && !getMemSegReg(); |
| 183 | } |
| 184 | |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 185 | bool isReg() const { return Kind == Register; } |
| 186 | |
Daniel Dunbar | 26d511f | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 187 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
| 188 | // Add as immediates when possible. |
| 189 | if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) |
| 190 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 191 | else |
| 192 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
| 193 | } |
| 194 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 195 | void addRegOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 196 | assert(N == 1 && "Invalid number of operands!"); |
| 197 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 198 | } |
| 199 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 200 | void addImmOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 201 | assert(N == 1 && "Invalid number of operands!"); |
Daniel Dunbar | 26d511f | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 202 | addExpr(Inst, getImm()); |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 205 | void addImmSExt8Operands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 206 | // FIXME: Support user customization of the render method. |
| 207 | assert(N == 1 && "Invalid number of operands!"); |
Daniel Dunbar | 26d511f | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 208 | addExpr(Inst, getImm()); |
Daniel Dunbar | 06d5cb6 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Daniel Dunbar | b3413d8 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 211 | void addMemOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | fc1b32a | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 212 | assert((N == 5) && "Invalid number of operands!"); |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 213 | Inst.addOperand(MCOperand::CreateReg(getMemBaseReg())); |
| 214 | Inst.addOperand(MCOperand::CreateImm(getMemScale())); |
| 215 | Inst.addOperand(MCOperand::CreateReg(getMemIndexReg())); |
Daniel Dunbar | 26d511f | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 216 | addExpr(Inst, getMemDisp()); |
Daniel Dunbar | fc1b32a | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 217 | Inst.addOperand(MCOperand::CreateReg(getMemSegReg())); |
| 218 | } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 219 | |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 220 | void addAbsMemOperands(MCInst &Inst, unsigned N) const { |
| 221 | assert((N == 1) && "Invalid number of operands!"); |
| 222 | Inst.addOperand(MCOperand::CreateExpr(getMemDisp())); |
| 223 | } |
| 224 | |
Daniel Dunbar | fc1b32a | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 225 | void addNoSegMemOperands(MCInst &Inst, unsigned N) const { |
| 226 | assert((N == 4) && "Invalid number of operands!"); |
Daniel Dunbar | fc1b32a | 2010-01-30 00:24:00 +0000 | [diff] [blame] | 227 | Inst.addOperand(MCOperand::CreateReg(getMemBaseReg())); |
| 228 | Inst.addOperand(MCOperand::CreateImm(getMemScale())); |
| 229 | Inst.addOperand(MCOperand::CreateReg(getMemIndexReg())); |
Daniel Dunbar | 26d511f | 2010-02-13 00:17:21 +0000 | [diff] [blame] | 230 | addExpr(Inst, getMemDisp()); |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 284abb6 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 233 | static X86Operand *CreateToken(StringRef Str, SMLoc Loc) { |
| 234 | X86Operand *Res = new X86Operand(Token, Loc, Loc); |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 235 | Res->Tok.Data = Str.data(); |
| 236 | Res->Tok.Length = Str.size(); |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 237 | return Res; |
| 238 | } |
| 239 | |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 240 | static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) { |
Chris Lattner | 61cd2c3 | 2010-01-15 19:06:59 +0000 | [diff] [blame] | 241 | X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc); |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 242 | Res->Reg.RegNo = RegNo; |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 243 | return Res; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 244 | } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 284abb6 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 246 | static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){ |
| 247 | X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc); |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 248 | Res->Imm.Val = Val; |
| 249 | return Res; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 250 | } |
Daniel Dunbar | fe6759e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 252 | /// Create an absolute memory operand. |
| 253 | static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, |
| 254 | SMLoc EndLoc) { |
| 255 | X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc); |
| 256 | Res->Mem.SegReg = 0; |
| 257 | Res->Mem.Disp = Disp; |
| 258 | Res->Mem.BaseReg = 0; |
| 259 | Res->Mem.IndexReg = 0; |
Daniel Dunbar | 08a1aae | 2010-02-02 21:44:16 +0000 | [diff] [blame] | 260 | Res->Mem.Scale = 1; |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 261 | return Res; |
| 262 | } |
| 263 | |
| 264 | /// Create a generalized memory operand. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 265 | static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp, |
| 266 | unsigned BaseReg, unsigned IndexReg, |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 267 | unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) { |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 268 | // We should never just have a displacement, that should be parsed as an |
| 269 | // absolute memory operand. |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 270 | assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!"); |
| 271 | |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 272 | // The scale should always be one of {1,2,4,8}. |
| 273 | assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) && |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 274 | "Invalid scale!"); |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 275 | X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc); |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 276 | Res->Mem.SegReg = SegReg; |
| 277 | Res->Mem.Disp = Disp; |
| 278 | Res->Mem.BaseReg = BaseReg; |
| 279 | Res->Mem.IndexReg = IndexReg; |
| 280 | Res->Mem.Scale = Scale; |
| 281 | return Res; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 282 | } |
| 283 | }; |
Daniel Dunbar | 4b0f4ef | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 284 | |
Chris Lattner | e54532b | 2009-07-29 06:33:53 +0000 | [diff] [blame] | 285 | } // end anonymous namespace. |
Daniel Dunbar | d80432a | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 287 | |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 288 | bool X86ATTAsmParser::ParseRegister(unsigned &RegNo, |
| 289 | SMLoc &StartLoc, SMLoc &EndLoc) { |
Chris Lattner | 977d91a | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 290 | RegNo = 0; |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 291 | const AsmToken &TokPercent = Parser.getTok(); |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 292 | assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!"); |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 293 | StartLoc = TokPercent.getLoc(); |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 294 | Parser.Lex(); // Eat percent token. |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 295 | |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 296 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 01b83cf | 2009-09-16 17:18:29 +0000 | [diff] [blame] | 297 | if (Tok.isNot(AsmToken::Identifier)) |
| 298 | return Error(Tok.getLoc(), "invalid register name"); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 299 | |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 300 | // FIXME: Validate register for the current architecture; we have to do |
| 301 | // validation later, so maybe there is no need for this here. |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 302 | RegNo = MatchRegisterName(Tok.getString()); |
Chris Lattner | 8b38200 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 57b278e | 2010-02-09 00:49:22 +0000 | [diff] [blame] | 304 | // Parse %st(1) and "%st" as "%st(0)" |
| 305 | if (RegNo == 0 && Tok.getString() == "st") { |
| 306 | RegNo = X86::ST0; |
| 307 | EndLoc = Tok.getLoc(); |
| 308 | Parser.Lex(); // Eat 'st' |
| 309 | |
| 310 | // Check to see if we have '(4)' after %st. |
| 311 | if (getLexer().isNot(AsmToken::LParen)) |
| 312 | return false; |
| 313 | // Lex the paren. |
| 314 | getParser().Lex(); |
| 315 | |
| 316 | const AsmToken &IntTok = Parser.getTok(); |
| 317 | if (IntTok.isNot(AsmToken::Integer)) |
| 318 | return Error(IntTok.getLoc(), "expected stack index"); |
| 319 | switch (IntTok.getIntVal()) { |
| 320 | case 0: RegNo = X86::ST0; break; |
| 321 | case 1: RegNo = X86::ST1; break; |
| 322 | case 2: RegNo = X86::ST2; break; |
| 323 | case 3: RegNo = X86::ST3; break; |
| 324 | case 4: RegNo = X86::ST4; break; |
| 325 | case 5: RegNo = X86::ST5; break; |
| 326 | case 6: RegNo = X86::ST6; break; |
| 327 | case 7: RegNo = X86::ST7; break; |
| 328 | default: return Error(IntTok.getLoc(), "invalid stack index"); |
| 329 | } |
| 330 | |
| 331 | if (getParser().Lex().isNot(AsmToken::RParen)) |
| 332 | return Error(Parser.getTok().getLoc(), "expected ')'"); |
| 333 | |
| 334 | EndLoc = Tok.getLoc(); |
| 335 | Parser.Lex(); // Eat ')' |
| 336 | return false; |
| 337 | } |
| 338 | |
Daniel Dunbar | b0e6abe | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 339 | if (RegNo == 0) |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 340 | return Error(Tok.getLoc(), "invalid register name"); |
| 341 | |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 342 | EndLoc = Tok.getLoc(); |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 343 | Parser.Lex(); // Eat identifier token. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 344 | return false; |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 347 | X86Operand *X86ATTAsmParser::ParseOperand() { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 348 | switch (getLexer().getKind()) { |
| 349 | default: |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 350 | return ParseMemOperand(); |
Chris Lattner | 977d91a | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 351 | case AsmToken::Percent: { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 352 | // FIXME: if a segment register, this could either be just the seg reg, or |
| 353 | // the start of a memory operand. |
Chris Lattner | 977d91a | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 354 | unsigned RegNo; |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 355 | SMLoc Start, End; |
| 356 | if (ParseRegister(RegNo, Start, End)) return 0; |
| 357 | return X86Operand::CreateReg(RegNo, Start, End); |
Chris Lattner | 977d91a | 2010-01-15 18:27:19 +0000 | [diff] [blame] | 358 | } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 359 | case AsmToken::Dollar: { |
| 360 | // $42 -> immediate. |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 361 | SMLoc Start = Parser.getTok().getLoc(), End; |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 362 | Parser.Lex(); |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 363 | const MCExpr *Val; |
Chris Lattner | 2ca686d | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 364 | if (getParser().ParseExpression(Val, End)) |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 365 | return 0; |
Chris Lattner | 284abb6 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 366 | return X86Operand::CreateImm(Val, Start, End); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 367 | } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 368 | } |
Daniel Dunbar | 78929e5 | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 371 | /// ParseMemOperand: segment: disp(basereg, indexreg, scale) |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 372 | X86Operand *X86ATTAsmParser::ParseMemOperand() { |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 373 | SMLoc MemStart = Parser.getTok().getLoc(); |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 374 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 375 | // FIXME: If SegReg ':' (e.g. %gs:), eat and remember. |
| 376 | unsigned SegReg = 0; |
| 377 | |
| 378 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 379 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
Chris Lattner | bded9a3 | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 380 | // only way to do this without lookahead is to eat the '(' and see what is |
| 381 | // after it. |
Daniel Dunbar | 6e96621 | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 382 | const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext()); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 383 | if (getLexer().isNot(AsmToken::LParen)) { |
Chris Lattner | 2ca686d | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 384 | SMLoc ExprEnd; |
| 385 | if (getParser().ParseExpression(Disp, ExprEnd)) return 0; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 386 | |
| 387 | // After parsing the base expression we could either have a parenthesized |
| 388 | // memory address or not. If not, return now. If so, eat the (. |
| 389 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 390 | // Unless we have a segment register, treat this as an immediate. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 391 | if (SegReg == 0) |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 392 | return X86Operand::CreateMem(Disp, MemStart, ExprEnd); |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 393 | return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Eat the '('. |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 397 | Parser.Lex(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 398 | } else { |
| 399 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 400 | // so we have to eat the ( to see beyond it. |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 401 | SMLoc LParenLoc = Parser.getTok().getLoc(); |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 402 | Parser.Lex(); // Eat the '('. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 403 | |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 404 | if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) { |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 405 | // Nothing to do here, fall into the code below with the '(' part of the |
| 406 | // memory operand consumed. |
| 407 | } else { |
Chris Lattner | 284abb6 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 408 | SMLoc ExprEnd; |
| 409 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 410 | // It must be an parenthesized expression, parse it now. |
Chris Lattner | 284abb6 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 411 | if (getParser().ParseParenExpression(Disp, ExprEnd)) |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 412 | return 0; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 413 | |
| 414 | // After parsing the base expression we could either have a parenthesized |
| 415 | // memory address or not. If not, return now. If so, eat the (. |
| 416 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 2409171 | 2009-07-31 22:22:54 +0000 | [diff] [blame] | 417 | // Unless we have a segment register, treat this as an immediate. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 418 | if (SegReg == 0) |
Daniel Dunbar | 4dcefd7 | 2010-01-30 01:02:48 +0000 | [diff] [blame] | 419 | return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd); |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 420 | return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Eat the '('. |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 424 | Parser.Lex(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 429 | // the rest of the memory operand. |
Daniel Dunbar | b7ddef1 | 2009-07-31 20:53:16 +0000 | [diff] [blame] | 430 | unsigned BaseReg = 0, IndexReg = 0, Scale = 1; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 431 | |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 432 | if (getLexer().is(AsmToken::Percent)) { |
| 433 | SMLoc L; |
| 434 | if (ParseRegister(BaseReg, L, L)) return 0; |
| 435 | } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 436 | |
| 437 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 438 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 439 | |
| 440 | // Following the comma we should have either an index register, or a scale |
| 441 | // value. We don't support the later form, but we want to parse it |
| 442 | // correctly. |
| 443 | // |
| 444 | // Not that even though it would be completely consistent to support syntax |
| 445 | // like "1(%eax,,1)", the assembler doesn't. |
Kevin Enderby | e71842b | 2009-09-03 17:15:07 +0000 | [diff] [blame] | 446 | if (getLexer().is(AsmToken::Percent)) { |
Chris Lattner | c2fc91a | 2010-01-15 18:51:29 +0000 | [diff] [blame] | 447 | SMLoc L; |
| 448 | if (ParseRegister(IndexReg, L, L)) return 0; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 449 | |
| 450 | if (getLexer().isNot(AsmToken::RParen)) { |
| 451 | // Parse the scale amount: |
| 452 | // ::= ',' [scale-expression] |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 453 | if (getLexer().isNot(AsmToken::Comma)) { |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 454 | Error(Parser.getTok().getLoc(), |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 455 | "expected comma in scale expression"); |
| 456 | return 0; |
| 457 | } |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 458 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 459 | |
| 460 | if (getLexer().isNot(AsmToken::RParen)) { |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 461 | SMLoc Loc = Parser.getTok().getLoc(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 462 | |
| 463 | int64_t ScaleVal; |
| 464 | if (getParser().ParseAbsoluteExpression(ScaleVal)) |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 465 | return 0; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 466 | |
| 467 | // Validate the scale amount. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 468 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){ |
| 469 | Error(Loc, "scale factor in address must be 1, 2, 4 or 8"); |
| 470 | return 0; |
| 471 | } |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 472 | Scale = (unsigned)ScaleVal; |
| 473 | } |
| 474 | } |
| 475 | } else if (getLexer().isNot(AsmToken::RParen)) { |
| 476 | // Otherwise we have the unsupported form of a scale amount without an |
| 477 | // index. |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 478 | SMLoc Loc = Parser.getTok().getLoc(); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 479 | |
| 480 | int64_t Value; |
| 481 | if (getParser().ParseAbsoluteExpression(Value)) |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 482 | return 0; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 483 | |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 484 | Error(Loc, "cannot have scale factor without index register"); |
| 485 | return 0; |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 490 | if (getLexer().isNot(AsmToken::RParen)) { |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 491 | Error(Parser.getTok().getLoc(), "unexpected token in memory operand"); |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | } |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 494 | SMLoc MemEnd = Parser.getTok().getLoc(); |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 495 | Parser.Lex(); // Eat the ')'. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 80cc03a | 2010-01-15 19:33:43 +0000 | [diff] [blame] | 497 | return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, |
| 498 | MemStart, MemEnd); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chris Lattner | 22f480d | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 501 | bool X86ATTAsmParser:: |
| 502 | ParseInstruction(const StringRef &Name, SMLoc NameLoc, |
| 503 | SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Daniel Dunbar | 6b3888b | 2010-02-10 21:19:28 +0000 | [diff] [blame] | 504 | // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to |
| 505 | // represent alternative syntaxes in the .td file, without requiring |
| 506 | // instruction duplication. |
| 507 | StringRef PatchedName = StringSwitch<StringRef>(Name) |
| 508 | .Case("sal", "shl") |
| 509 | .Case("salb", "shlb") |
| 510 | .Case("sall", "shll") |
| 511 | .Case("salq", "shlq") |
| 512 | .Case("salw", "shlw") |
| 513 | .Case("repe", "rep") |
| 514 | .Case("repz", "rep") |
| 515 | .Case("repnz", "repne") |
| 516 | .Default(Name); |
| 517 | Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc)); |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 518 | |
| 519 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 7695367 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 520 | |
| 521 | // Parse '*' modifier. |
| 522 | if (getLexer().is(AsmToken::Star)) { |
Sean Callanan | 3cc5fa0 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 523 | SMLoc Loc = Parser.getTok().getLoc(); |
Chris Lattner | 284abb6 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 524 | Operands.push_back(X86Operand::CreateToken("*", Loc)); |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 525 | Parser.Lex(); // Eat the star. |
Daniel Dunbar | 7695367 | 2009-08-11 05:00:25 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 528 | // Read the first operand. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 529 | if (X86Operand *Op = ParseOperand()) |
| 530 | Operands.push_back(Op); |
| 531 | else |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 532 | return true; |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 533 | |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 534 | while (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 535 | Parser.Lex(); // Eat the comma. |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 536 | |
| 537 | // Parse and remember the operand. |
Chris Lattner | e4d457c | 2010-01-15 18:44:13 +0000 | [diff] [blame] | 538 | if (X86Operand *Op = ParseOperand()) |
| 539 | Operands.push_back(Op); |
| 540 | else |
Daniel Dunbar | 14c5bf8 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 541 | return true; |
| 542 | } |
| 543 | } |
| 544 | |
Chris Lattner | 22f480d | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 545 | return false; |
Daniel Dunbar | 4b0f4ef | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Kevin Enderby | ae90d09 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 548 | bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 549 | StringRef IDVal = DirectiveID.getIdentifier(); |
| 550 | if (IDVal == ".word") |
| 551 | return ParseDirectiveWord(2, DirectiveID.getLoc()); |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | /// ParseDirectiveWord |
| 556 | /// ::= .word [ expression (, expression)* ] |
| 557 | bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { |
| 558 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 559 | for (;;) { |
| 560 | const MCExpr *Value; |
| 561 | if (getParser().ParseExpression(Value)) |
| 562 | return true; |
| 563 | |
Chris Lattner | a71dc60 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 564 | getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/); |
Kevin Enderby | ae90d09 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 565 | |
| 566 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 567 | break; |
| 568 | |
| 569 | // FIXME: Improve diagnostic. |
| 570 | if (getLexer().isNot(AsmToken::Comma)) |
| 571 | return Error(L, "unexpected token in directive"); |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 572 | Parser.Lex(); |
Kevin Enderby | ae90d09 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Sean Callanan | 34b4a46 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 576 | Parser.Lex(); |
Kevin Enderby | ae90d09 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 577 | return false; |
| 578 | } |
| 579 | |
Sean Callanan | 5bbbc37 | 2010-01-23 02:43:15 +0000 | [diff] [blame] | 580 | extern "C" void LLVMInitializeX86AsmLexer(); |
| 581 | |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 582 | // Force static initialization. |
| 583 | extern "C" void LLVMInitializeX86AsmParser() { |
Daniel Dunbar | c680b01 | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 584 | RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target); |
| 585 | RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target); |
Sean Callanan | 5bbbc37 | 2010-01-23 02:43:15 +0000 | [diff] [blame] | 586 | LLVMInitializeX86AsmLexer(); |
Daniel Dunbar | c7df3cb | 2009-07-17 20:42:00 +0000 | [diff] [blame] | 587 | } |
Daniel Dunbar | 85f1b39 | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 588 | |
| 589 | #include "X86GenAsmMatcher.inc" |