blob: 15dcc5cc39b6575044336cd8709ee867c98594d4 [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:
Alp Tokera5b88a52013-12-02 16:06:06 +000053 SMLoc consumeToken() {
54 SMLoc Result = Parser.getTok().getLoc();
55 Parser.Lex();
56 return Result;
57 }
58
Chad Rosier5362af92013-04-16 18:15:40 +000059 enum InfixCalculatorTok {
60 IC_PLUS = 0,
61 IC_MINUS,
62 IC_MULTIPLY,
63 IC_DIVIDE,
64 IC_RPAREN,
65 IC_LPAREN,
66 IC_IMM,
67 IC_REGISTER
68 };
69
70 class InfixCalculator {
71 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
72 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
73 SmallVector<ICToken, 4> PostfixStack;
74
75 public:
76 int64_t popOperand() {
77 assert (!PostfixStack.empty() && "Poped an empty stack!");
78 ICToken Op = PostfixStack.pop_back_val();
79 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
80 && "Expected and immediate or register!");
81 return Op.second;
82 }
83 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
84 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
85 "Unexpected operand!");
86 PostfixStack.push_back(std::make_pair(Op, Val));
87 }
88
Jakub Staszak9c349222013-08-08 15:48:46 +000089 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +000090 void pushOperator(InfixCalculatorTok Op) {
91 // Push the new operator if the stack is empty.
92 if (InfixOperatorStack.empty()) {
93 InfixOperatorStack.push_back(Op);
94 return;
95 }
96
97 // Push the new operator if it has a higher precedence than the operator
98 // on the top of the stack or the operator on the top of the stack is a
99 // left parentheses.
100 unsigned Idx = InfixOperatorStack.size() - 1;
101 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
102 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
103 InfixOperatorStack.push_back(Op);
104 return;
105 }
106
107 // The operator on the top of the stack has higher precedence than the
108 // new operator.
109 unsigned ParenCount = 0;
110 while (1) {
111 // Nothing to process.
112 if (InfixOperatorStack.empty())
113 break;
114
115 Idx = InfixOperatorStack.size() - 1;
116 StackOp = InfixOperatorStack[Idx];
117 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
118 break;
119
120 // If we have an even parentheses count and we see a left parentheses,
121 // then stop processing.
122 if (!ParenCount && StackOp == IC_LPAREN)
123 break;
124
125 if (StackOp == IC_RPAREN) {
126 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000127 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000128 } else if (StackOp == IC_LPAREN) {
129 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000130 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000131 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000132 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000133 PostfixStack.push_back(std::make_pair(StackOp, 0));
134 }
135 }
136 // Push the new operator.
137 InfixOperatorStack.push_back(Op);
138 }
139 int64_t execute() {
140 // Push any remaining operators onto the postfix stack.
141 while (!InfixOperatorStack.empty()) {
142 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
143 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
144 PostfixStack.push_back(std::make_pair(StackOp, 0));
145 }
146
147 if (PostfixStack.empty())
148 return 0;
149
150 SmallVector<ICToken, 16> OperandStack;
151 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
152 ICToken Op = PostfixStack[i];
153 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
154 OperandStack.push_back(Op);
155 } else {
156 assert (OperandStack.size() > 1 && "Too few operands.");
157 int64_t Val;
158 ICToken Op2 = OperandStack.pop_back_val();
159 ICToken Op1 = OperandStack.pop_back_val();
160 switch (Op.first) {
161 default:
162 report_fatal_error("Unexpected operator!");
163 break;
164 case IC_PLUS:
165 Val = Op1.second + Op2.second;
166 OperandStack.push_back(std::make_pair(IC_IMM, Val));
167 break;
168 case IC_MINUS:
169 Val = Op1.second - Op2.second;
170 OperandStack.push_back(std::make_pair(IC_IMM, Val));
171 break;
172 case IC_MULTIPLY:
173 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
174 "Multiply operation with an immediate and a register!");
175 Val = Op1.second * Op2.second;
176 OperandStack.push_back(std::make_pair(IC_IMM, Val));
177 break;
178 case IC_DIVIDE:
179 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
180 "Divide operation with an immediate and a register!");
181 assert (Op2.second != 0 && "Division by zero!");
182 Val = Op1.second / Op2.second;
183 OperandStack.push_back(std::make_pair(IC_IMM, Val));
184 break;
185 }
186 }
187 }
188 assert (OperandStack.size() == 1 && "Expected a single result.");
189 return OperandStack.pop_back_val().second;
190 }
191 };
192
193 enum IntelExprState {
194 IES_PLUS,
195 IES_MINUS,
196 IES_MULTIPLY,
197 IES_DIVIDE,
198 IES_LBRAC,
199 IES_RBRAC,
200 IES_LPAREN,
201 IES_RPAREN,
202 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000203 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000204 IES_IDENTIFIER,
205 IES_ERROR
206 };
207
208 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000209 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000210 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000211 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000212 const MCExpr *Sym;
213 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000214 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000215 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000216 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000217 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000218 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000219 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
220 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000221 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000222
223 unsigned getBaseReg() { return BaseReg; }
224 unsigned getIndexReg() { return IndexReg; }
225 unsigned getScale() { return Scale; }
226 const MCExpr *getSym() { return Sym; }
227 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000228 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000229 bool isValidEndState() {
230 return State == IES_RBRAC || State == IES_INTEGER;
231 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000232 bool getStopOnLBrac() { return StopOnLBrac; }
233 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000234 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000235
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000236 InlineAsmIdentifierInfo &getIdentifierInfo() {
237 return Info;
238 }
239
Chad Rosier5362af92013-04-16 18:15:40 +0000240 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000241 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000242 switch (State) {
243 default:
244 State = IES_ERROR;
245 break;
246 case IES_INTEGER:
247 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000248 case IES_REGISTER:
249 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000250 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000251 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
252 // If we already have a BaseReg, then assume this is the IndexReg with
253 // a scale of 1.
254 if (!BaseReg) {
255 BaseReg = TmpReg;
256 } else {
257 assert (!IndexReg && "BaseReg/IndexReg already set!");
258 IndexReg = TmpReg;
259 Scale = 1;
260 }
261 }
Chad Rosier5362af92013-04-16 18:15:40 +0000262 break;
263 }
Chad Rosier31246272013-04-17 21:01:45 +0000264 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000265 }
266 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000267 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000268 switch (State) {
269 default:
270 State = IES_ERROR;
271 break;
272 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000273 case IES_MULTIPLY:
274 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000275 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000276 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000277 case IES_LBRAC:
278 case IES_RBRAC:
279 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000280 case IES_REGISTER:
281 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000282 // Only push the minus operator if it is not a unary operator.
283 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
284 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
285 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
286 IC.pushOperator(IC_MINUS);
287 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
288 // If we already have a BaseReg, then assume this is the IndexReg with
289 // a scale of 1.
290 if (!BaseReg) {
291 BaseReg = TmpReg;
292 } else {
293 assert (!IndexReg && "BaseReg/IndexReg already set!");
294 IndexReg = TmpReg;
295 Scale = 1;
296 }
Chad Rosier5362af92013-04-16 18:15:40 +0000297 }
Chad Rosier5362af92013-04-16 18:15:40 +0000298 break;
299 }
Chad Rosier31246272013-04-17 21:01:45 +0000300 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000301 }
302 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000303 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000304 switch (State) {
305 default:
306 State = IES_ERROR;
307 break;
308 case IES_PLUS:
309 case IES_LPAREN:
310 State = IES_REGISTER;
311 TmpReg = Reg;
312 IC.pushOperand(IC_REGISTER);
313 break;
Chad Rosier31246272013-04-17 21:01:45 +0000314 case IES_MULTIPLY:
315 // Index Register - Scale * Register
316 if (PrevState == IES_INTEGER) {
317 assert (!IndexReg && "IndexReg already set!");
318 State = IES_REGISTER;
319 IndexReg = Reg;
320 // Get the scale and replace the 'Scale * Register' with '0'.
321 Scale = IC.popOperand();
322 IC.pushOperand(IC_IMM);
323 IC.popOperator();
324 } else {
325 State = IES_ERROR;
326 }
Chad Rosier5362af92013-04-16 18:15:40 +0000327 break;
328 }
Chad Rosier31246272013-04-17 21:01:45 +0000329 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000330 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000331 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000332 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000333 switch (State) {
334 default:
335 State = IES_ERROR;
336 break;
337 case IES_PLUS:
338 case IES_MINUS:
339 State = IES_INTEGER;
340 Sym = SymRef;
341 SymName = SymRefName;
342 IC.pushOperand(IC_IMM);
343 break;
344 }
345 }
346 void onInteger(int64_t TmpInt) {
Chad Rosier31246272013-04-17 21:01:45 +0000347 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000348 switch (State) {
349 default:
350 State = IES_ERROR;
351 break;
352 case IES_PLUS:
353 case IES_MINUS:
Chad Rosier5362af92013-04-16 18:15:40 +0000354 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000355 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000356 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000357 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000358 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
359 // Index Register - Register * Scale
360 assert (!IndexReg && "IndexReg already set!");
361 IndexReg = TmpReg;
362 Scale = TmpInt;
363 // Get the scale and replace the 'Register * Scale' with '0'.
364 IC.popOperator();
365 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
366 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
367 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
368 CurrState == IES_MINUS) {
369 // Unary minus. No need to pop the minus operand because it was never
370 // pushed.
371 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
372 } else {
373 IC.pushOperand(IC_IMM, TmpInt);
374 }
Chad Rosier5362af92013-04-16 18:15:40 +0000375 break;
376 }
Chad Rosier31246272013-04-17 21:01:45 +0000377 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000378 }
379 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000380 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000381 switch (State) {
382 default:
383 State = IES_ERROR;
384 break;
385 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000386 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000387 case IES_RPAREN:
388 State = IES_MULTIPLY;
389 IC.pushOperator(IC_MULTIPLY);
390 break;
391 }
392 }
393 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000394 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000395 switch (State) {
396 default:
397 State = IES_ERROR;
398 break;
399 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000400 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000401 State = IES_DIVIDE;
402 IC.pushOperator(IC_DIVIDE);
403 break;
404 }
405 }
406 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000407 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000408 switch (State) {
409 default:
410 State = IES_ERROR;
411 break;
412 case IES_RBRAC:
413 State = IES_PLUS;
414 IC.pushOperator(IC_PLUS);
415 break;
416 }
417 }
418 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000419 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000420 switch (State) {
421 default:
422 State = IES_ERROR;
423 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000424 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000425 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000426 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000427 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000428 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
429 // If we already have a BaseReg, then assume this is the IndexReg with
430 // a scale of 1.
431 if (!BaseReg) {
432 BaseReg = TmpReg;
433 } else {
434 assert (!IndexReg && "BaseReg/IndexReg already set!");
435 IndexReg = TmpReg;
436 Scale = 1;
437 }
Chad Rosier5362af92013-04-16 18:15:40 +0000438 }
439 break;
440 }
Chad Rosier31246272013-04-17 21:01:45 +0000441 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000442 }
443 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000444 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000445 switch (State) {
446 default:
447 State = IES_ERROR;
448 break;
449 case IES_PLUS:
450 case IES_MINUS:
451 case IES_MULTIPLY:
452 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000453 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000454 // FIXME: We don't handle this type of unary minus, yet.
455 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
456 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
457 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
458 CurrState == IES_MINUS) {
459 State = IES_ERROR;
460 break;
461 }
Chad Rosier5362af92013-04-16 18:15:40 +0000462 State = IES_LPAREN;
463 IC.pushOperator(IC_LPAREN);
464 break;
465 }
Chad Rosier31246272013-04-17 21:01:45 +0000466 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000467 }
468 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000469 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000470 switch (State) {
471 default:
472 State = IES_ERROR;
473 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000474 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000475 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000476 case IES_RPAREN:
477 State = IES_RPAREN;
478 IC.pushOperator(IC_RPAREN);
479 break;
480 }
481 }
482 };
483
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000484 MCAsmParser &getParser() const { return Parser; }
485
486 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
487
Chris Lattnera3a06812011-10-16 04:47:35 +0000488 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000489 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000490 bool MatchingInlineAsm = false) {
491 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000492 return Parser.Error(L, Msg, Ranges);
493 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000494
Devang Patel41b9dde2012-01-17 18:00:18 +0000495 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
496 Error(Loc, Msg);
497 return 0;
498 }
499
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000500 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000501 X86Operand *ParseATTOperand();
502 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000503 X86Operand *ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000504 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000505 X86Operand *ParseIntelOperator(unsigned OpKind);
David Majnemeraa34d792013-08-27 21:56:17 +0000506 X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
507 X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
508 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000509 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000510 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000511 int64_t ImmDisp, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000512 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
513 InlineAsmIdentifierInfo &Info,
514 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000515
Chris Lattnerb9270732010-04-17 18:56:34 +0000516 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000517
Chad Rosier175d0ae2013-04-12 18:21:18 +0000518 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
519 unsigned BaseReg, unsigned IndexReg,
520 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000521 unsigned Size, StringRef Identifier,
522 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000523
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000524 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000525 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000526
Devang Patelde47cce2012-01-18 22:42:29 +0000527 bool processInstruction(MCInst &Inst,
528 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
529
Chad Rosier49963552012-10-13 00:26:04 +0000530 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000531 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000532 MCStreamer &Out, unsigned &ErrorInfo,
533 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000534
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000535 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000536 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000537 bool isSrcOp(X86Operand &Op);
538
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000539 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
540 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000541 bool isDstOp(X86Operand &Op);
542
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000543 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000544 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000545 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000546 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000547 void SwitchMode() {
548 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
549 setAvailableFeatures(FB);
550 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000551
Chad Rosierc2f055d2013-04-18 16:13:18 +0000552 bool isParsingIntelSyntax() {
553 return getParser().getAssemblerDialect();
554 }
555
Daniel Dunbareefe8612010-07-19 05:44:09 +0000556 /// @name Auto-generated Matcher Functions
557 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000558
Chris Lattner3e4582a2010-09-06 19:11:01 +0000559#define GET_ASSEMBLER_HEADER
560#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000561
Daniel Dunbar00331992009-07-29 00:02:19 +0000562 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000563
564public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000565 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
566 const MCInstrInfo &MII)
567 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000568
Daniel Dunbareefe8612010-07-19 05:44:09 +0000569 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000570 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000571 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000572 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000573
Chad Rosierf0e87202012-10-25 20:41:34 +0000574 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
575 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000576 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000577
578 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000579};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000580} // end anonymous namespace
581
Sean Callanan86c11812010-01-23 00:40:33 +0000582/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000583/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000584
Chris Lattner60db0a62010-02-09 00:34:28 +0000585static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000586
587/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000588
Craig Topper6bf3ed42012-07-18 04:59:16 +0000589static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000590 return (( Value <= 0x000000000000007FULL)||
591 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
592 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
593}
594
595static bool isImmSExti32i8Value(uint64_t Value) {
596 return (( Value <= 0x000000000000007FULL)||
597 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
598 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
599}
600
601static bool isImmZExtu32u8Value(uint64_t Value) {
602 return (Value <= 0x00000000000000FFULL);
603}
604
605static bool isImmSExti64i8Value(uint64_t Value) {
606 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000607 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000608}
609
610static bool isImmSExti64i32Value(uint64_t Value) {
611 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000612 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000613}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000614namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000615
616/// X86Operand - Instances of this class represent a parsed X86 machine
617/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000618struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000619 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000620 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000621 Register,
622 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000623 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000624 } Kind;
625
Chris Lattner0c2538f2010-01-15 18:51:29 +0000626 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000627 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000628 StringRef SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000629 void *OpDecl;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000630 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000631
Eric Christopher8996c5d2013-03-15 00:42:55 +0000632 struct TokOp {
633 const char *Data;
634 unsigned Length;
635 };
636
637 struct RegOp {
638 unsigned RegNo;
639 };
640
641 struct ImmOp {
642 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000643 };
644
645 struct MemOp {
646 unsigned SegReg;
647 const MCExpr *Disp;
648 unsigned BaseReg;
649 unsigned IndexReg;
650 unsigned Scale;
651 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000652 };
653
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000654 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000655 struct TokOp Tok;
656 struct RegOp Reg;
657 struct ImmOp Imm;
658 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000659 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000660
Chris Lattner015cfb12010-01-15 19:33:43 +0000661 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000662 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000663
Chad Rosiere81309b2013-04-09 17:53:49 +0000664 StringRef getSymName() { return SymName; }
Chad Rosier732b8372013-04-22 22:04:25 +0000665 void *getOpDecl() { return OpDecl; }
Chad Rosiere81309b2013-04-09 17:53:49 +0000666
Chris Lattner86e61532010-01-15 19:06:59 +0000667 /// getStartLoc - Get the location of the first token of this operand.
668 SMLoc getStartLoc() const { return StartLoc; }
669 /// getEndLoc - Get the location of the last token of this operand.
670 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000671 /// getLocRange - Get the range between the first and last token of this
672 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000673 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000674 /// getOffsetOfLoc - Get the location of the offset operator.
675 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000676
Jim Grosbach602aa902011-07-13 15:34:57 +0000677 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000678
Daniel Dunbare10787e2009-08-07 08:26:05 +0000679 StringRef getToken() const {
680 assert(Kind == Token && "Invalid access!");
681 return StringRef(Tok.Data, Tok.Length);
682 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000683 void setTokenValue(StringRef Value) {
684 assert(Kind == Token && "Invalid access!");
685 Tok.Data = Value.data();
686 Tok.Length = Value.size();
687 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000688
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000689 unsigned getReg() const {
690 assert(Kind == Register && "Invalid access!");
691 return Reg.RegNo;
692 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000693
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000694 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000695 assert(Kind == Immediate && "Invalid access!");
696 return Imm.Val;
697 }
698
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000699 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000700 assert(Kind == Memory && "Invalid access!");
701 return Mem.Disp;
702 }
703 unsigned getMemSegReg() const {
704 assert(Kind == Memory && "Invalid access!");
705 return Mem.SegReg;
706 }
707 unsigned getMemBaseReg() const {
708 assert(Kind == Memory && "Invalid access!");
709 return Mem.BaseReg;
710 }
711 unsigned getMemIndexReg() const {
712 assert(Kind == Memory && "Invalid access!");
713 return Mem.IndexReg;
714 }
715 unsigned getMemScale() const {
716 assert(Kind == Memory && "Invalid access!");
717 return Mem.Scale;
718 }
719
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000720 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000721
722 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000723
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000724 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000725 if (!isImm())
726 return false;
727
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000728 // If this isn't a constant expr, just assume it fits and let relaxation
729 // handle it.
730 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
731 if (!CE)
732 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000733
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000734 // Otherwise, check the value is in a range that makes sense for this
735 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000736 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000737 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000738 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000739 if (!isImm())
740 return false;
741
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000742 // If this isn't a constant expr, just assume it fits and let relaxation
743 // handle it.
744 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
745 if (!CE)
746 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000747
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000748 // Otherwise, check the value is in a range that makes sense for this
749 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000750 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000751 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000752 bool isImmZExtu32u8() const {
753 if (!isImm())
754 return false;
755
756 // If this isn't a constant expr, just assume it fits and let relaxation
757 // handle it.
758 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
759 if (!CE)
760 return true;
761
762 // Otherwise, check the value is in a range that makes sense for this
763 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000764 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000765 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000766 bool isImmSExti64i8() const {
767 if (!isImm())
768 return false;
769
770 // If this isn't a constant expr, just assume it fits and let relaxation
771 // handle it.
772 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
773 if (!CE)
774 return true;
775
776 // Otherwise, check the value is in a range that makes sense for this
777 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000778 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000779 }
780 bool isImmSExti64i32() const {
781 if (!isImm())
782 return false;
783
784 // If this isn't a constant expr, just assume it fits and let relaxation
785 // handle it.
786 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
787 if (!CE)
788 return true;
789
790 // Otherwise, check the value is in a range that makes sense for this
791 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000792 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000793 }
794
Chad Rosier5bca3f92012-10-22 19:50:35 +0000795 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000796 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000797 }
798
Chad Rosiera4bc9432013-01-10 22:10:27 +0000799 bool needAddressOf() const {
800 return AddressOf;
801 }
802
Daniel Dunbare10787e2009-08-07 08:26:05 +0000803 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000804 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000805 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000806 }
Chad Rosier51afe632012-06-27 22:34:28 +0000807 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000808 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000809 }
Chad Rosier51afe632012-06-27 22:34:28 +0000810 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000811 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000812 }
Chad Rosier51afe632012-06-27 22:34:28 +0000813 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000814 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000815 }
Chad Rosier51afe632012-06-27 22:34:28 +0000816 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000817 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000818 }
Chad Rosier51afe632012-06-27 22:34:28 +0000819 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000820 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000821 }
Chad Rosier51afe632012-06-27 22:34:28 +0000822 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000823 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000824 }
Craig Topper8c26c422013-08-25 23:18:05 +0000825 bool isMem512() const {
826 return Kind == Memory && (!Mem.Size || Mem.Size == 512);
827 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000828
Craig Topper01deb5f2012-07-18 04:11:12 +0000829 bool isMemVX32() const {
830 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
831 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
832 }
833 bool isMemVY32() const {
834 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
835 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
836 }
837 bool isMemVX64() const {
838 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
839 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
840 }
841 bool isMemVY64() const {
842 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
843 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
844 }
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000845 bool isMemVZ32() const {
846 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
847 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
848 }
849 bool isMemVZ64() const {
850 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
851 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
852 }
853
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000854 bool isAbsMem() const {
855 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000856 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000857 }
858
Craig Topper18854172013-08-25 22:23:38 +0000859 bool isMemOffs8() const {
860 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
861 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 8);
862 }
863 bool isMemOffs16() const {
864 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
865 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 16);
866 }
867 bool isMemOffs32() const {
868 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
869 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 32);
870 }
871 bool isMemOffs64() const {
872 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
873 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 64);
874 }
875
Daniel Dunbare10787e2009-08-07 08:26:05 +0000876 bool isReg() const { return Kind == Register; }
877
Craig Toppera422b092013-10-14 04:55:01 +0000878 bool isGR32orGR64() const {
879 return Kind == Register &&
880 (X86MCRegisterClasses[X86::GR32RegClassID].contains(getReg()) ||
881 X86MCRegisterClasses[X86::GR64RegClassID].contains(getReg()));
882 }
883
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000884 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
885 // Add as immediates when possible.
886 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
887 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
888 else
889 Inst.addOperand(MCOperand::CreateExpr(Expr));
890 }
891
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000892 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000893 assert(N == 1 && "Invalid number of operands!");
894 Inst.addOperand(MCOperand::CreateReg(getReg()));
895 }
896
Craig Toppera422b092013-10-14 04:55:01 +0000897 static unsigned getGR32FromGR64(unsigned RegNo) {
898 switch (RegNo) {
899 default: llvm_unreachable("Unexpected register");
900 case X86::RAX: return X86::EAX;
901 case X86::RCX: return X86::ECX;
902 case X86::RDX: return X86::EDX;
903 case X86::RBX: return X86::EBX;
904 case X86::RBP: return X86::EBP;
905 case X86::RSP: return X86::ESP;
906 case X86::RSI: return X86::ESI;
907 case X86::RDI: return X86::EDI;
908 case X86::R8: return X86::R8D;
909 case X86::R9: return X86::R9D;
910 case X86::R10: return X86::R10D;
911 case X86::R11: return X86::R11D;
912 case X86::R12: return X86::R12D;
913 case X86::R13: return X86::R13D;
914 case X86::R14: return X86::R14D;
915 case X86::R15: return X86::R15D;
916 case X86::RIP: return X86::EIP;
917 }
918 }
919
920 void addGR32orGR64Operands(MCInst &Inst, unsigned N) const {
921 assert(N == 1 && "Invalid number of operands!");
922 unsigned RegNo = getReg();
923 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo))
924 RegNo = getGR32FromGR64(RegNo);
925 Inst.addOperand(MCOperand::CreateReg(RegNo));
926 }
927
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000928 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000929 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000930 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000931 }
932
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000933 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000934 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000935 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
936 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
937 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000938 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000939 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
940 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000941
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000942 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
943 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000944 // Add as immediates when possible.
945 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
946 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
947 else
948 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000949 }
950
Craig Topper18854172013-08-25 22:23:38 +0000951 void addMemOffsOperands(MCInst &Inst, unsigned N) const {
952 assert((N == 1) && "Invalid number of operands!");
953 // Add as immediates when possible.
954 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
955 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
956 else
957 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
958 }
959
Chris Lattner528d00b2010-01-15 19:28:38 +0000960 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000961 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000962 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000963 Res->Tok.Data = Str.data();
964 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000965 return Res;
966 }
967
Chad Rosier91c82662012-10-24 17:22:29 +0000968 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000969 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000970 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +0000971 StringRef SymName = StringRef(),
972 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +0000973 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000974 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000975 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000976 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000977 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000978 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000979 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000980 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000981
Chad Rosierf3c04f62013-03-19 21:58:18 +0000982 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000983 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000984 Res->Imm.Val = Val;
985 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000986 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000987
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000988 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000989 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +0000990 unsigned Size = 0, StringRef SymName = StringRef(),
991 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000992 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
993 Res->Mem.SegReg = 0;
994 Res->Mem.Disp = Disp;
995 Res->Mem.BaseReg = 0;
996 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000997 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000998 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +0000999 Res->SymName = SymName;
1000 Res->OpDecl = OpDecl;
1001 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001002 return Res;
1003 }
1004
1005 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001006 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
1007 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +00001008 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +00001009 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +00001010 StringRef SymName = StringRef(),
1011 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001012 // We should never just have a displacement, that should be parsed as an
1013 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001014 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
1015
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001016 // The scale should always be one of {1,2,4,8}.
1017 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001018 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +00001019 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001020 Res->Mem.SegReg = SegReg;
1021 Res->Mem.Disp = Disp;
1022 Res->Mem.BaseReg = BaseReg;
1023 Res->Mem.IndexReg = IndexReg;
1024 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +00001025 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001026 Res->SymName = SymName;
1027 Res->OpDecl = OpDecl;
1028 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001029 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001030 }
1031};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00001032
Chris Lattner4eb9df02009-07-29 06:33:53 +00001033} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +00001034
Devang Patel4a6e7782012-01-12 18:03:40 +00001035bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001036 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001037
1038 return (Op.isMem() &&
1039 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
1040 isa<MCConstantExpr>(Op.Mem.Disp) &&
1041 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1042 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
1043}
1044
Devang Patel4a6e7782012-01-12 18:03:40 +00001045bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001046 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001047
Chad Rosier51afe632012-06-27 22:34:28 +00001048 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +00001049 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001050 isa<MCConstantExpr>(Op.Mem.Disp) &&
1051 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1052 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
1053}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001054
Devang Patel4a6e7782012-01-12 18:03:40 +00001055bool X86AsmParser::ParseRegister(unsigned &RegNo,
1056 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001057 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001058 const AsmToken &PercentTok = Parser.getTok();
1059 StartLoc = PercentTok.getLoc();
1060
1061 // If we encounter a %, ignore it. This code handles registers with and
1062 // without the prefix, unprefixed registers can occur in cfi directives.
1063 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001064 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001065
Sean Callanan936b0d32010-01-19 21:44:56 +00001066 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001067 EndLoc = Tok.getEndLoc();
1068
Devang Patelce6a2ca2012-01-20 22:32:05 +00001069 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001070 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001071 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001072 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001073 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001074
Kevin Enderby7d912182009-09-03 17:15:07 +00001075 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001076
Chris Lattner1261b812010-09-22 04:11:10 +00001077 // If the match failed, try the register name as lowercase.
1078 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001079 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001080
Evan Chengeda1d4f2011-07-27 23:22:03 +00001081 if (!is64BitMode()) {
1082 // FIXME: This should be done using Requires<In32BitMode> and
1083 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1084 // checked.
1085 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1086 // REX prefix.
1087 if (RegNo == X86::RIZ ||
1088 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1089 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1090 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001091 return Error(StartLoc, "register %"
1092 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001093 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001094 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001095
Chris Lattner1261b812010-09-22 04:11:10 +00001096 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1097 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001098 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001099 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001100
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001101 // Check to see if we have '(4)' after %st.
1102 if (getLexer().isNot(AsmToken::LParen))
1103 return false;
1104 // Lex the paren.
1105 getParser().Lex();
1106
1107 const AsmToken &IntTok = Parser.getTok();
1108 if (IntTok.isNot(AsmToken::Integer))
1109 return Error(IntTok.getLoc(), "expected stack index");
1110 switch (IntTok.getIntVal()) {
1111 case 0: RegNo = X86::ST0; break;
1112 case 1: RegNo = X86::ST1; break;
1113 case 2: RegNo = X86::ST2; break;
1114 case 3: RegNo = X86::ST3; break;
1115 case 4: RegNo = X86::ST4; break;
1116 case 5: RegNo = X86::ST5; break;
1117 case 6: RegNo = X86::ST6; break;
1118 case 7: RegNo = X86::ST7; break;
1119 default: return Error(IntTok.getLoc(), "invalid stack index");
1120 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001121
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001122 if (getParser().Lex().isNot(AsmToken::RParen))
1123 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001124
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001125 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001126 Parser.Lex(); // Eat ')'
1127 return false;
1128 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001129
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001130 EndLoc = Parser.getTok().getEndLoc();
1131
Chris Lattner80486622010-06-24 07:29:18 +00001132 // If this is "db[0-7]", match it as an alias
1133 // for dr[0-7].
1134 if (RegNo == 0 && Tok.getString().size() == 3 &&
1135 Tok.getString().startswith("db")) {
1136 switch (Tok.getString()[2]) {
1137 case '0': RegNo = X86::DR0; break;
1138 case '1': RegNo = X86::DR1; break;
1139 case '2': RegNo = X86::DR2; break;
1140 case '3': RegNo = X86::DR3; break;
1141 case '4': RegNo = X86::DR4; break;
1142 case '5': RegNo = X86::DR5; break;
1143 case '6': RegNo = X86::DR6; break;
1144 case '7': RegNo = X86::DR7; break;
1145 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001146
Chris Lattner80486622010-06-24 07:29:18 +00001147 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001148 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001149 Parser.Lex(); // Eat it.
1150 return false;
1151 }
1152 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001153
Devang Patelce6a2ca2012-01-20 22:32:05 +00001154 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001155 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001156 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001157 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001158 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001159
Sean Callanana83fd7d2010-01-19 20:27:46 +00001160 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001161 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001162}
1163
Devang Patel4a6e7782012-01-12 18:03:40 +00001164X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001165 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001166 return ParseIntelOperand();
1167 return ParseATTOperand();
1168}
1169
Devang Patel41b9dde2012-01-17 18:00:18 +00001170/// getIntelMemOperandSize - Return intel memory operand size.
1171static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001172 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001173 .Cases("BYTE", "byte", 8)
1174 .Cases("WORD", "word", 16)
1175 .Cases("DWORD", "dword", 32)
1176 .Cases("QWORD", "qword", 64)
1177 .Cases("XWORD", "xword", 80)
1178 .Cases("XMMWORD", "xmmword", 128)
1179 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001180 .Default(0);
1181 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001182}
1183
Chad Rosier175d0ae2013-04-12 18:21:18 +00001184X86Operand *
1185X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1186 unsigned BaseReg, unsigned IndexReg,
1187 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001188 unsigned Size, StringRef Identifier,
1189 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001190 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001191 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1192 // reference. We need an 'r' constraint here, so we need to create register
1193 // operand to ensure proper matching. Just pick a GPR based on the size of
1194 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001195 if (!Info.IsVarDecl) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001196 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1197 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001198 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001199 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001200 if (!Size) {
1201 Size = Info.Type * 8; // Size is in terms of bits in this context.
1202 if (Size)
1203 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1204 /*Len=*/0, Size));
1205 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001206 }
1207
Chad Rosier7ca135b2013-03-19 21:11:56 +00001208 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001209 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001210 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001211 BaseReg = BaseReg ? BaseReg : 1;
1212 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001213 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001214}
1215
Chad Rosierd383db52013-04-12 20:20:54 +00001216static void
1217RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1218 StringRef SymName, int64_t ImmDisp,
1219 int64_t FinalImmDisp, SMLoc &BracLoc,
1220 SMLoc &StartInBrac, SMLoc &End) {
1221 // Remove the '[' and ']' from the IR string.
1222 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1223 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1224
1225 // If ImmDisp is non-zero, then we parsed a displacement before the
1226 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1227 // If ImmDisp doesn't match the displacement computed by the state machine
1228 // then we have an additional displacement in the bracketed expression.
1229 if (ImmDisp != FinalImmDisp) {
1230 if (ImmDisp) {
1231 // We have an immediate displacement before the bracketed expression.
1232 // Adjust this to match the final immediate displacement.
1233 bool Found = false;
1234 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1235 E = AsmRewrites->end(); I != E; ++I) {
1236 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1237 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001238 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1239 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001240 (*I).Kind = AOK_Imm;
1241 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1242 (*I).Val = FinalImmDisp;
1243 Found = true;
1244 break;
1245 }
1246 }
1247 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001248 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001249 } else {
1250 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001251 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001252 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001253 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001254 }
1255 }
1256 // Remove all the ImmPrefix rewrites within the brackets.
1257 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1258 E = AsmRewrites->end(); I != E; ++I) {
1259 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1260 continue;
1261 if ((*I).Kind == AOK_ImmPrefix)
1262 (*I).Kind = AOK_Delete;
1263 }
1264 const char *SymLocPtr = SymName.data();
1265 // Skip everything before the symbol.
1266 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1267 assert(Len > 0 && "Expected a non-negative length.");
1268 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1269 }
1270 // Skip everything after the symbol.
1271 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1272 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1273 assert(Len > 0 && "Expected a non-negative length.");
1274 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1275 }
1276}
1277
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001278bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001279 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001280
Chad Rosier5c118fd2013-01-14 22:31:35 +00001281 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001282 while (!Done) {
1283 bool UpdateLocLex = true;
1284
1285 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1286 // identifier. Don't try an parse it as a register.
1287 if (Tok.getString().startswith("."))
1288 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001289
1290 // If we're parsing an immediate expression, we don't expect a '['.
1291 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1292 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001293
1294 switch (getLexer().getKind()) {
1295 default: {
1296 if (SM.isValidEndState()) {
1297 Done = true;
1298 break;
1299 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001300 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001301 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001302 case AsmToken::EndOfStatement: {
1303 Done = true;
1304 break;
1305 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001306 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001307 // This could be a register or a symbolic displacement.
1308 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001309 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001310 SMLoc IdentLoc = Tok.getLoc();
1311 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001312 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001313 SM.onRegister(TmpReg);
1314 UpdateLocLex = false;
1315 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001316 } else {
1317 if (!isParsingInlineAsm()) {
1318 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001319 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001320 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001321 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001322 if (ParseIntelIdentifier(Val, Identifier, Info,
1323 /*Unevaluated=*/false, End))
1324 return true;
Chad Rosier95ce8892013-04-19 18:39:50 +00001325 }
1326 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001327 UpdateLocLex = false;
1328 break;
1329 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001330 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001331 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001332 case AsmToken::Integer:
Chad Rosierbfb70992013-04-17 00:11:46 +00001333 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001334 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1335 Tok.getLoc()));
1336 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001337 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001338 case AsmToken::Plus: SM.onPlus(); break;
1339 case AsmToken::Minus: SM.onMinus(); break;
1340 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001341 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001342 case AsmToken::LBrac: SM.onLBrac(); break;
1343 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001344 case AsmToken::LParen: SM.onLParen(); break;
1345 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001346 }
Chad Rosier31246272013-04-17 21:01:45 +00001347 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001348 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001349
Alp Tokera5b88a52013-12-02 16:06:06 +00001350 if (!Done && UpdateLocLex)
1351 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001352 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001353 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001354}
1355
1356X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001357 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001358 unsigned Size) {
1359 const AsmToken &Tok = Parser.getTok();
1360 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1361 if (getLexer().isNot(AsmToken::LBrac))
1362 return ErrorOperand(BracLoc, "Expected '[' token!");
1363 Parser.Lex(); // Eat '['
1364
1365 SMLoc StartInBrac = Tok.getLoc();
1366 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1367 // may have already parsed an immediate displacement before the bracketed
1368 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001369 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001370 if (ParseIntelExpression(SM, End))
1371 return 0;
Devang Patel41b9dde2012-01-17 18:00:18 +00001372
Chad Rosier175d0ae2013-04-12 18:21:18 +00001373 const MCExpr *Disp;
1374 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001375 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001376 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001377 if (isParsingInlineAsm())
1378 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001379 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001380 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001381 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001382 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001383 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001384 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001385
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001386 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001387 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001388 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001389 if (ParseIntelDotOperator(Disp, NewDisp))
1390 return 0;
Chad Rosier911c1f32012-10-25 17:37:43 +00001391
Chad Rosier70f47592013-04-10 20:07:47 +00001392 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001393 Parser.Lex(); // Eat the field.
1394 Disp = NewDisp;
1395 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001396
Chad Rosier5c118fd2013-01-14 22:31:35 +00001397 int BaseReg = SM.getBaseReg();
1398 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001399 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001400 if (!isParsingInlineAsm()) {
1401 // handle [-42]
1402 if (!BaseReg && !IndexReg) {
1403 if (!SegReg)
1404 return X86Operand::CreateMem(Disp, Start, End, Size);
1405 else
1406 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1407 }
1408 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1409 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001410 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001411
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001412 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001413 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001414 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001415}
1416
Chad Rosier8a244662013-04-02 20:02:33 +00001417// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001418bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1419 StringRef &Identifier,
1420 InlineAsmIdentifierInfo &Info,
1421 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001422 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1423 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001424
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001425 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001426 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001427
Chad Rosier8a244662013-04-02 20:02:33 +00001428 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001429
1430 // Advance the token stream until the end of the current token is
1431 // after the end of what the frontend claimed.
1432 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1433 while (true) {
1434 End = Tok.getEndLoc();
1435 getLexer().Lex();
1436
1437 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1438 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001439 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001440
1441 // Create the symbol reference.
1442 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001443 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1444 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001445 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001446 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001447}
1448
David Majnemeraa34d792013-08-27 21:56:17 +00001449/// \brief Parse intel style segment override.
1450X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1451 SMLoc Start,
1452 unsigned Size) {
1453 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1454 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1455 if (Tok.isNot(AsmToken::Colon))
1456 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1457 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001458
David Majnemeraa34d792013-08-27 21:56:17 +00001459 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001460 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001461 ImmDisp = Tok.getIntVal();
1462 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1463
Chad Rosier1530ba52013-03-27 21:49:56 +00001464 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001465 InstInfo->AsmRewrites->push_back(
1466 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1467
1468 if (getLexer().isNot(AsmToken::LBrac)) {
1469 // An immediate following a 'segment register', 'colon' token sequence can
1470 // be followed by a bracketed expression. If it isn't we know we have our
1471 // final segment override.
1472 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1473 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1474 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1475 Size);
1476 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001477 }
1478
Chad Rosier91c82662012-10-24 17:22:29 +00001479 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001480 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001481
David Majnemeraa34d792013-08-27 21:56:17 +00001482 const MCExpr *Val;
1483 SMLoc End;
1484 if (!isParsingInlineAsm()) {
1485 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001486 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001487
1488 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001489 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001490
David Majnemeraa34d792013-08-27 21:56:17 +00001491 InlineAsmIdentifierInfo Info;
1492 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001493 if (ParseIntelIdentifier(Val, Identifier, Info,
1494 /*Unevaluated=*/false, End))
1495 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001496 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1497 /*Scale=*/1, Start, End, Size, Identifier, Info);
1498}
1499
1500/// ParseIntelMemOperand - Parse intel style memory operand.
1501X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1502 unsigned Size) {
1503 const AsmToken &Tok = Parser.getTok();
1504 SMLoc End;
1505
1506 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1507 if (getLexer().is(AsmToken::LBrac))
1508 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1509
Chad Rosier95ce8892013-04-19 18:39:50 +00001510 const MCExpr *Val;
1511 if (!isParsingInlineAsm()) {
1512 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001513 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001514
1515 return X86Operand::CreateMem(Val, Start, End, Size);
1516 }
1517
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001518 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001519 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001520 if (ParseIntelIdentifier(Val, Identifier, Info,
1521 /*Unevaluated=*/false, End))
1522 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001523 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001524 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001525}
1526
Chad Rosier5dcb4662012-10-24 22:21:50 +00001527/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001528bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001529 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001530 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001531 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001532
1533 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001534 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001535 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001536 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001537 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001538
1539 // Drop the '.'.
1540 StringRef DotDispStr = Tok.getString().drop_front(1);
1541
Chad Rosier5dcb4662012-10-24 22:21:50 +00001542 // .Imm gets lexed as a real.
1543 if (Tok.is(AsmToken::Real)) {
1544 APInt DotDisp;
1545 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001546 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001547 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001548 unsigned DotDisp;
1549 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1550 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001551 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001552 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001553 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001554 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001555 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001556
Chad Rosier240b7b92012-10-25 21:51:10 +00001557 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1558 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1559 unsigned Len = DotDispStr.size();
1560 unsigned Val = OrigDispVal + DotDispVal;
1561 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1562 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001563 }
1564
Chad Rosiercc541e82013-04-19 15:57:00 +00001565 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001566 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001567}
1568
Chad Rosier91c82662012-10-24 17:22:29 +00001569/// Parse the 'offset' operator. This operator is used to specify the
1570/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001571X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001572 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001573 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001574 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001575
Chad Rosier91c82662012-10-24 17:22:29 +00001576 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001577 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001578 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001579 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001580 if (ParseIntelIdentifier(Val, Identifier, Info,
1581 /*Unevaluated=*/false, End))
1582 return 0;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001583
Chad Rosiere2f03772012-10-26 16:09:20 +00001584 // Don't emit the offset operator.
1585 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1586
Chad Rosier91c82662012-10-24 17:22:29 +00001587 // The offset operator will have an 'r' constraint, thus we need to create
1588 // register operand to ensure proper matching. Just pick a GPR based on
1589 // the size of a pointer.
1590 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001591 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001592 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001593}
1594
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001595enum IntelOperatorKind {
1596 IOK_LENGTH,
1597 IOK_SIZE,
1598 IOK_TYPE
1599};
1600
1601/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1602/// returns the number of elements in an array. It returns the value 1 for
1603/// non-array variables. The SIZE operator returns the size of a C or C++
1604/// variable. A variable's size is the product of its LENGTH and TYPE. The
1605/// TYPE operator returns the size of a C or C++ type or variable. If the
1606/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001607X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001608 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001609 SMLoc TypeLoc = Tok.getLoc();
1610 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001611
Chad Rosier95ce8892013-04-19 18:39:50 +00001612 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001613 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001614 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001615 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001616 if (ParseIntelIdentifier(Val, Identifier, Info,
1617 /*Unevaluated=*/true, End))
1618 return 0;
1619
1620 if (!Info.OpDecl)
1621 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001622
Chad Rosierf6675c32013-04-22 17:01:46 +00001623 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001624 switch(OpKind) {
1625 default: llvm_unreachable("Unexpected operand kind!");
1626 case IOK_LENGTH: CVal = Info.Length; break;
1627 case IOK_SIZE: CVal = Info.Size; break;
1628 case IOK_TYPE: CVal = Info.Type; break;
1629 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001630
1631 // Rewrite the type operator and the C or C++ type or variable in terms of an
1632 // immediate. E.g. TYPE foo -> $$4
1633 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001634 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001635
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001636 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001637 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001638}
1639
Devang Patel41b9dde2012-01-17 18:00:18 +00001640X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001641 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001642 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001643
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001644 // Offset, length, type and size operators.
1645 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001646 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001647 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001648 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001649 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001650 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001651 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001652 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001653 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001654 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001655 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001656
David Majnemeraa34d792013-08-27 21:56:17 +00001657 unsigned Size = getIntelMemOperandSize(Tok.getString());
1658 if (Size) {
1659 Parser.Lex(); // Eat operand size (e.g., byte, word).
1660 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1661 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1662 Parser.Lex(); // Eat ptr.
1663 }
1664 Start = Tok.getLoc();
1665
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001666 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001667 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1668 getLexer().is(AsmToken::LParen)) {
1669 AsmToken StartTok = Tok;
1670 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1671 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001672 if (ParseIntelExpression(SM, End))
1673 return 0;
Chad Rosierbfb70992013-04-17 00:11:46 +00001674
1675 int64_t Imm = SM.getImm();
1676 if (isParsingInlineAsm()) {
1677 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1678 if (StartTok.getString().size() == Len)
1679 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001680 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001681 else
1682 // Otherwise, rewrite the complex expression as a single immediate.
1683 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001684 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001685
1686 if (getLexer().isNot(AsmToken::LBrac)) {
1687 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1688 return X86Operand::CreateImm(ImmExpr, Start, End);
1689 }
1690
1691 // Only positive immediates are valid.
1692 if (Imm < 0)
1693 return ErrorOperand(Start, "expected a positive immediate displacement "
1694 "before bracketed expr.");
1695
1696 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001697 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001698 }
1699
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001700 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001701 unsigned RegNo = 0;
1702 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001703 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001704 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001705 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001706 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001707
David Majnemeraa34d792013-08-27 21:56:17 +00001708 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001709 }
1710
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001711 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001712 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001713}
1714
Devang Patel4a6e7782012-01-12 18:03:40 +00001715X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001716 switch (getLexer().getKind()) {
1717 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001718 // Parse a memory operand with no segment register.
1719 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001720 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001721 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001722 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001723 SMLoc Start, End;
1724 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001725 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001726 Error(Start, "%eiz and %riz can only be used as index registers",
1727 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001728 return 0;
1729 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001730
Chris Lattnerb9270732010-04-17 18:56:34 +00001731 // If this is a segment register followed by a ':', then this is the start
1732 // of a memory reference, otherwise this is a normal register reference.
1733 if (getLexer().isNot(AsmToken::Colon))
1734 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001735
Chris Lattnerb9270732010-04-17 18:56:34 +00001736 getParser().Lex(); // Eat the colon.
1737 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001738 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001739 case AsmToken::Dollar: {
1740 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001741 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001742 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001743 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001744 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001745 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001746 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001747 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001748 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001749}
1750
Chris Lattnerb9270732010-04-17 18:56:34 +00001751/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1752/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001753X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001754
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001755 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1756 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001757 // only way to do this without lookahead is to eat the '(' and see what is
1758 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001759 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001760 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001761 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001762 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001763
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001764 // After parsing the base expression we could either have a parenthesized
1765 // memory address or not. If not, return now. If so, eat the (.
1766 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001767 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001768 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001769 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001770 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001771 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001772
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001773 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001774 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001775 } else {
1776 // Okay, we have a '('. We don't know if this is an expression or not, but
1777 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001778 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001779 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001780
Kevin Enderby7d912182009-09-03 17:15:07 +00001781 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001782 // Nothing to do here, fall into the code below with the '(' part of the
1783 // memory operand consumed.
1784 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001785 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001786
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001787 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001788 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001789 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001790
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001791 // After parsing the base expression we could either have a parenthesized
1792 // memory address or not. If not, return now. If so, eat the (.
1793 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001794 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001795 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001796 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001797 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001798 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001799
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001800 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001801 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001802 }
1803 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001804
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001805 // If we reached here, then we just ate the ( of the memory operand. Process
1806 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001807 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001808 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001809
Chris Lattner0c2538f2010-01-15 18:51:29 +00001810 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001811 SMLoc StartLoc, EndLoc;
1812 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001813 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001814 Error(StartLoc, "eiz and riz can only be used as index registers",
1815 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001816 return 0;
1817 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001818 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001819
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001820 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001821 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001822 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001823
1824 // Following the comma we should have either an index register, or a scale
1825 // value. We don't support the later form, but we want to parse it
1826 // correctly.
1827 //
1828 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001829 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001830 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001831 SMLoc L;
1832 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001833
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001834 if (getLexer().isNot(AsmToken::RParen)) {
1835 // Parse the scale amount:
1836 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001837 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001838 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001839 "expected comma in scale expression");
1840 return 0;
1841 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001842 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001843
1844 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001845 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001846
1847 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001848 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001849 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001850 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001851 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001852
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001853 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001854 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1855 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1856 return 0;
1857 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001858 Scale = (unsigned)ScaleVal;
1859 }
1860 }
1861 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001862 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001863 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001864 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001865
1866 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001867 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001868 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001869
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001870 if (Value != 1)
1871 Warning(Loc, "scale factor without index register is ignored");
1872 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001873 }
1874 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001875
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001876 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001877 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001878 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001879 return 0;
1880 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001881 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001882 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001883
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001884 // If we have both a base register and an index register make sure they are
1885 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001886 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001887 if (BaseReg != 0 && IndexReg != 0) {
1888 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001889 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1890 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001891 IndexReg != X86::RIZ) {
1892 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1893 return 0;
1894 }
1895 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001896 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1897 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001898 IndexReg != X86::EIZ){
1899 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1900 return 0;
1901 }
1902 }
1903
Chris Lattner015cfb12010-01-15 19:33:43 +00001904 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1905 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001906}
1907
Devang Patel4a6e7782012-01-12 18:03:40 +00001908bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001909ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001910 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001911 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001912 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001913
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001914 // FIXME: Hack to recognize setneb as setne.
1915 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1916 PatchedName != "setb" && PatchedName != "setnb")
1917 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001918
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001919 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1920 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001921 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001922 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1923 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001924 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001925 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001926 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001927 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001928 .Case("eq", 0x00)
1929 .Case("lt", 0x01)
1930 .Case("le", 0x02)
1931 .Case("unord", 0x03)
1932 .Case("neq", 0x04)
1933 .Case("nlt", 0x05)
1934 .Case("nle", 0x06)
1935 .Case("ord", 0x07)
1936 /* AVX only from here */
1937 .Case("eq_uq", 0x08)
1938 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001939 .Case("ngt", 0x0A)
1940 .Case("false", 0x0B)
1941 .Case("neq_oq", 0x0C)
1942 .Case("ge", 0x0D)
1943 .Case("gt", 0x0E)
1944 .Case("true", 0x0F)
1945 .Case("eq_os", 0x10)
1946 .Case("lt_oq", 0x11)
1947 .Case("le_oq", 0x12)
1948 .Case("unord_s", 0x13)
1949 .Case("neq_us", 0x14)
1950 .Case("nlt_uq", 0x15)
1951 .Case("nle_uq", 0x16)
1952 .Case("ord_s", 0x17)
1953 .Case("eq_us", 0x18)
1954 .Case("nge_uq", 0x19)
1955 .Case("ngt_uq", 0x1A)
1956 .Case("false_os", 0x1B)
1957 .Case("neq_os", 0x1C)
1958 .Case("ge_oq", 0x1D)
1959 .Case("gt_oq", 0x1E)
1960 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001961 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001962 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001963 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1964 getParser().getContext());
1965 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001966 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001967 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001968 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001969 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001970 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001971 } else {
1972 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001973 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001974 }
1975 }
1976 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001977
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001978 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001979
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001980 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001981 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001982
Chris Lattner086a83a2010-09-08 05:17:37 +00001983 // Determine whether this is an instruction prefix.
1984 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001985 Name == "lock" || Name == "rep" ||
1986 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001987 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001988 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001989
1990
Chris Lattner086a83a2010-09-08 05:17:37 +00001991 // This does the actual operand parsing. Don't parse any more if we have a
1992 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1993 // just want to parse the "lock" as the first instruction and the "incl" as
1994 // the next one.
1995 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001996
1997 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00001998 if (getLexer().is(AsmToken::Star))
1999 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002000
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002001 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002002 if (X86Operand *Op = ParseOperand())
2003 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002004 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002005 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002006 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002007 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002008
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002009 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002010 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002011
2012 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002013 if (X86Operand *Op = ParseOperand())
2014 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002015 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002016 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002017 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002018 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002019 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002020
Elena Demikhovsky89529742013-09-12 08:55:00 +00002021 if (STI.getFeatureBits() & X86::FeatureAVX512) {
2022 // Parse mask register {%k1}
2023 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002024 Operands.push_back(X86Operand::CreateToken("{", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002025 if (X86Operand *Op = ParseOperand()) {
2026 Operands.push_back(Op);
2027 if (!getLexer().is(AsmToken::RCurly)) {
2028 SMLoc Loc = getLexer().getLoc();
2029 Parser.eatToEndOfStatement();
2030 return Error(Loc, "Expected } at this point");
2031 }
Alp Tokera5b88a52013-12-02 16:06:06 +00002032 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002033 } else {
2034 Parser.eatToEndOfStatement();
2035 return true;
2036 }
2037 }
2038 // Parse "zeroing non-masked" semantic {z}
2039 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002040 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002041 if (!getLexer().is(AsmToken::Identifier) || getLexer().getTok().getIdentifier() != "z") {
2042 SMLoc Loc = getLexer().getLoc();
2043 Parser.eatToEndOfStatement();
2044 return Error(Loc, "Expected z at this point");
2045 }
2046 Parser.Lex(); // Eat the z
2047 if (!getLexer().is(AsmToken::RCurly)) {
2048 SMLoc Loc = getLexer().getLoc();
2049 Parser.eatToEndOfStatement();
2050 return Error(Loc, "Expected } at this point");
2051 }
2052 Parser.Lex(); // Eat the }
2053 }
2054 }
2055
Chris Lattnera2a9d162010-09-11 16:18:25 +00002056 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00002057 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002058 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00002059 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00002060 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002061 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002062
Chris Lattner086a83a2010-09-08 05:17:37 +00002063 if (getLexer().is(AsmToken::EndOfStatement))
2064 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00002065 else if (isPrefix && getLexer().is(AsmToken::Slash))
2066 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002067
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002068 if (ExtraImmOp && isParsingIntelSyntax())
2069 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2070
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002071 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2072 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2073 // documented form in various unofficial manuals, so a lot of code uses it.
2074 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2075 Operands.size() == 3) {
2076 X86Operand &Op = *(X86Operand*)Operands.back();
2077 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2078 isa<MCConstantExpr>(Op.Mem.Disp) &&
2079 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2080 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2081 SMLoc Loc = Op.getEndLoc();
2082 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2083 delete &Op;
2084 }
2085 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002086 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2087 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2088 Operands.size() == 3) {
2089 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2090 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2091 isa<MCConstantExpr>(Op.Mem.Disp) &&
2092 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2093 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2094 SMLoc Loc = Op.getEndLoc();
2095 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2096 delete &Op;
2097 }
2098 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002099 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
2100 if (Name.startswith("ins") && Operands.size() == 3 &&
2101 (Name == "insb" || Name == "insw" || Name == "insl")) {
2102 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2103 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2104 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
2105 Operands.pop_back();
2106 Operands.pop_back();
2107 delete &Op;
2108 delete &Op2;
2109 }
2110 }
2111
2112 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
2113 if (Name.startswith("outs") && Operands.size() == 3 &&
2114 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
2115 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2116 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2117 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2118 Operands.pop_back();
2119 Operands.pop_back();
2120 delete &Op;
2121 delete &Op2;
2122 }
2123 }
2124
2125 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2126 if (Name.startswith("movs") && Operands.size() == 3 &&
2127 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002128 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002129 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2130 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2131 if (isSrcOp(Op) && isDstOp(Op2)) {
2132 Operands.pop_back();
2133 Operands.pop_back();
2134 delete &Op;
2135 delete &Op2;
2136 }
2137 }
2138 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2139 if (Name.startswith("lods") && Operands.size() == 3 &&
2140 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002141 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002142 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2143 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2144 if (isSrcOp(*Op1) && Op2->isReg()) {
2145 const char *ins;
2146 unsigned reg = Op2->getReg();
2147 bool isLods = Name == "lods";
2148 if (reg == X86::AL && (isLods || Name == "lodsb"))
2149 ins = "lodsb";
2150 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2151 ins = "lodsw";
2152 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2153 ins = "lodsl";
2154 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2155 ins = "lodsq";
2156 else
2157 ins = NULL;
2158 if (ins != NULL) {
2159 Operands.pop_back();
2160 Operands.pop_back();
2161 delete Op1;
2162 delete Op2;
2163 if (Name != ins)
2164 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2165 }
2166 }
2167 }
2168 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2169 if (Name.startswith("stos") && Operands.size() == 3 &&
2170 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002171 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002172 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2173 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2174 if (isDstOp(*Op2) && Op1->isReg()) {
2175 const char *ins;
2176 unsigned reg = Op1->getReg();
2177 bool isStos = Name == "stos";
2178 if (reg == X86::AL && (isStos || Name == "stosb"))
2179 ins = "stosb";
2180 else if (reg == X86::AX && (isStos || Name == "stosw"))
2181 ins = "stosw";
2182 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2183 ins = "stosl";
2184 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2185 ins = "stosq";
2186 else
2187 ins = NULL;
2188 if (ins != NULL) {
2189 Operands.pop_back();
2190 Operands.pop_back();
2191 delete Op1;
2192 delete Op2;
2193 if (Name != ins)
2194 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2195 }
2196 }
2197 }
2198
Chris Lattner4bd21712010-09-15 04:33:27 +00002199 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002200 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002201 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002202 Name.startswith("shl") || Name.startswith("sal") ||
2203 Name.startswith("rcl") || Name.startswith("rcr") ||
2204 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002205 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002206 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002207 // Intel syntax
2208 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2209 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002210 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2211 delete Operands[2];
2212 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002213 }
2214 } else {
2215 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2216 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002217 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2218 delete Operands[1];
2219 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002220 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002221 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002222 }
Chad Rosier51afe632012-06-27 22:34:28 +00002223
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002224 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2225 // instalias with an immediate operand yet.
2226 if (Name == "int" && Operands.size() == 2) {
2227 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2228 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2229 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2230 delete Operands[1];
2231 Operands.erase(Operands.begin() + 1);
2232 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2233 }
2234 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002235
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002236 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002237}
2238
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002239static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2240 bool isCmp) {
2241 MCInst TmpInst;
2242 TmpInst.setOpcode(Opcode);
2243 if (!isCmp)
2244 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2245 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2246 TmpInst.addOperand(Inst.getOperand(0));
2247 Inst = TmpInst;
2248 return true;
2249}
2250
2251static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2252 bool isCmp = false) {
2253 if (!Inst.getOperand(0).isImm() ||
2254 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2255 return false;
2256
2257 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2258}
2259
2260static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2261 bool isCmp = false) {
2262 if (!Inst.getOperand(0).isImm() ||
2263 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2264 return false;
2265
2266 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2267}
2268
2269static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2270 bool isCmp = false) {
2271 if (!Inst.getOperand(0).isImm() ||
2272 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2273 return false;
2274
2275 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2276}
2277
Devang Patel4a6e7782012-01-12 18:03:40 +00002278bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002279processInstruction(MCInst &Inst,
2280 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2281 switch (Inst.getOpcode()) {
2282 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002283 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2284 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2285 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2286 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2287 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2288 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2289 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2290 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2291 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2292 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2293 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2294 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2295 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2296 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2297 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2298 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2299 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2300 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002301 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2302 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2303 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2304 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2305 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2306 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002307 case X86::VMOVAPDrr:
2308 case X86::VMOVAPDYrr:
2309 case X86::VMOVAPSrr:
2310 case X86::VMOVAPSYrr:
2311 case X86::VMOVDQArr:
2312 case X86::VMOVDQAYrr:
2313 case X86::VMOVDQUrr:
2314 case X86::VMOVDQUYrr:
2315 case X86::VMOVUPDrr:
2316 case X86::VMOVUPDYrr:
2317 case X86::VMOVUPSrr:
2318 case X86::VMOVUPSYrr: {
2319 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2320 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2321 return false;
2322
2323 unsigned NewOpc;
2324 switch (Inst.getOpcode()) {
2325 default: llvm_unreachable("Invalid opcode");
2326 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2327 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2328 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2329 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2330 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2331 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2332 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2333 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2334 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2335 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2336 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2337 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2338 }
2339 Inst.setOpcode(NewOpc);
2340 return true;
2341 }
2342 case X86::VMOVSDrr:
2343 case X86::VMOVSSrr: {
2344 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2345 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2346 return false;
2347 unsigned NewOpc;
2348 switch (Inst.getOpcode()) {
2349 default: llvm_unreachable("Invalid opcode");
2350 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2351 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2352 }
2353 Inst.setOpcode(NewOpc);
2354 return true;
2355 }
Devang Patelde47cce2012-01-18 22:42:29 +00002356 }
Devang Patelde47cce2012-01-18 22:42:29 +00002357}
2358
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002359static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002360bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002361MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002362 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002363 MCStreamer &Out, unsigned &ErrorInfo,
2364 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002365 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002366 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2367 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002368 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002369
Chris Lattnera63292a2010-09-29 01:50:45 +00002370 // First, handle aliases that expand to multiple instructions.
2371 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002372 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002373 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002374 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002375 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002376 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002377 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002378 MCInst Inst;
2379 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002380 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002381 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002382 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002383
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002384 const char *Repl =
2385 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002386 .Case("finit", "fninit")
2387 .Case("fsave", "fnsave")
2388 .Case("fstcw", "fnstcw")
2389 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002390 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002391 .Case("fstsw", "fnstsw")
2392 .Case("fstsww", "fnstsw")
2393 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002394 .Default(0);
2395 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002396 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002397 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002398 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002399
Chris Lattner628fbec2010-09-06 21:54:15 +00002400 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002401 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002402
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002403 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002404 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002405 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002406 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002407 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002408 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002409 // Some instructions need post-processing to, for example, tweak which
2410 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002411 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002412 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002413 while (processInstruction(Inst, Operands))
2414 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002415
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002416 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002417 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002418 Out.EmitInstruction(Inst);
2419 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002420 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002421 case Match_MissingFeature: {
2422 assert(ErrorInfo && "Unknown missing feature!");
2423 // Special case the error message for the very common case where only
2424 // a single subtarget feature is missing.
2425 std::string Msg = "instruction requires:";
2426 unsigned Mask = 1;
2427 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2428 if (ErrorInfo & Mask) {
2429 Msg += " ";
2430 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2431 }
2432 Mask <<= 1;
2433 }
2434 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2435 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002436 case Match_InvalidOperand:
2437 WasOriginallyInvalidOperand = true;
2438 break;
2439 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002440 break;
2441 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002442
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002443 // FIXME: Ideally, we would only attempt suffix matches for things which are
2444 // valid prefixes, and we could just infer the right unambiguous
2445 // type. However, that requires substantially more matcher support than the
2446 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002447
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002448 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002449 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002450 SmallString<16> Tmp;
2451 Tmp += Base;
2452 Tmp += ' ';
2453 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002454
Chris Lattnerfab94132010-11-06 18:28:02 +00002455 // If this instruction starts with an 'f', then it is a floating point stack
2456 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2457 // 80-bit floating point, which use the suffixes s,l,t respectively.
2458 //
2459 // Otherwise, we assume that this may be an integer instruction, which comes
2460 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2461 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002462
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002463 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002464 Tmp[Base.size()] = Suffixes[0];
2465 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002466 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002467 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002468
Chad Rosier2f480a82012-10-12 22:53:36 +00002469 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002470 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002471 // If this returned as a missing feature failure, remember that.
2472 if (Match1 == Match_MissingFeature)
2473 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002474 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002475 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002476 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002477 // If this returned as a missing feature failure, remember that.
2478 if (Match2 == Match_MissingFeature)
2479 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002480 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002481 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002482 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002483 // If this returned as a missing feature failure, remember that.
2484 if (Match3 == Match_MissingFeature)
2485 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002486 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002487 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002488 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002489 // If this returned as a missing feature failure, remember that.
2490 if (Match4 == Match_MissingFeature)
2491 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002492
2493 // Restore the old token.
2494 Op->setTokenValue(Base);
2495
2496 // If exactly one matched, then we treat that as a successful match (and the
2497 // instruction will already have been filled in correctly, since the failing
2498 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002499 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002500 (Match1 == Match_Success) + (Match2 == Match_Success) +
2501 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002502 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002503 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002504 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002505 Out.EmitInstruction(Inst);
2506 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002507 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002508 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002509
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002510 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002511
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002512 // If we had multiple suffix matches, then identify this as an ambiguous
2513 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002514 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002515 char MatchChars[4];
2516 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002517 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2518 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2519 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2520 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002521
2522 SmallString<126> Msg;
2523 raw_svector_ostream OS(Msg);
2524 OS << "ambiguous instructions require an explicit suffix (could be ";
2525 for (unsigned i = 0; i != NumMatches; ++i) {
2526 if (i != 0)
2527 OS << ", ";
2528 if (i + 1 == NumMatches)
2529 OS << "or ";
2530 OS << "'" << Base << MatchChars[i] << "'";
2531 }
2532 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002533 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002534 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002535 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002536
Chris Lattner628fbec2010-09-06 21:54:15 +00002537 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002538
Chris Lattner628fbec2010-09-06 21:54:15 +00002539 // If all of the instructions reported an invalid mnemonic, then the original
2540 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002541 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2542 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002543 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002544 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002545 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002546 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002547 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002548 }
2549
2550 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002551 if (ErrorInfo != ~0U) {
2552 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002553 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002554 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002555
Chad Rosier49963552012-10-13 00:26:04 +00002556 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002557 if (Operand->getStartLoc().isValid()) {
2558 SMRange OperandRange = Operand->getLocRange();
2559 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002560 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002561 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002562 }
2563
Chad Rosier3d4bc622012-08-21 19:36:59 +00002564 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002565 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002566 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002567
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002568 // If one instruction matched with a missing feature, report this as a
2569 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002570 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2571 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002572 std::string Msg = "instruction requires:";
2573 unsigned Mask = 1;
2574 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2575 if (ErrorInfoMissingFeature & Mask) {
2576 Msg += " ";
2577 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2578 }
2579 Mask <<= 1;
2580 }
2581 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002582 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002583
Chris Lattner628fbec2010-09-06 21:54:15 +00002584 // If one instruction matched with an invalid operand, report this as an
2585 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002586 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2587 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002588 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002589 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002590 return true;
2591 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002592
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002593 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002594 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002595 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002596 return true;
2597}
2598
2599
Devang Patel4a6e7782012-01-12 18:03:40 +00002600bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002601 StringRef IDVal = DirectiveID.getIdentifier();
2602 if (IDVal == ".word")
2603 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002604 else if (IDVal.startswith(".code"))
2605 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002606 else if (IDVal.startswith(".att_syntax")) {
2607 getParser().setAssemblerDialect(0);
2608 return false;
2609 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002610 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002611 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2612 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002613 // FIXME : Handle noprefix
2614 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002615 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002616 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002617 }
2618 return false;
2619 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002620 return true;
2621}
2622
2623/// ParseDirectiveWord
2624/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002625bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2627 for (;;) {
2628 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002629 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002630 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002631
Eric Christopherbf7bc492013-01-09 03:52:05 +00002632 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002633
Chris Lattner72c0b592010-10-30 17:38:55 +00002634 if (getLexer().is(AsmToken::EndOfStatement))
2635 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002636
Chris Lattner72c0b592010-10-30 17:38:55 +00002637 // FIXME: Improve diagnostic.
2638 if (getLexer().isNot(AsmToken::Comma))
2639 return Error(L, "unexpected token in directive");
2640 Parser.Lex();
2641 }
2642 }
Chad Rosier51afe632012-06-27 22:34:28 +00002643
Chris Lattner72c0b592010-10-30 17:38:55 +00002644 Parser.Lex();
2645 return false;
2646}
2647
Evan Cheng481ebb02011-07-27 00:38:12 +00002648/// ParseDirectiveCode
2649/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002650bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002651 if (IDVal == ".code32") {
2652 Parser.Lex();
2653 if (is64BitMode()) {
2654 SwitchMode();
2655 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2656 }
2657 } else if (IDVal == ".code64") {
2658 Parser.Lex();
2659 if (!is64BitMode()) {
2660 SwitchMode();
2661 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2662 }
2663 } else {
2664 return Error(L, "unexpected directive " + IDVal);
2665 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002666
Evan Cheng481ebb02011-07-27 00:38:12 +00002667 return false;
2668}
Chris Lattner72c0b592010-10-30 17:38:55 +00002669
Daniel Dunbar71475772009-07-17 20:42:00 +00002670// Force static initialization.
2671extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002672 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2673 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002674}
Daniel Dunbar00331992009-07-29 00:02:19 +00002675
Chris Lattner3e4582a2010-09-06 19:11:01 +00002676#define GET_REGISTER_MATCHER
2677#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002678#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002679#include "X86GenAsmMatcher.inc"