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