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