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