blob: bc8f367e9255b37d06884e0133e978c489a1f00f [file] [log] [blame]
Daniel Dunbar71475772009-07-17 20:42:00 +00001//===-- 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
Evan Cheng11424442011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Chad Rosier6844ea02012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000012#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000015#include "llvm/ADT/StringSwitch.h"
16#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000017#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCParser/MCAsmLexer.h"
21#include "llvm/MC/MCParser/MCAsmParser.h"
22#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCStreamer.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000028#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000030#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000031
Daniel Dunbar71475772009-07-17 20:42:00 +000032using namespace llvm;
33
34namespace {
Benjamin Kramerb60210e2009-07-31 11:35:26 +000035struct X86Operand;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000036
Chad Rosier5362af92013-04-16 18:15:40 +000037static const char OpPrecedence[] = {
38 0, // IC_PLUS
39 0, // IC_MINUS
40 1, // IC_MULTIPLY
41 1, // IC_DIVIDE
42 2, // IC_RPAREN
43 3, // IC_LPAREN
44 0, // IC_IMM
45 0 // IC_REGISTER
46};
47
Devang Patel4a6e7782012-01-12 18:03:40 +000048class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000049 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000050 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000051 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000052private:
Chad Rosier5362af92013-04-16 18:15:40 +000053 enum InfixCalculatorTok {
54 IC_PLUS = 0,
55 IC_MINUS,
56 IC_MULTIPLY,
57 IC_DIVIDE,
58 IC_RPAREN,
59 IC_LPAREN,
60 IC_IMM,
61 IC_REGISTER
62 };
63
64 class InfixCalculator {
65 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
66 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
67 SmallVector<ICToken, 4> PostfixStack;
68
69 public:
70 int64_t popOperand() {
71 assert (!PostfixStack.empty() && "Poped an empty stack!");
72 ICToken Op = PostfixStack.pop_back_val();
73 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
74 && "Expected and immediate or register!");
75 return Op.second;
76 }
77 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
78 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
79 "Unexpected operand!");
80 PostfixStack.push_back(std::make_pair(Op, Val));
81 }
82
Jakub Staszak9c349222013-08-08 15:48:46 +000083 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +000084 void pushOperator(InfixCalculatorTok Op) {
85 // Push the new operator if the stack is empty.
86 if (InfixOperatorStack.empty()) {
87 InfixOperatorStack.push_back(Op);
88 return;
89 }
90
91 // Push the new operator if it has a higher precedence than the operator
92 // on the top of the stack or the operator on the top of the stack is a
93 // left parentheses.
94 unsigned Idx = InfixOperatorStack.size() - 1;
95 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
96 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
97 InfixOperatorStack.push_back(Op);
98 return;
99 }
100
101 // The operator on the top of the stack has higher precedence than the
102 // new operator.
103 unsigned ParenCount = 0;
104 while (1) {
105 // Nothing to process.
106 if (InfixOperatorStack.empty())
107 break;
108
109 Idx = InfixOperatorStack.size() - 1;
110 StackOp = InfixOperatorStack[Idx];
111 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
112 break;
113
114 // If we have an even parentheses count and we see a left parentheses,
115 // then stop processing.
116 if (!ParenCount && StackOp == IC_LPAREN)
117 break;
118
119 if (StackOp == IC_RPAREN) {
120 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000121 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000122 } else if (StackOp == IC_LPAREN) {
123 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000124 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000125 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000126 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000127 PostfixStack.push_back(std::make_pair(StackOp, 0));
128 }
129 }
130 // Push the new operator.
131 InfixOperatorStack.push_back(Op);
132 }
133 int64_t execute() {
134 // Push any remaining operators onto the postfix stack.
135 while (!InfixOperatorStack.empty()) {
136 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
137 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
138 PostfixStack.push_back(std::make_pair(StackOp, 0));
139 }
140
141 if (PostfixStack.empty())
142 return 0;
143
144 SmallVector<ICToken, 16> OperandStack;
145 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
146 ICToken Op = PostfixStack[i];
147 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
148 OperandStack.push_back(Op);
149 } else {
150 assert (OperandStack.size() > 1 && "Too few operands.");
151 int64_t Val;
152 ICToken Op2 = OperandStack.pop_back_val();
153 ICToken Op1 = OperandStack.pop_back_val();
154 switch (Op.first) {
155 default:
156 report_fatal_error("Unexpected operator!");
157 break;
158 case IC_PLUS:
159 Val = Op1.second + Op2.second;
160 OperandStack.push_back(std::make_pair(IC_IMM, Val));
161 break;
162 case IC_MINUS:
163 Val = Op1.second - Op2.second;
164 OperandStack.push_back(std::make_pair(IC_IMM, Val));
165 break;
166 case IC_MULTIPLY:
167 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
168 "Multiply operation with an immediate and a register!");
169 Val = Op1.second * Op2.second;
170 OperandStack.push_back(std::make_pair(IC_IMM, Val));
171 break;
172 case IC_DIVIDE:
173 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
174 "Divide operation with an immediate and a register!");
175 assert (Op2.second != 0 && "Division by zero!");
176 Val = Op1.second / Op2.second;
177 OperandStack.push_back(std::make_pair(IC_IMM, Val));
178 break;
179 }
180 }
181 }
182 assert (OperandStack.size() == 1 && "Expected a single result.");
183 return OperandStack.pop_back_val().second;
184 }
185 };
186
187 enum IntelExprState {
188 IES_PLUS,
189 IES_MINUS,
190 IES_MULTIPLY,
191 IES_DIVIDE,
192 IES_LBRAC,
193 IES_RBRAC,
194 IES_LPAREN,
195 IES_RPAREN,
196 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000197 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000198 IES_IDENTIFIER,
199 IES_ERROR
200 };
201
202 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000203 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000204 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000205 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000206 const MCExpr *Sym;
207 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000208 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000209 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000210 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000211 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000212 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000213 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
214 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000215 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000216
217 unsigned getBaseReg() { return BaseReg; }
218 unsigned getIndexReg() { return IndexReg; }
219 unsigned getScale() { return Scale; }
220 const MCExpr *getSym() { return Sym; }
221 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000222 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000223 bool isValidEndState() {
224 return State == IES_RBRAC || State == IES_INTEGER;
225 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000226 bool getStopOnLBrac() { return StopOnLBrac; }
227 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000228 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000229
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000230 InlineAsmIdentifierInfo &getIdentifierInfo() {
231 return Info;
232 }
233
Chad Rosier5362af92013-04-16 18:15:40 +0000234 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000235 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000236 switch (State) {
237 default:
238 State = IES_ERROR;
239 break;
240 case IES_INTEGER:
241 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000242 case IES_REGISTER:
243 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000244 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000245 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
246 // If we already have a BaseReg, then assume this is the IndexReg with
247 // a scale of 1.
248 if (!BaseReg) {
249 BaseReg = TmpReg;
250 } else {
251 assert (!IndexReg && "BaseReg/IndexReg already set!");
252 IndexReg = TmpReg;
253 Scale = 1;
254 }
255 }
Chad Rosier5362af92013-04-16 18:15:40 +0000256 break;
257 }
Chad Rosier31246272013-04-17 21:01:45 +0000258 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000259 }
260 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000261 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000262 switch (State) {
263 default:
264 State = IES_ERROR;
265 break;
266 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000267 case IES_MULTIPLY:
268 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000269 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000270 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000271 case IES_LBRAC:
272 case IES_RBRAC:
273 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000274 case IES_REGISTER:
275 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000276 // Only push the minus operator if it is not a unary operator.
277 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
278 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
279 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
280 IC.pushOperator(IC_MINUS);
281 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
282 // If we already have a BaseReg, then assume this is the IndexReg with
283 // a scale of 1.
284 if (!BaseReg) {
285 BaseReg = TmpReg;
286 } else {
287 assert (!IndexReg && "BaseReg/IndexReg already set!");
288 IndexReg = TmpReg;
289 Scale = 1;
290 }
Chad Rosier5362af92013-04-16 18:15:40 +0000291 }
Chad Rosier5362af92013-04-16 18:15:40 +0000292 break;
293 }
Chad Rosier31246272013-04-17 21:01:45 +0000294 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000295 }
296 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000297 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000298 switch (State) {
299 default:
300 State = IES_ERROR;
301 break;
302 case IES_PLUS:
303 case IES_LPAREN:
304 State = IES_REGISTER;
305 TmpReg = Reg;
306 IC.pushOperand(IC_REGISTER);
307 break;
Chad Rosier31246272013-04-17 21:01:45 +0000308 case IES_MULTIPLY:
309 // Index Register - Scale * Register
310 if (PrevState == IES_INTEGER) {
311 assert (!IndexReg && "IndexReg already set!");
312 State = IES_REGISTER;
313 IndexReg = Reg;
314 // Get the scale and replace the 'Scale * Register' with '0'.
315 Scale = IC.popOperand();
316 IC.pushOperand(IC_IMM);
317 IC.popOperator();
318 } else {
319 State = IES_ERROR;
320 }
Chad Rosier5362af92013-04-16 18:15:40 +0000321 break;
322 }
Chad Rosier31246272013-04-17 21:01:45 +0000323 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000324 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000325 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000326 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000327 switch (State) {
328 default:
329 State = IES_ERROR;
330 break;
331 case IES_PLUS:
332 case IES_MINUS:
333 State = IES_INTEGER;
334 Sym = SymRef;
335 SymName = SymRefName;
336 IC.pushOperand(IC_IMM);
337 break;
338 }
339 }
340 void onInteger(int64_t TmpInt) {
Chad Rosier31246272013-04-17 21:01:45 +0000341 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000342 switch (State) {
343 default:
344 State = IES_ERROR;
345 break;
346 case IES_PLUS:
347 case IES_MINUS:
Chad Rosier5362af92013-04-16 18:15:40 +0000348 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000349 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000350 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000351 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000352 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
353 // Index Register - Register * Scale
354 assert (!IndexReg && "IndexReg already set!");
355 IndexReg = TmpReg;
356 Scale = TmpInt;
357 // Get the scale and replace the 'Register * Scale' with '0'.
358 IC.popOperator();
359 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
360 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
361 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
362 CurrState == IES_MINUS) {
363 // Unary minus. No need to pop the minus operand because it was never
364 // pushed.
365 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
366 } else {
367 IC.pushOperand(IC_IMM, TmpInt);
368 }
Chad Rosier5362af92013-04-16 18:15:40 +0000369 break;
370 }
Chad Rosier31246272013-04-17 21:01:45 +0000371 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000372 }
373 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000374 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000375 switch (State) {
376 default:
377 State = IES_ERROR;
378 break;
379 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000380 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000381 case IES_RPAREN:
382 State = IES_MULTIPLY;
383 IC.pushOperator(IC_MULTIPLY);
384 break;
385 }
386 }
387 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000388 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000389 switch (State) {
390 default:
391 State = IES_ERROR;
392 break;
393 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000394 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000395 State = IES_DIVIDE;
396 IC.pushOperator(IC_DIVIDE);
397 break;
398 }
399 }
400 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000401 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000402 switch (State) {
403 default:
404 State = IES_ERROR;
405 break;
406 case IES_RBRAC:
407 State = IES_PLUS;
408 IC.pushOperator(IC_PLUS);
409 break;
410 }
411 }
412 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000413 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000414 switch (State) {
415 default:
416 State = IES_ERROR;
417 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000418 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000419 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000420 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000421 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000422 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
423 // If we already have a BaseReg, then assume this is the IndexReg with
424 // a scale of 1.
425 if (!BaseReg) {
426 BaseReg = TmpReg;
427 } else {
428 assert (!IndexReg && "BaseReg/IndexReg already set!");
429 IndexReg = TmpReg;
430 Scale = 1;
431 }
Chad Rosier5362af92013-04-16 18:15:40 +0000432 }
433 break;
434 }
Chad Rosier31246272013-04-17 21:01:45 +0000435 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000436 }
437 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000438 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000439 switch (State) {
440 default:
441 State = IES_ERROR;
442 break;
443 case IES_PLUS:
444 case IES_MINUS:
445 case IES_MULTIPLY:
446 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000447 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000448 // FIXME: We don't handle this type of unary minus, yet.
449 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
450 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
451 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
452 CurrState == IES_MINUS) {
453 State = IES_ERROR;
454 break;
455 }
Chad Rosier5362af92013-04-16 18:15:40 +0000456 State = IES_LPAREN;
457 IC.pushOperator(IC_LPAREN);
458 break;
459 }
Chad Rosier31246272013-04-17 21:01:45 +0000460 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000461 }
462 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000463 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000464 switch (State) {
465 default:
466 State = IES_ERROR;
467 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000468 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000469 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000470 case IES_RPAREN:
471 State = IES_RPAREN;
472 IC.pushOperator(IC_RPAREN);
473 break;
474 }
475 }
476 };
477
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000478 MCAsmParser &getParser() const { return Parser; }
479
480 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
481
Chris Lattnera3a06812011-10-16 04:47:35 +0000482 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000483 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000484 bool MatchingInlineAsm = false) {
485 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000486 return Parser.Error(L, Msg, Ranges);
487 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000488
Devang Patel41b9dde2012-01-17 18:00:18 +0000489 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
490 Error(Loc, Msg);
491 return 0;
492 }
493
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000494 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000495 X86Operand *ParseATTOperand();
496 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000497 X86Operand *ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000498 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000499 X86Operand *ParseIntelOperator(unsigned OpKind);
David Majnemeraa34d792013-08-27 21:56:17 +0000500 X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
501 X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
502 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000503 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000504 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000505 int64_t ImmDisp, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000506 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
507 InlineAsmIdentifierInfo &Info,
508 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000509
Chris Lattnerb9270732010-04-17 18:56:34 +0000510 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000511
Chad Rosier175d0ae2013-04-12 18:21:18 +0000512 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
513 unsigned BaseReg, unsigned IndexReg,
514 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000515 unsigned Size, StringRef Identifier,
516 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000517
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000518 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000519 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000520
Devang Patelde47cce2012-01-18 22:42:29 +0000521 bool processInstruction(MCInst &Inst,
522 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
523
Chad Rosier49963552012-10-13 00:26:04 +0000524 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000525 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000526 MCStreamer &Out, unsigned &ErrorInfo,
527 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000528
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000529 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000530 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000531 bool isSrcOp(X86Operand &Op);
532
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000533 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
534 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000535 bool isDstOp(X86Operand &Op);
536
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000537 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000538 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000539 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000540 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000541 void SwitchMode() {
542 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
543 setAvailableFeatures(FB);
544 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000545
Chad Rosierc2f055d2013-04-18 16:13:18 +0000546 bool isParsingIntelSyntax() {
547 return getParser().getAssemblerDialect();
548 }
549
Daniel Dunbareefe8612010-07-19 05:44:09 +0000550 /// @name Auto-generated Matcher Functions
551 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000552
Chris Lattner3e4582a2010-09-06 19:11:01 +0000553#define GET_ASSEMBLER_HEADER
554#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000555
Daniel Dunbar00331992009-07-29 00:02:19 +0000556 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000557
558public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000559 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
560 const MCInstrInfo &MII)
561 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000562
Daniel Dunbareefe8612010-07-19 05:44:09 +0000563 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000564 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000565 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000566 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000567
Chad Rosierf0e87202012-10-25 20:41:34 +0000568 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
569 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000570 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000571
572 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000573};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000574} // end anonymous namespace
575
Sean Callanan86c11812010-01-23 00:40:33 +0000576/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000577/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000578
Chris Lattner60db0a62010-02-09 00:34:28 +0000579static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000580
581/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000582
Craig Topper6bf3ed42012-07-18 04:59:16 +0000583static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000584 return (( Value <= 0x000000000000007FULL)||
585 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
586 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
587}
588
589static bool isImmSExti32i8Value(uint64_t Value) {
590 return (( Value <= 0x000000000000007FULL)||
591 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
592 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
593}
594
595static bool isImmZExtu32u8Value(uint64_t Value) {
596 return (Value <= 0x00000000000000FFULL);
597}
598
599static bool isImmSExti64i8Value(uint64_t Value) {
600 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000601 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000602}
603
604static bool isImmSExti64i32Value(uint64_t Value) {
605 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000606 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000607}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000608namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000609
610/// X86Operand - Instances of this class represent a parsed X86 machine
611/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000612struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000613 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000614 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000615 Register,
616 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000617 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000618 } Kind;
619
Chris Lattner0c2538f2010-01-15 18:51:29 +0000620 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000621 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000622 StringRef SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000623 void *OpDecl;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000624 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000625
Eric Christopher8996c5d2013-03-15 00:42:55 +0000626 struct TokOp {
627 const char *Data;
628 unsigned Length;
629 };
630
631 struct RegOp {
632 unsigned RegNo;
633 };
634
635 struct ImmOp {
636 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000637 };
638
639 struct MemOp {
640 unsigned SegReg;
641 const MCExpr *Disp;
642 unsigned BaseReg;
643 unsigned IndexReg;
644 unsigned Scale;
645 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000646 };
647
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000648 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000649 struct TokOp Tok;
650 struct RegOp Reg;
651 struct ImmOp Imm;
652 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000653 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000654
Chris Lattner015cfb12010-01-15 19:33:43 +0000655 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000656 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000657
Chad Rosiere81309b2013-04-09 17:53:49 +0000658 StringRef getSymName() { return SymName; }
Chad Rosier732b8372013-04-22 22:04:25 +0000659 void *getOpDecl() { return OpDecl; }
Chad Rosiere81309b2013-04-09 17:53:49 +0000660
Chris Lattner86e61532010-01-15 19:06:59 +0000661 /// getStartLoc - Get the location of the first token of this operand.
662 SMLoc getStartLoc() const { return StartLoc; }
663 /// getEndLoc - Get the location of the last token of this operand.
664 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000665 /// getLocRange - Get the range between the first and last token of this
666 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000667 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000668 /// getOffsetOfLoc - Get the location of the offset operator.
669 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000670
Jim Grosbach602aa902011-07-13 15:34:57 +0000671 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000672
Daniel Dunbare10787e2009-08-07 08:26:05 +0000673 StringRef getToken() const {
674 assert(Kind == Token && "Invalid access!");
675 return StringRef(Tok.Data, Tok.Length);
676 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000677 void setTokenValue(StringRef Value) {
678 assert(Kind == Token && "Invalid access!");
679 Tok.Data = Value.data();
680 Tok.Length = Value.size();
681 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000682
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000683 unsigned getReg() const {
684 assert(Kind == Register && "Invalid access!");
685 return Reg.RegNo;
686 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000687
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000688 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000689 assert(Kind == Immediate && "Invalid access!");
690 return Imm.Val;
691 }
692
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000693 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000694 assert(Kind == Memory && "Invalid access!");
695 return Mem.Disp;
696 }
697 unsigned getMemSegReg() const {
698 assert(Kind == Memory && "Invalid access!");
699 return Mem.SegReg;
700 }
701 unsigned getMemBaseReg() const {
702 assert(Kind == Memory && "Invalid access!");
703 return Mem.BaseReg;
704 }
705 unsigned getMemIndexReg() const {
706 assert(Kind == Memory && "Invalid access!");
707 return Mem.IndexReg;
708 }
709 unsigned getMemScale() const {
710 assert(Kind == Memory && "Invalid access!");
711 return Mem.Scale;
712 }
713
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000714 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000715
716 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000717
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000718 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000719 if (!isImm())
720 return false;
721
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000722 // If this isn't a constant expr, just assume it fits and let relaxation
723 // handle it.
724 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
725 if (!CE)
726 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000727
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000728 // Otherwise, check the value is in a range that makes sense for this
729 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000730 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000731 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000732 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000733 if (!isImm())
734 return false;
735
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000736 // If this isn't a constant expr, just assume it fits and let relaxation
737 // handle it.
738 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
739 if (!CE)
740 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000741
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000742 // Otherwise, check the value is in a range that makes sense for this
743 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000744 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000745 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000746 bool isImmZExtu32u8() const {
747 if (!isImm())
748 return false;
749
750 // If this isn't a constant expr, just assume it fits and let relaxation
751 // handle it.
752 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
753 if (!CE)
754 return true;
755
756 // Otherwise, check the value is in a range that makes sense for this
757 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000758 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000759 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000760 bool isImmSExti64i8() const {
761 if (!isImm())
762 return false;
763
764 // If this isn't a constant expr, just assume it fits and let relaxation
765 // handle it.
766 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
767 if (!CE)
768 return true;
769
770 // Otherwise, check the value is in a range that makes sense for this
771 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000772 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000773 }
774 bool isImmSExti64i32() const {
775 if (!isImm())
776 return false;
777
778 // If this isn't a constant expr, just assume it fits and let relaxation
779 // handle it.
780 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
781 if (!CE)
782 return true;
783
784 // Otherwise, check the value is in a range that makes sense for this
785 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000786 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000787 }
788
Chad Rosier5bca3f92012-10-22 19:50:35 +0000789 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000790 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000791 }
792
Chad Rosiera4bc9432013-01-10 22:10:27 +0000793 bool needAddressOf() const {
794 return AddressOf;
795 }
796
Daniel Dunbare10787e2009-08-07 08:26:05 +0000797 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000798 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000799 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000800 }
Chad Rosier51afe632012-06-27 22:34:28 +0000801 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000802 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000803 }
Chad Rosier51afe632012-06-27 22:34:28 +0000804 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000805 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000806 }
Chad Rosier51afe632012-06-27 22:34:28 +0000807 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000808 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000809 }
Chad Rosier51afe632012-06-27 22:34:28 +0000810 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000811 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000812 }
Chad Rosier51afe632012-06-27 22:34:28 +0000813 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000814 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000815 }
Chad Rosier51afe632012-06-27 22:34:28 +0000816 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000817 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000818 }
Craig Topper8c26c422013-08-25 23:18:05 +0000819 bool isMem512() const {
820 return Kind == Memory && (!Mem.Size || Mem.Size == 512);
821 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000822
Craig Topper01deb5f2012-07-18 04:11:12 +0000823 bool isMemVX32() const {
824 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
825 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
826 }
827 bool isMemVY32() const {
828 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
829 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
830 }
831 bool isMemVX64() const {
832 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
833 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
834 }
835 bool isMemVY64() const {
836 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
837 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
838 }
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000839 bool isMemVZ32() const {
840 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
841 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
842 }
843 bool isMemVZ64() const {
844 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
845 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
846 }
847
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000848 bool isAbsMem() const {
849 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000850 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000851 }
852
Craig Topper18854172013-08-25 22:23:38 +0000853 bool isMemOffs8() const {
854 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
855 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 8);
856 }
857 bool isMemOffs16() const {
858 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
859 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 16);
860 }
861 bool isMemOffs32() const {
862 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
863 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 32);
864 }
865 bool isMemOffs64() const {
866 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
867 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 64);
868 }
869
Daniel Dunbare10787e2009-08-07 08:26:05 +0000870 bool isReg() const { return Kind == Register; }
871
Craig Toppera422b092013-10-14 04:55:01 +0000872 bool isGR32orGR64() const {
873 return Kind == Register &&
874 (X86MCRegisterClasses[X86::GR32RegClassID].contains(getReg()) ||
875 X86MCRegisterClasses[X86::GR64RegClassID].contains(getReg()));
876 }
877
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000878 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
879 // Add as immediates when possible.
880 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
881 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
882 else
883 Inst.addOperand(MCOperand::CreateExpr(Expr));
884 }
885
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000886 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000887 assert(N == 1 && "Invalid number of operands!");
888 Inst.addOperand(MCOperand::CreateReg(getReg()));
889 }
890
Craig Toppera422b092013-10-14 04:55:01 +0000891 static unsigned getGR32FromGR64(unsigned RegNo) {
892 switch (RegNo) {
893 default: llvm_unreachable("Unexpected register");
894 case X86::RAX: return X86::EAX;
895 case X86::RCX: return X86::ECX;
896 case X86::RDX: return X86::EDX;
897 case X86::RBX: return X86::EBX;
898 case X86::RBP: return X86::EBP;
899 case X86::RSP: return X86::ESP;
900 case X86::RSI: return X86::ESI;
901 case X86::RDI: return X86::EDI;
902 case X86::R8: return X86::R8D;
903 case X86::R9: return X86::R9D;
904 case X86::R10: return X86::R10D;
905 case X86::R11: return X86::R11D;
906 case X86::R12: return X86::R12D;
907 case X86::R13: return X86::R13D;
908 case X86::R14: return X86::R14D;
909 case X86::R15: return X86::R15D;
910 case X86::RIP: return X86::EIP;
911 }
912 }
913
914 void addGR32orGR64Operands(MCInst &Inst, unsigned N) const {
915 assert(N == 1 && "Invalid number of operands!");
916 unsigned RegNo = getReg();
917 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo))
918 RegNo = getGR32FromGR64(RegNo);
919 Inst.addOperand(MCOperand::CreateReg(RegNo));
920 }
921
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000922 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000923 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000924 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000925 }
926
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000927 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000928 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000929 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
930 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
931 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000932 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000933 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
934 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000935
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000936 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
937 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000938 // Add as immediates when possible.
939 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
940 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
941 else
942 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000943 }
944
Craig Topper18854172013-08-25 22:23:38 +0000945 void addMemOffsOperands(MCInst &Inst, unsigned N) const {
946 assert((N == 1) && "Invalid number of operands!");
947 // Add as immediates when possible.
948 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
949 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
950 else
951 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
952 }
953
Chris Lattner528d00b2010-01-15 19:28:38 +0000954 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000955 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000956 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000957 Res->Tok.Data = Str.data();
958 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000959 return Res;
960 }
961
Chad Rosier91c82662012-10-24 17:22:29 +0000962 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000963 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000964 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +0000965 StringRef SymName = StringRef(),
966 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +0000967 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000968 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000969 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000970 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000971 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000972 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000973 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000974 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000975
Chad Rosierf3c04f62013-03-19 21:58:18 +0000976 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000977 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000978 Res->Imm.Val = Val;
979 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000980 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000981
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000982 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000983 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +0000984 unsigned Size = 0, StringRef SymName = StringRef(),
985 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000986 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
987 Res->Mem.SegReg = 0;
988 Res->Mem.Disp = Disp;
989 Res->Mem.BaseReg = 0;
990 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000991 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000992 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +0000993 Res->SymName = SymName;
994 Res->OpDecl = OpDecl;
995 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000996 return Res;
997 }
998
999 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001000 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
1001 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +00001002 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +00001003 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +00001004 StringRef SymName = StringRef(),
1005 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001006 // We should never just have a displacement, that should be parsed as an
1007 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001008 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
1009
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001010 // The scale should always be one of {1,2,4,8}.
1011 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001012 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +00001013 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001014 Res->Mem.SegReg = SegReg;
1015 Res->Mem.Disp = Disp;
1016 Res->Mem.BaseReg = BaseReg;
1017 Res->Mem.IndexReg = IndexReg;
1018 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +00001019 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001020 Res->SymName = SymName;
1021 Res->OpDecl = OpDecl;
1022 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001023 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001024 }
1025};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00001026
Chris Lattner4eb9df02009-07-29 06:33:53 +00001027} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +00001028
Devang Patel4a6e7782012-01-12 18:03:40 +00001029bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001030 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001031
1032 return (Op.isMem() &&
1033 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
1034 isa<MCConstantExpr>(Op.Mem.Disp) &&
1035 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1036 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
1037}
1038
Devang Patel4a6e7782012-01-12 18:03:40 +00001039bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001040 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001041
Chad Rosier51afe632012-06-27 22:34:28 +00001042 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +00001043 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001044 isa<MCConstantExpr>(Op.Mem.Disp) &&
1045 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1046 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
1047}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001048
Devang Patel4a6e7782012-01-12 18:03:40 +00001049bool X86AsmParser::ParseRegister(unsigned &RegNo,
1050 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001051 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001052 const AsmToken &PercentTok = Parser.getTok();
1053 StartLoc = PercentTok.getLoc();
1054
1055 // If we encounter a %, ignore it. This code handles registers with and
1056 // without the prefix, unprefixed registers can occur in cfi directives.
1057 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001058 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001059
Sean Callanan936b0d32010-01-19 21:44:56 +00001060 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001061 EndLoc = Tok.getEndLoc();
1062
Devang Patelce6a2ca2012-01-20 22:32:05 +00001063 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001064 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001065 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001066 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001067 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001068
Kevin Enderby7d912182009-09-03 17:15:07 +00001069 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001070
Chris Lattner1261b812010-09-22 04:11:10 +00001071 // If the match failed, try the register name as lowercase.
1072 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001073 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001074
Evan Chengeda1d4f2011-07-27 23:22:03 +00001075 if (!is64BitMode()) {
1076 // FIXME: This should be done using Requires<In32BitMode> and
1077 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1078 // checked.
1079 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1080 // REX prefix.
1081 if (RegNo == X86::RIZ ||
1082 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1083 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1084 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001085 return Error(StartLoc, "register %"
1086 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001087 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001088 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001089
Chris Lattner1261b812010-09-22 04:11:10 +00001090 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1091 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001092 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001093 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001094
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001095 // Check to see if we have '(4)' after %st.
1096 if (getLexer().isNot(AsmToken::LParen))
1097 return false;
1098 // Lex the paren.
1099 getParser().Lex();
1100
1101 const AsmToken &IntTok = Parser.getTok();
1102 if (IntTok.isNot(AsmToken::Integer))
1103 return Error(IntTok.getLoc(), "expected stack index");
1104 switch (IntTok.getIntVal()) {
1105 case 0: RegNo = X86::ST0; break;
1106 case 1: RegNo = X86::ST1; break;
1107 case 2: RegNo = X86::ST2; break;
1108 case 3: RegNo = X86::ST3; break;
1109 case 4: RegNo = X86::ST4; break;
1110 case 5: RegNo = X86::ST5; break;
1111 case 6: RegNo = X86::ST6; break;
1112 case 7: RegNo = X86::ST7; break;
1113 default: return Error(IntTok.getLoc(), "invalid stack index");
1114 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001115
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001116 if (getParser().Lex().isNot(AsmToken::RParen))
1117 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001118
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001119 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001120 Parser.Lex(); // Eat ')'
1121 return false;
1122 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001123
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001124 EndLoc = Parser.getTok().getEndLoc();
1125
Chris Lattner80486622010-06-24 07:29:18 +00001126 // If this is "db[0-7]", match it as an alias
1127 // for dr[0-7].
1128 if (RegNo == 0 && Tok.getString().size() == 3 &&
1129 Tok.getString().startswith("db")) {
1130 switch (Tok.getString()[2]) {
1131 case '0': RegNo = X86::DR0; break;
1132 case '1': RegNo = X86::DR1; break;
1133 case '2': RegNo = X86::DR2; break;
1134 case '3': RegNo = X86::DR3; break;
1135 case '4': RegNo = X86::DR4; break;
1136 case '5': RegNo = X86::DR5; break;
1137 case '6': RegNo = X86::DR6; break;
1138 case '7': RegNo = X86::DR7; break;
1139 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001140
Chris Lattner80486622010-06-24 07:29:18 +00001141 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001142 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001143 Parser.Lex(); // Eat it.
1144 return false;
1145 }
1146 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001147
Devang Patelce6a2ca2012-01-20 22:32:05 +00001148 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001149 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001150 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001151 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001152 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001153
Sean Callanana83fd7d2010-01-19 20:27:46 +00001154 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001155 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001156}
1157
Devang Patel4a6e7782012-01-12 18:03:40 +00001158X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001159 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001160 return ParseIntelOperand();
1161 return ParseATTOperand();
1162}
1163
Devang Patel41b9dde2012-01-17 18:00:18 +00001164/// getIntelMemOperandSize - Return intel memory operand size.
1165static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001166 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001167 .Cases("BYTE", "byte", 8)
1168 .Cases("WORD", "word", 16)
1169 .Cases("DWORD", "dword", 32)
1170 .Cases("QWORD", "qword", 64)
1171 .Cases("XWORD", "xword", 80)
1172 .Cases("XMMWORD", "xmmword", 128)
1173 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001174 .Default(0);
1175 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001176}
1177
Chad Rosier175d0ae2013-04-12 18:21:18 +00001178X86Operand *
1179X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1180 unsigned BaseReg, unsigned IndexReg,
1181 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001182 unsigned Size, StringRef Identifier,
1183 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001184 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001185 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1186 // reference. We need an 'r' constraint here, so we need to create register
1187 // operand to ensure proper matching. Just pick a GPR based on the size of
1188 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001189 if (!Info.IsVarDecl) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001190 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1191 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001192 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001193 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001194 if (!Size) {
1195 Size = Info.Type * 8; // Size is in terms of bits in this context.
1196 if (Size)
1197 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1198 /*Len=*/0, Size));
1199 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001200 }
1201
Chad Rosier7ca135b2013-03-19 21:11:56 +00001202 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001203 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001204 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001205 BaseReg = BaseReg ? BaseReg : 1;
1206 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001207 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001208}
1209
Chad Rosierd383db52013-04-12 20:20:54 +00001210static void
1211RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1212 StringRef SymName, int64_t ImmDisp,
1213 int64_t FinalImmDisp, SMLoc &BracLoc,
1214 SMLoc &StartInBrac, SMLoc &End) {
1215 // Remove the '[' and ']' from the IR string.
1216 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1217 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1218
1219 // If ImmDisp is non-zero, then we parsed a displacement before the
1220 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1221 // If ImmDisp doesn't match the displacement computed by the state machine
1222 // then we have an additional displacement in the bracketed expression.
1223 if (ImmDisp != FinalImmDisp) {
1224 if (ImmDisp) {
1225 // We have an immediate displacement before the bracketed expression.
1226 // Adjust this to match the final immediate displacement.
1227 bool Found = false;
1228 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1229 E = AsmRewrites->end(); I != E; ++I) {
1230 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1231 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001232 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1233 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001234 (*I).Kind = AOK_Imm;
1235 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1236 (*I).Val = FinalImmDisp;
1237 Found = true;
1238 break;
1239 }
1240 }
1241 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001242 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001243 } else {
1244 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001245 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001246 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001247 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001248 }
1249 }
1250 // Remove all the ImmPrefix rewrites within the brackets.
1251 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1252 E = AsmRewrites->end(); I != E; ++I) {
1253 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1254 continue;
1255 if ((*I).Kind == AOK_ImmPrefix)
1256 (*I).Kind = AOK_Delete;
1257 }
1258 const char *SymLocPtr = SymName.data();
1259 // Skip everything before the symbol.
1260 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1261 assert(Len > 0 && "Expected a non-negative length.");
1262 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1263 }
1264 // Skip everything after the symbol.
1265 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1266 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1267 assert(Len > 0 && "Expected a non-negative length.");
1268 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1269 }
1270}
1271
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001272bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001273 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001274
Chad Rosier5c118fd2013-01-14 22:31:35 +00001275 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001276 while (!Done) {
1277 bool UpdateLocLex = true;
1278
1279 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1280 // identifier. Don't try an parse it as a register.
1281 if (Tok.getString().startswith("."))
1282 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001283
1284 // If we're parsing an immediate expression, we don't expect a '['.
1285 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1286 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001287
1288 switch (getLexer().getKind()) {
1289 default: {
1290 if (SM.isValidEndState()) {
1291 Done = true;
1292 break;
1293 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001294 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001295 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001296 case AsmToken::EndOfStatement: {
1297 Done = true;
1298 break;
1299 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001300 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001301 // This could be a register or a symbolic displacement.
1302 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001303 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001304 SMLoc IdentLoc = Tok.getLoc();
1305 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001306 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001307 SM.onRegister(TmpReg);
1308 UpdateLocLex = false;
1309 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001310 } else {
1311 if (!isParsingInlineAsm()) {
1312 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001313 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001314 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001315 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001316 if (ParseIntelIdentifier(Val, Identifier, Info,
1317 /*Unevaluated=*/false, End))
1318 return true;
Chad Rosier95ce8892013-04-19 18:39:50 +00001319 }
1320 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001321 UpdateLocLex = false;
1322 break;
1323 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001324 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001325 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001326 case AsmToken::Integer:
Chad Rosierbfb70992013-04-17 00:11:46 +00001327 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001328 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1329 Tok.getLoc()));
1330 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001331 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001332 case AsmToken::Plus: SM.onPlus(); break;
1333 case AsmToken::Minus: SM.onMinus(); break;
1334 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001335 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001336 case AsmToken::LBrac: SM.onLBrac(); break;
1337 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001338 case AsmToken::LParen: SM.onLParen(); break;
1339 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001340 }
Chad Rosier31246272013-04-17 21:01:45 +00001341 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001342 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001343
Chad Rosier5c118fd2013-01-14 22:31:35 +00001344 if (!Done && UpdateLocLex) {
1345 End = Tok.getLoc();
1346 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001347 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001348 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001349 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001350}
1351
1352X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001353 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001354 unsigned Size) {
1355 const AsmToken &Tok = Parser.getTok();
1356 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1357 if (getLexer().isNot(AsmToken::LBrac))
1358 return ErrorOperand(BracLoc, "Expected '[' token!");
1359 Parser.Lex(); // Eat '['
1360
1361 SMLoc StartInBrac = Tok.getLoc();
1362 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1363 // may have already parsed an immediate displacement before the bracketed
1364 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001365 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001366 if (ParseIntelExpression(SM, End))
1367 return 0;
Devang Patel41b9dde2012-01-17 18:00:18 +00001368
Chad Rosier175d0ae2013-04-12 18:21:18 +00001369 const MCExpr *Disp;
1370 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001371 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001372 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001373 if (isParsingInlineAsm())
1374 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001375 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001376 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001377 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001378 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001379 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001380 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001381
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001382 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001383 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001384 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001385 if (ParseIntelDotOperator(Disp, NewDisp))
1386 return 0;
Chad Rosier911c1f32012-10-25 17:37:43 +00001387
Chad Rosier70f47592013-04-10 20:07:47 +00001388 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001389 Parser.Lex(); // Eat the field.
1390 Disp = NewDisp;
1391 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001392
Chad Rosier5c118fd2013-01-14 22:31:35 +00001393 int BaseReg = SM.getBaseReg();
1394 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001395 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001396 if (!isParsingInlineAsm()) {
1397 // handle [-42]
1398 if (!BaseReg && !IndexReg) {
1399 if (!SegReg)
1400 return X86Operand::CreateMem(Disp, Start, End, Size);
1401 else
1402 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1403 }
1404 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1405 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001406 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001407
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001408 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001409 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001410 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001411}
1412
Chad Rosier8a244662013-04-02 20:02:33 +00001413// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001414bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1415 StringRef &Identifier,
1416 InlineAsmIdentifierInfo &Info,
1417 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001418 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1419 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001420
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001421 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001422 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001423
Chad Rosier8a244662013-04-02 20:02:33 +00001424 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001425
1426 // Advance the token stream until the end of the current token is
1427 // after the end of what the frontend claimed.
1428 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1429 while (true) {
1430 End = Tok.getEndLoc();
1431 getLexer().Lex();
1432
1433 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1434 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001435 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001436
1437 // Create the symbol reference.
1438 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001439 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1440 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001441 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001442 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001443}
1444
David Majnemeraa34d792013-08-27 21:56:17 +00001445/// \brief Parse intel style segment override.
1446X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1447 SMLoc Start,
1448 unsigned Size) {
1449 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1450 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1451 if (Tok.isNot(AsmToken::Colon))
1452 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1453 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001454
David Majnemeraa34d792013-08-27 21:56:17 +00001455 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001456 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001457 ImmDisp = Tok.getIntVal();
1458 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1459
Chad Rosier1530ba52013-03-27 21:49:56 +00001460 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001461 InstInfo->AsmRewrites->push_back(
1462 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1463
1464 if (getLexer().isNot(AsmToken::LBrac)) {
1465 // An immediate following a 'segment register', 'colon' token sequence can
1466 // be followed by a bracketed expression. If it isn't we know we have our
1467 // final segment override.
1468 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1469 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1470 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1471 Size);
1472 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001473 }
1474
Chad Rosier91c82662012-10-24 17:22:29 +00001475 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001476 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001477
David Majnemeraa34d792013-08-27 21:56:17 +00001478 const MCExpr *Val;
1479 SMLoc End;
1480 if (!isParsingInlineAsm()) {
1481 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001482 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001483
1484 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001485 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001486
David Majnemeraa34d792013-08-27 21:56:17 +00001487 InlineAsmIdentifierInfo Info;
1488 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001489 if (ParseIntelIdentifier(Val, Identifier, Info,
1490 /*Unevaluated=*/false, End))
1491 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001492 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1493 /*Scale=*/1, Start, End, Size, Identifier, Info);
1494}
1495
1496/// ParseIntelMemOperand - Parse intel style memory operand.
1497X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1498 unsigned Size) {
1499 const AsmToken &Tok = Parser.getTok();
1500 SMLoc End;
1501
1502 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1503 if (getLexer().is(AsmToken::LBrac))
1504 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1505
Chad Rosier95ce8892013-04-19 18:39:50 +00001506 const MCExpr *Val;
1507 if (!isParsingInlineAsm()) {
1508 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001509 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001510
1511 return X86Operand::CreateMem(Val, Start, End, Size);
1512 }
1513
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001514 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001515 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001516 if (ParseIntelIdentifier(Val, Identifier, Info,
1517 /*Unevaluated=*/false, End))
1518 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001519 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001520 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001521}
1522
Chad Rosier5dcb4662012-10-24 22:21:50 +00001523/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001524bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001525 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001526 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001527 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001528
1529 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001530 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001531 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001532 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001533 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001534
1535 // Drop the '.'.
1536 StringRef DotDispStr = Tok.getString().drop_front(1);
1537
Chad Rosier5dcb4662012-10-24 22:21:50 +00001538 // .Imm gets lexed as a real.
1539 if (Tok.is(AsmToken::Real)) {
1540 APInt DotDisp;
1541 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001542 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001543 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001544 unsigned DotDisp;
1545 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1546 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001547 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001548 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001549 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001550 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001551 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001552
Chad Rosier240b7b92012-10-25 21:51:10 +00001553 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1554 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1555 unsigned Len = DotDispStr.size();
1556 unsigned Val = OrigDispVal + DotDispVal;
1557 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1558 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001559 }
1560
Chad Rosiercc541e82013-04-19 15:57:00 +00001561 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001562 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001563}
1564
Chad Rosier91c82662012-10-24 17:22:29 +00001565/// Parse the 'offset' operator. This operator is used to specify the
1566/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001567X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001568 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001569 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001570 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001571
Chad Rosier91c82662012-10-24 17:22:29 +00001572 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001573 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001574 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001575 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001576 if (ParseIntelIdentifier(Val, Identifier, Info,
1577 /*Unevaluated=*/false, End))
1578 return 0;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001579
Chad Rosiere2f03772012-10-26 16:09:20 +00001580 // Don't emit the offset operator.
1581 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1582
Chad Rosier91c82662012-10-24 17:22:29 +00001583 // The offset operator will have an 'r' constraint, thus we need to create
1584 // register operand to ensure proper matching. Just pick a GPR based on
1585 // the size of a pointer.
1586 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001587 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001588 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001589}
1590
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001591enum IntelOperatorKind {
1592 IOK_LENGTH,
1593 IOK_SIZE,
1594 IOK_TYPE
1595};
1596
1597/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1598/// returns the number of elements in an array. It returns the value 1 for
1599/// non-array variables. The SIZE operator returns the size of a C or C++
1600/// variable. A variable's size is the product of its LENGTH and TYPE. The
1601/// TYPE operator returns the size of a C or C++ type or variable. If the
1602/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001603X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001604 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001605 SMLoc TypeLoc = Tok.getLoc();
1606 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001607
Chad Rosier95ce8892013-04-19 18:39:50 +00001608 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001609 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001610 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001611 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001612 if (ParseIntelIdentifier(Val, Identifier, Info,
1613 /*Unevaluated=*/true, End))
1614 return 0;
1615
1616 if (!Info.OpDecl)
1617 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001618
Chad Rosierf6675c32013-04-22 17:01:46 +00001619 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001620 switch(OpKind) {
1621 default: llvm_unreachable("Unexpected operand kind!");
1622 case IOK_LENGTH: CVal = Info.Length; break;
1623 case IOK_SIZE: CVal = Info.Size; break;
1624 case IOK_TYPE: CVal = Info.Type; break;
1625 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001626
1627 // Rewrite the type operator and the C or C++ type or variable in terms of an
1628 // immediate. E.g. TYPE foo -> $$4
1629 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001630 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001631
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001632 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001633 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001634}
1635
Devang Patel41b9dde2012-01-17 18:00:18 +00001636X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001637 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001638 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001639
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001640 // Offset, length, type and size operators.
1641 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001642 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001643 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001644 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001645 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001646 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001647 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001648 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001649 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001650 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001651 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001652
David Majnemeraa34d792013-08-27 21:56:17 +00001653 unsigned Size = getIntelMemOperandSize(Tok.getString());
1654 if (Size) {
1655 Parser.Lex(); // Eat operand size (e.g., byte, word).
1656 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1657 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1658 Parser.Lex(); // Eat ptr.
1659 }
1660 Start = Tok.getLoc();
1661
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001662 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001663 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1664 getLexer().is(AsmToken::LParen)) {
1665 AsmToken StartTok = Tok;
1666 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1667 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001668 if (ParseIntelExpression(SM, End))
1669 return 0;
Chad Rosierbfb70992013-04-17 00:11:46 +00001670
1671 int64_t Imm = SM.getImm();
1672 if (isParsingInlineAsm()) {
1673 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1674 if (StartTok.getString().size() == Len)
1675 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001676 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001677 else
1678 // Otherwise, rewrite the complex expression as a single immediate.
1679 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001680 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001681
1682 if (getLexer().isNot(AsmToken::LBrac)) {
1683 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1684 return X86Operand::CreateImm(ImmExpr, Start, End);
1685 }
1686
1687 // Only positive immediates are valid.
1688 if (Imm < 0)
1689 return ErrorOperand(Start, "expected a positive immediate displacement "
1690 "before bracketed expr.");
1691
1692 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001693 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001694 }
1695
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001696 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001697 unsigned RegNo = 0;
1698 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001699 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001700 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001701 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001702 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001703
David Majnemeraa34d792013-08-27 21:56:17 +00001704 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001705 }
1706
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001707 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001708 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001709}
1710
Devang Patel4a6e7782012-01-12 18:03:40 +00001711X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001712 switch (getLexer().getKind()) {
1713 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001714 // Parse a memory operand with no segment register.
1715 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001716 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001717 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001718 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001719 SMLoc Start, End;
1720 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001721 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001722 Error(Start, "%eiz and %riz can only be used as index registers",
1723 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001724 return 0;
1725 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001726
Chris Lattnerb9270732010-04-17 18:56:34 +00001727 // If this is a segment register followed by a ':', then this is the start
1728 // of a memory reference, otherwise this is a normal register reference.
1729 if (getLexer().isNot(AsmToken::Colon))
1730 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001731
Chris Lattnerb9270732010-04-17 18:56:34 +00001732 getParser().Lex(); // Eat the colon.
1733 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001734 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001735 case AsmToken::Dollar: {
1736 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001737 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001738 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001739 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001740 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001741 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001742 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001743 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001744 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001745}
1746
Chris Lattnerb9270732010-04-17 18:56:34 +00001747/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1748/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001749X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001750
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001751 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1752 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001753 // only way to do this without lookahead is to eat the '(' and see what is
1754 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001755 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001756 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001757 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001758 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001759
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001760 // After parsing the base expression we could either have a parenthesized
1761 // memory address or not. If not, return now. If so, eat the (.
1762 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001763 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001764 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001765 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001766 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001767 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001768
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001769 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001770 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001771 } else {
1772 // Okay, we have a '('. We don't know if this is an expression or not, but
1773 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001774 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001775 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001776
Kevin Enderby7d912182009-09-03 17:15:07 +00001777 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001778 // Nothing to do here, fall into the code below with the '(' part of the
1779 // memory operand consumed.
1780 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001781 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001782
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001783 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001784 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001785 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001786
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001787 // After parsing the base expression we could either have a parenthesized
1788 // memory address or not. If not, return now. If so, eat the (.
1789 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001790 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001791 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001792 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001793 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001794 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001795
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001796 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001797 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001798 }
1799 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001800
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001801 // If we reached here, then we just ate the ( of the memory operand. Process
1802 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001803 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001804 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001805
Chris Lattner0c2538f2010-01-15 18:51:29 +00001806 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001807 SMLoc StartLoc, EndLoc;
1808 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001809 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001810 Error(StartLoc, "eiz and riz can only be used as index registers",
1811 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001812 return 0;
1813 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001814 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001815
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001816 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001817 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001818 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001819
1820 // Following the comma we should have either an index register, or a scale
1821 // value. We don't support the later form, but we want to parse it
1822 // correctly.
1823 //
1824 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001825 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001826 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001827 SMLoc L;
1828 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001829
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001830 if (getLexer().isNot(AsmToken::RParen)) {
1831 // Parse the scale amount:
1832 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001833 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001834 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001835 "expected comma in scale expression");
1836 return 0;
1837 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001838 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001839
1840 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001841 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001842
1843 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001844 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001845 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001846 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001847 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001848
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001849 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001850 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1851 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1852 return 0;
1853 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001854 Scale = (unsigned)ScaleVal;
1855 }
1856 }
1857 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001858 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001859 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001860 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001861
1862 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001863 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001864 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001865
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001866 if (Value != 1)
1867 Warning(Loc, "scale factor without index register is ignored");
1868 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001869 }
1870 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001871
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001872 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001873 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001874 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001875 return 0;
1876 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001877 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001878 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001879
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001880 // If we have both a base register and an index register make sure they are
1881 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001882 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001883 if (BaseReg != 0 && IndexReg != 0) {
1884 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001885 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1886 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001887 IndexReg != X86::RIZ) {
1888 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1889 return 0;
1890 }
1891 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001892 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1893 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001894 IndexReg != X86::EIZ){
1895 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1896 return 0;
1897 }
1898 }
1899
Chris Lattner015cfb12010-01-15 19:33:43 +00001900 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1901 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001902}
1903
Devang Patel4a6e7782012-01-12 18:03:40 +00001904bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001905ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001906 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001907 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001908 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001909
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001910 // FIXME: Hack to recognize setneb as setne.
1911 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1912 PatchedName != "setb" && PatchedName != "setnb")
1913 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001914
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001915 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1916 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001917 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001918 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1919 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001920 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001921 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001922 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001923 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001924 .Case("eq", 0x00)
1925 .Case("lt", 0x01)
1926 .Case("le", 0x02)
1927 .Case("unord", 0x03)
1928 .Case("neq", 0x04)
1929 .Case("nlt", 0x05)
1930 .Case("nle", 0x06)
1931 .Case("ord", 0x07)
1932 /* AVX only from here */
1933 .Case("eq_uq", 0x08)
1934 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001935 .Case("ngt", 0x0A)
1936 .Case("false", 0x0B)
1937 .Case("neq_oq", 0x0C)
1938 .Case("ge", 0x0D)
1939 .Case("gt", 0x0E)
1940 .Case("true", 0x0F)
1941 .Case("eq_os", 0x10)
1942 .Case("lt_oq", 0x11)
1943 .Case("le_oq", 0x12)
1944 .Case("unord_s", 0x13)
1945 .Case("neq_us", 0x14)
1946 .Case("nlt_uq", 0x15)
1947 .Case("nle_uq", 0x16)
1948 .Case("ord_s", 0x17)
1949 .Case("eq_us", 0x18)
1950 .Case("nge_uq", 0x19)
1951 .Case("ngt_uq", 0x1A)
1952 .Case("false_os", 0x1B)
1953 .Case("neq_os", 0x1C)
1954 .Case("ge_oq", 0x1D)
1955 .Case("gt_oq", 0x1E)
1956 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001957 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001958 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001959 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1960 getParser().getContext());
1961 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001962 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001963 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001964 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001965 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001966 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001967 } else {
1968 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001969 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001970 }
1971 }
1972 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001973
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001974 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001975
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001976 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001977 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001978
Chris Lattner086a83a2010-09-08 05:17:37 +00001979 // Determine whether this is an instruction prefix.
1980 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001981 Name == "lock" || Name == "rep" ||
1982 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001983 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001984 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001985
1986
Chris Lattner086a83a2010-09-08 05:17:37 +00001987 // This does the actual operand parsing. Don't parse any more if we have a
1988 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1989 // just want to parse the "lock" as the first instruction and the "incl" as
1990 // the next one.
1991 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001992
1993 // Parse '*' modifier.
1994 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001995 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001996 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001997 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001998 }
1999
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002000 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002001 if (X86Operand *Op = ParseOperand())
2002 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002003 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002004 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002005 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002006 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002007
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002008 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002009 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002010
2011 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002012 if (X86Operand *Op = ParseOperand())
2013 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002014 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002015 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002016 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002017 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002018 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002019
Elena Demikhovsky89529742013-09-12 08:55:00 +00002020 if (STI.getFeatureBits() & X86::FeatureAVX512) {
2021 // Parse mask register {%k1}
2022 if (getLexer().is(AsmToken::LCurly)) {
2023 SMLoc Loc = Parser.getTok().getLoc();
2024 Operands.push_back(X86Operand::CreateToken("{", Loc));
2025 Parser.Lex(); // Eat the {
2026 if (X86Operand *Op = ParseOperand()) {
2027 Operands.push_back(Op);
2028 if (!getLexer().is(AsmToken::RCurly)) {
2029 SMLoc Loc = getLexer().getLoc();
2030 Parser.eatToEndOfStatement();
2031 return Error(Loc, "Expected } at this point");
2032 }
2033 Loc = Parser.getTok().getLoc();
2034 Operands.push_back(X86Operand::CreateToken("}", Loc));
2035 Parser.Lex(); // Eat the }
2036 } else {
2037 Parser.eatToEndOfStatement();
2038 return true;
2039 }
2040 }
2041 // Parse "zeroing non-masked" semantic {z}
2042 if (getLexer().is(AsmToken::LCurly)) {
2043 SMLoc Loc = Parser.getTok().getLoc();
2044 Operands.push_back(X86Operand::CreateToken("{z}", Loc));
2045 Parser.Lex(); // Eat the {
2046 if (!getLexer().is(AsmToken::Identifier) || getLexer().getTok().getIdentifier() != "z") {
2047 SMLoc Loc = getLexer().getLoc();
2048 Parser.eatToEndOfStatement();
2049 return Error(Loc, "Expected z at this point");
2050 }
2051 Parser.Lex(); // Eat the z
2052 if (!getLexer().is(AsmToken::RCurly)) {
2053 SMLoc Loc = getLexer().getLoc();
2054 Parser.eatToEndOfStatement();
2055 return Error(Loc, "Expected } at this point");
2056 }
2057 Parser.Lex(); // Eat the }
2058 }
2059 }
2060
Chris Lattnera2a9d162010-09-11 16:18:25 +00002061 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00002062 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002063 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00002064 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00002065 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002066 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002067
Chris Lattner086a83a2010-09-08 05:17:37 +00002068 if (getLexer().is(AsmToken::EndOfStatement))
2069 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00002070 else if (isPrefix && getLexer().is(AsmToken::Slash))
2071 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002072
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002073 if (ExtraImmOp && isParsingIntelSyntax())
2074 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2075
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002076 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2077 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2078 // documented form in various unofficial manuals, so a lot of code uses it.
2079 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2080 Operands.size() == 3) {
2081 X86Operand &Op = *(X86Operand*)Operands.back();
2082 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2083 isa<MCConstantExpr>(Op.Mem.Disp) &&
2084 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2085 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2086 SMLoc Loc = Op.getEndLoc();
2087 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2088 delete &Op;
2089 }
2090 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002091 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2092 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2093 Operands.size() == 3) {
2094 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2095 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2096 isa<MCConstantExpr>(Op.Mem.Disp) &&
2097 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2098 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2099 SMLoc Loc = Op.getEndLoc();
2100 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2101 delete &Op;
2102 }
2103 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002104 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
2105 if (Name.startswith("ins") && Operands.size() == 3 &&
2106 (Name == "insb" || Name == "insw" || Name == "insl")) {
2107 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2108 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2109 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
2110 Operands.pop_back();
2111 Operands.pop_back();
2112 delete &Op;
2113 delete &Op2;
2114 }
2115 }
2116
2117 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
2118 if (Name.startswith("outs") && Operands.size() == 3 &&
2119 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
2120 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2121 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2122 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2123 Operands.pop_back();
2124 Operands.pop_back();
2125 delete &Op;
2126 delete &Op2;
2127 }
2128 }
2129
2130 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2131 if (Name.startswith("movs") && Operands.size() == 3 &&
2132 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002133 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002134 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2135 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2136 if (isSrcOp(Op) && isDstOp(Op2)) {
2137 Operands.pop_back();
2138 Operands.pop_back();
2139 delete &Op;
2140 delete &Op2;
2141 }
2142 }
2143 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2144 if (Name.startswith("lods") && Operands.size() == 3 &&
2145 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002146 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002147 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2148 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2149 if (isSrcOp(*Op1) && Op2->isReg()) {
2150 const char *ins;
2151 unsigned reg = Op2->getReg();
2152 bool isLods = Name == "lods";
2153 if (reg == X86::AL && (isLods || Name == "lodsb"))
2154 ins = "lodsb";
2155 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2156 ins = "lodsw";
2157 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2158 ins = "lodsl";
2159 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2160 ins = "lodsq";
2161 else
2162 ins = NULL;
2163 if (ins != NULL) {
2164 Operands.pop_back();
2165 Operands.pop_back();
2166 delete Op1;
2167 delete Op2;
2168 if (Name != ins)
2169 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2170 }
2171 }
2172 }
2173 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2174 if (Name.startswith("stos") && Operands.size() == 3 &&
2175 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002176 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002177 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2178 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2179 if (isDstOp(*Op2) && Op1->isReg()) {
2180 const char *ins;
2181 unsigned reg = Op1->getReg();
2182 bool isStos = Name == "stos";
2183 if (reg == X86::AL && (isStos || Name == "stosb"))
2184 ins = "stosb";
2185 else if (reg == X86::AX && (isStos || Name == "stosw"))
2186 ins = "stosw";
2187 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2188 ins = "stosl";
2189 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2190 ins = "stosq";
2191 else
2192 ins = NULL;
2193 if (ins != NULL) {
2194 Operands.pop_back();
2195 Operands.pop_back();
2196 delete Op1;
2197 delete Op2;
2198 if (Name != ins)
2199 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2200 }
2201 }
2202 }
2203
Chris Lattner4bd21712010-09-15 04:33:27 +00002204 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002205 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002206 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002207 Name.startswith("shl") || Name.startswith("sal") ||
2208 Name.startswith("rcl") || Name.startswith("rcr") ||
2209 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002210 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002211 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002212 // Intel syntax
2213 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2214 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002215 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2216 delete Operands[2];
2217 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002218 }
2219 } else {
2220 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2221 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002222 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2223 delete Operands[1];
2224 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002225 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002226 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002227 }
Chad Rosier51afe632012-06-27 22:34:28 +00002228
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002229 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2230 // instalias with an immediate operand yet.
2231 if (Name == "int" && Operands.size() == 2) {
2232 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2233 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2234 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2235 delete Operands[1];
2236 Operands.erase(Operands.begin() + 1);
2237 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2238 }
2239 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002240
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002241 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002242}
2243
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002244static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2245 bool isCmp) {
2246 MCInst TmpInst;
2247 TmpInst.setOpcode(Opcode);
2248 if (!isCmp)
2249 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2250 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2251 TmpInst.addOperand(Inst.getOperand(0));
2252 Inst = TmpInst;
2253 return true;
2254}
2255
2256static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2257 bool isCmp = false) {
2258 if (!Inst.getOperand(0).isImm() ||
2259 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2260 return false;
2261
2262 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2263}
2264
2265static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2266 bool isCmp = false) {
2267 if (!Inst.getOperand(0).isImm() ||
2268 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2269 return false;
2270
2271 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2272}
2273
2274static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2275 bool isCmp = false) {
2276 if (!Inst.getOperand(0).isImm() ||
2277 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2278 return false;
2279
2280 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2281}
2282
Devang Patel4a6e7782012-01-12 18:03:40 +00002283bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002284processInstruction(MCInst &Inst,
2285 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2286 switch (Inst.getOpcode()) {
2287 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002288 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2289 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2290 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2291 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2292 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2293 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2294 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2295 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2296 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2297 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2298 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2299 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2300 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2301 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2302 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2303 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2304 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2305 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002306 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2307 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2308 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2309 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2310 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2311 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002312 case X86::VMOVAPDrr:
2313 case X86::VMOVAPDYrr:
2314 case X86::VMOVAPSrr:
2315 case X86::VMOVAPSYrr:
2316 case X86::VMOVDQArr:
2317 case X86::VMOVDQAYrr:
2318 case X86::VMOVDQUrr:
2319 case X86::VMOVDQUYrr:
2320 case X86::VMOVUPDrr:
2321 case X86::VMOVUPDYrr:
2322 case X86::VMOVUPSrr:
2323 case X86::VMOVUPSYrr: {
2324 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2325 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2326 return false;
2327
2328 unsigned NewOpc;
2329 switch (Inst.getOpcode()) {
2330 default: llvm_unreachable("Invalid opcode");
2331 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2332 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2333 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2334 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2335 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2336 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2337 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2338 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2339 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2340 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2341 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2342 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2343 }
2344 Inst.setOpcode(NewOpc);
2345 return true;
2346 }
2347 case X86::VMOVSDrr:
2348 case X86::VMOVSSrr: {
2349 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2350 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2351 return false;
2352 unsigned NewOpc;
2353 switch (Inst.getOpcode()) {
2354 default: llvm_unreachable("Invalid opcode");
2355 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2356 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2357 }
2358 Inst.setOpcode(NewOpc);
2359 return true;
2360 }
Devang Patelde47cce2012-01-18 22:42:29 +00002361 }
Devang Patelde47cce2012-01-18 22:42:29 +00002362}
2363
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002364static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002365bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002366MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002367 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002368 MCStreamer &Out, unsigned &ErrorInfo,
2369 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002370 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002371 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2372 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002373 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002374
Chris Lattnera63292a2010-09-29 01:50:45 +00002375 // First, handle aliases that expand to multiple instructions.
2376 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002377 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002378 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002379 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002380 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002381 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002382 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002383 MCInst Inst;
2384 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002385 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002386 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002387 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002388
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002389 const char *Repl =
2390 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002391 .Case("finit", "fninit")
2392 .Case("fsave", "fnsave")
2393 .Case("fstcw", "fnstcw")
2394 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002395 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002396 .Case("fstsw", "fnstsw")
2397 .Case("fstsww", "fnstsw")
2398 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002399 .Default(0);
2400 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002401 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002402 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002403 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002404
Chris Lattner628fbec2010-09-06 21:54:15 +00002405 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002406 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002407
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002408 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002409 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002410 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002411 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002412 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002413 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002414 // Some instructions need post-processing to, for example, tweak which
2415 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002416 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002417 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002418 while (processInstruction(Inst, Operands))
2419 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002420
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002421 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002422 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002423 Out.EmitInstruction(Inst);
2424 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002425 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002426 case Match_MissingFeature: {
2427 assert(ErrorInfo && "Unknown missing feature!");
2428 // Special case the error message for the very common case where only
2429 // a single subtarget feature is missing.
2430 std::string Msg = "instruction requires:";
2431 unsigned Mask = 1;
2432 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2433 if (ErrorInfo & Mask) {
2434 Msg += " ";
2435 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2436 }
2437 Mask <<= 1;
2438 }
2439 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2440 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002441 case Match_InvalidOperand:
2442 WasOriginallyInvalidOperand = true;
2443 break;
2444 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002445 break;
2446 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002447
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002448 // FIXME: Ideally, we would only attempt suffix matches for things which are
2449 // valid prefixes, and we could just infer the right unambiguous
2450 // type. However, that requires substantially more matcher support than the
2451 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002452
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002453 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002454 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002455 SmallString<16> Tmp;
2456 Tmp += Base;
2457 Tmp += ' ';
2458 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002459
Chris Lattnerfab94132010-11-06 18:28:02 +00002460 // If this instruction starts with an 'f', then it is a floating point stack
2461 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2462 // 80-bit floating point, which use the suffixes s,l,t respectively.
2463 //
2464 // Otherwise, we assume that this may be an integer instruction, which comes
2465 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2466 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002467
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002468 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002469 Tmp[Base.size()] = Suffixes[0];
2470 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002471 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002472 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002473
Chad Rosier2f480a82012-10-12 22:53:36 +00002474 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002475 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002476 // If this returned as a missing feature failure, remember that.
2477 if (Match1 == Match_MissingFeature)
2478 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002479 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002480 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002481 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002482 // If this returned as a missing feature failure, remember that.
2483 if (Match2 == Match_MissingFeature)
2484 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002485 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002486 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002487 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002488 // If this returned as a missing feature failure, remember that.
2489 if (Match3 == Match_MissingFeature)
2490 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002491 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002492 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002493 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002494 // If this returned as a missing feature failure, remember that.
2495 if (Match4 == Match_MissingFeature)
2496 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002497
2498 // Restore the old token.
2499 Op->setTokenValue(Base);
2500
2501 // If exactly one matched, then we treat that as a successful match (and the
2502 // instruction will already have been filled in correctly, since the failing
2503 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002504 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002505 (Match1 == Match_Success) + (Match2 == Match_Success) +
2506 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002507 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002508 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002509 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002510 Out.EmitInstruction(Inst);
2511 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002512 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002513 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002514
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002515 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002516
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002517 // If we had multiple suffix matches, then identify this as an ambiguous
2518 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002519 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002520 char MatchChars[4];
2521 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002522 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2523 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2524 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2525 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002526
2527 SmallString<126> Msg;
2528 raw_svector_ostream OS(Msg);
2529 OS << "ambiguous instructions require an explicit suffix (could be ";
2530 for (unsigned i = 0; i != NumMatches; ++i) {
2531 if (i != 0)
2532 OS << ", ";
2533 if (i + 1 == NumMatches)
2534 OS << "or ";
2535 OS << "'" << Base << MatchChars[i] << "'";
2536 }
2537 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002538 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002539 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002540 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002541
Chris Lattner628fbec2010-09-06 21:54:15 +00002542 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002543
Chris Lattner628fbec2010-09-06 21:54:15 +00002544 // If all of the instructions reported an invalid mnemonic, then the original
2545 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002546 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2547 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002548 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002549 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002550 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002551 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002552 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002553 }
2554
2555 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002556 if (ErrorInfo != ~0U) {
2557 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002558 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002559 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002560
Chad Rosier49963552012-10-13 00:26:04 +00002561 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002562 if (Operand->getStartLoc().isValid()) {
2563 SMRange OperandRange = Operand->getLocRange();
2564 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002565 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002566 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002567 }
2568
Chad Rosier3d4bc622012-08-21 19:36:59 +00002569 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002570 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002571 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002572
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002573 // If one instruction matched with a missing feature, report this as a
2574 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002575 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2576 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002577 std::string Msg = "instruction requires:";
2578 unsigned Mask = 1;
2579 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2580 if (ErrorInfoMissingFeature & Mask) {
2581 Msg += " ";
2582 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2583 }
2584 Mask <<= 1;
2585 }
2586 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002587 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002588
Chris Lattner628fbec2010-09-06 21:54:15 +00002589 // If one instruction matched with an invalid operand, report this as an
2590 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002591 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2592 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002593 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002594 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002595 return true;
2596 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002597
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002598 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002599 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002600 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002601 return true;
2602}
2603
2604
Devang Patel4a6e7782012-01-12 18:03:40 +00002605bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002606 StringRef IDVal = DirectiveID.getIdentifier();
2607 if (IDVal == ".word")
2608 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002609 else if (IDVal.startswith(".code"))
2610 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002611 else if (IDVal.startswith(".att_syntax")) {
2612 getParser().setAssemblerDialect(0);
2613 return false;
2614 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002615 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002616 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2617 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002618 // FIXME : Handle noprefix
2619 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002620 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002621 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002622 }
2623 return false;
2624 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002625 return true;
2626}
2627
2628/// ParseDirectiveWord
2629/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002630bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002631 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2632 for (;;) {
2633 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002634 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002635 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002636
Eric Christopherbf7bc492013-01-09 03:52:05 +00002637 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002638
Chris Lattner72c0b592010-10-30 17:38:55 +00002639 if (getLexer().is(AsmToken::EndOfStatement))
2640 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002641
Chris Lattner72c0b592010-10-30 17:38:55 +00002642 // FIXME: Improve diagnostic.
2643 if (getLexer().isNot(AsmToken::Comma))
2644 return Error(L, "unexpected token in directive");
2645 Parser.Lex();
2646 }
2647 }
Chad Rosier51afe632012-06-27 22:34:28 +00002648
Chris Lattner72c0b592010-10-30 17:38:55 +00002649 Parser.Lex();
2650 return false;
2651}
2652
Evan Cheng481ebb02011-07-27 00:38:12 +00002653/// ParseDirectiveCode
2654/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002655bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002656 if (IDVal == ".code32") {
2657 Parser.Lex();
2658 if (is64BitMode()) {
2659 SwitchMode();
2660 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2661 }
2662 } else if (IDVal == ".code64") {
2663 Parser.Lex();
2664 if (!is64BitMode()) {
2665 SwitchMode();
2666 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2667 }
2668 } else {
2669 return Error(L, "unexpected directive " + IDVal);
2670 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002671
Evan Cheng481ebb02011-07-27 00:38:12 +00002672 return false;
2673}
Chris Lattner72c0b592010-10-30 17:38:55 +00002674
Daniel Dunbar71475772009-07-17 20:42:00 +00002675// Force static initialization.
2676extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002677 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2678 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002679}
Daniel Dunbar00331992009-07-29 00:02:19 +00002680
Chris Lattner3e4582a2010-09-06 19:11:01 +00002681#define GET_REGISTER_MATCHER
2682#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002683#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002684#include "X86GenAsmMatcher.inc"