blob: 39e58557d288ef2fc7f81e65164add3d532b6fab [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[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000038 0, // IC_OR
39 1, // IC_AND
40 2, // IC_PLUS
41 2, // IC_MINUS
42 3, // IC_MULTIPLY
43 3, // IC_DIVIDE
44 4, // IC_RPAREN
45 5, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000046 0, // IC_IMM
47 0 // IC_REGISTER
48};
49
Devang Patel4a6e7782012-01-12 18:03:40 +000050class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000051 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000052 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000053 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000054private:
Alp Tokera5b88a52013-12-02 16:06:06 +000055 SMLoc consumeToken() {
56 SMLoc Result = Parser.getTok().getLoc();
57 Parser.Lex();
58 return Result;
59 }
60
Chad Rosier5362af92013-04-16 18:15:40 +000061 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000062 IC_OR = 0,
63 IC_AND,
64 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000065 IC_MINUS,
66 IC_MULTIPLY,
67 IC_DIVIDE,
68 IC_RPAREN,
69 IC_LPAREN,
70 IC_IMM,
71 IC_REGISTER
72 };
73
74 class InfixCalculator {
75 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
76 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
77 SmallVector<ICToken, 4> PostfixStack;
78
79 public:
80 int64_t popOperand() {
81 assert (!PostfixStack.empty() && "Poped an empty stack!");
82 ICToken Op = PostfixStack.pop_back_val();
83 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
84 && "Expected and immediate or register!");
85 return Op.second;
86 }
87 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
88 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
89 "Unexpected operand!");
90 PostfixStack.push_back(std::make_pair(Op, Val));
91 }
92
Jakub Staszak9c349222013-08-08 15:48:46 +000093 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +000094 void pushOperator(InfixCalculatorTok Op) {
95 // Push the new operator if the stack is empty.
96 if (InfixOperatorStack.empty()) {
97 InfixOperatorStack.push_back(Op);
98 return;
99 }
100
101 // Push the new operator if it has a higher precedence than the operator
102 // on the top of the stack or the operator on the top of the stack is a
103 // left parentheses.
104 unsigned Idx = InfixOperatorStack.size() - 1;
105 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
106 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
107 InfixOperatorStack.push_back(Op);
108 return;
109 }
110
111 // The operator on the top of the stack has higher precedence than the
112 // new operator.
113 unsigned ParenCount = 0;
114 while (1) {
115 // Nothing to process.
116 if (InfixOperatorStack.empty())
117 break;
118
119 Idx = InfixOperatorStack.size() - 1;
120 StackOp = InfixOperatorStack[Idx];
121 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
122 break;
123
124 // If we have an even parentheses count and we see a left parentheses,
125 // then stop processing.
126 if (!ParenCount && StackOp == IC_LPAREN)
127 break;
128
129 if (StackOp == IC_RPAREN) {
130 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000131 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000132 } else if (StackOp == IC_LPAREN) {
133 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000134 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000135 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000136 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000137 PostfixStack.push_back(std::make_pair(StackOp, 0));
138 }
139 }
140 // Push the new operator.
141 InfixOperatorStack.push_back(Op);
142 }
143 int64_t execute() {
144 // Push any remaining operators onto the postfix stack.
145 while (!InfixOperatorStack.empty()) {
146 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
147 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
148 PostfixStack.push_back(std::make_pair(StackOp, 0));
149 }
150
151 if (PostfixStack.empty())
152 return 0;
153
154 SmallVector<ICToken, 16> OperandStack;
155 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
156 ICToken Op = PostfixStack[i];
157 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
158 OperandStack.push_back(Op);
159 } else {
160 assert (OperandStack.size() > 1 && "Too few operands.");
161 int64_t Val;
162 ICToken Op2 = OperandStack.pop_back_val();
163 ICToken Op1 = OperandStack.pop_back_val();
164 switch (Op.first) {
165 default:
166 report_fatal_error("Unexpected operator!");
167 break;
168 case IC_PLUS:
169 Val = Op1.second + Op2.second;
170 OperandStack.push_back(std::make_pair(IC_IMM, Val));
171 break;
172 case IC_MINUS:
173 Val = Op1.second - Op2.second;
174 OperandStack.push_back(std::make_pair(IC_IMM, Val));
175 break;
176 case IC_MULTIPLY:
177 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
178 "Multiply operation with an immediate and a register!");
179 Val = Op1.second * Op2.second;
180 OperandStack.push_back(std::make_pair(IC_IMM, Val));
181 break;
182 case IC_DIVIDE:
183 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
184 "Divide operation with an immediate and a register!");
185 assert (Op2.second != 0 && "Division by zero!");
186 Val = Op1.second / Op2.second;
187 OperandStack.push_back(std::make_pair(IC_IMM, Val));
188 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000189 case IC_OR:
190 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
191 "Or operation with an immediate and a register!");
192 Val = Op1.second | Op2.second;
193 OperandStack.push_back(std::make_pair(IC_IMM, Val));
194 break;
195 case IC_AND:
196 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
197 "And operation with an immediate and a register!");
198 Val = Op1.second & Op2.second;
199 OperandStack.push_back(std::make_pair(IC_IMM, Val));
200 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000201 }
202 }
203 }
204 assert (OperandStack.size() == 1 && "Expected a single result.");
205 return OperandStack.pop_back_val().second;
206 }
207 };
208
209 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000210 IES_OR,
211 IES_AND,
Chad Rosier5362af92013-04-16 18:15:40 +0000212 IES_PLUS,
213 IES_MINUS,
214 IES_MULTIPLY,
215 IES_DIVIDE,
216 IES_LBRAC,
217 IES_RBRAC,
218 IES_LPAREN,
219 IES_RPAREN,
220 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000221 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000222 IES_IDENTIFIER,
223 IES_ERROR
224 };
225
226 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000227 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000228 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000229 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000230 const MCExpr *Sym;
231 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000232 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000233 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000234 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000235 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000236 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000237 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
238 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000239 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000240
241 unsigned getBaseReg() { return BaseReg; }
242 unsigned getIndexReg() { return IndexReg; }
243 unsigned getScale() { return Scale; }
244 const MCExpr *getSym() { return Sym; }
245 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000246 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000247 bool isValidEndState() {
248 return State == IES_RBRAC || State == IES_INTEGER;
249 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000250 bool getStopOnLBrac() { return StopOnLBrac; }
251 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000252 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000253
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000254 InlineAsmIdentifierInfo &getIdentifierInfo() {
255 return Info;
256 }
257
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000258 void onOr() {
259 IntelExprState CurrState = State;
260 switch (State) {
261 default:
262 State = IES_ERROR;
263 break;
264 case IES_INTEGER:
265 case IES_RPAREN:
266 case IES_REGISTER:
267 State = IES_OR;
268 IC.pushOperator(IC_OR);
269 break;
270 }
271 PrevState = CurrState;
272 }
273 void onAnd() {
274 IntelExprState CurrState = State;
275 switch (State) {
276 default:
277 State = IES_ERROR;
278 break;
279 case IES_INTEGER:
280 case IES_RPAREN:
281 case IES_REGISTER:
282 State = IES_AND;
283 IC.pushOperator(IC_AND);
284 break;
285 }
286 PrevState = CurrState;
287 }
Chad Rosier5362af92013-04-16 18:15:40 +0000288 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000289 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000290 switch (State) {
291 default:
292 State = IES_ERROR;
293 break;
294 case IES_INTEGER:
295 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000296 case IES_REGISTER:
297 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000298 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000299 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
300 // If we already have a BaseReg, then assume this is the IndexReg with
301 // a scale of 1.
302 if (!BaseReg) {
303 BaseReg = TmpReg;
304 } else {
305 assert (!IndexReg && "BaseReg/IndexReg already set!");
306 IndexReg = TmpReg;
307 Scale = 1;
308 }
309 }
Chad Rosier5362af92013-04-16 18:15:40 +0000310 break;
311 }
Chad Rosier31246272013-04-17 21:01:45 +0000312 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000313 }
314 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000315 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000316 switch (State) {
317 default:
318 State = IES_ERROR;
319 break;
320 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000321 case IES_MULTIPLY:
322 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000323 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000324 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000325 case IES_LBRAC:
326 case IES_RBRAC:
327 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000328 case IES_REGISTER:
329 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000330 // Only push the minus operator if it is not a unary operator.
331 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
332 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
333 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
334 IC.pushOperator(IC_MINUS);
335 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
336 // If we already have a BaseReg, then assume this is the IndexReg with
337 // a scale of 1.
338 if (!BaseReg) {
339 BaseReg = TmpReg;
340 } else {
341 assert (!IndexReg && "BaseReg/IndexReg already set!");
342 IndexReg = TmpReg;
343 Scale = 1;
344 }
Chad Rosier5362af92013-04-16 18:15:40 +0000345 }
Chad Rosier5362af92013-04-16 18:15:40 +0000346 break;
347 }
Chad Rosier31246272013-04-17 21:01:45 +0000348 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000349 }
350 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000351 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000352 switch (State) {
353 default:
354 State = IES_ERROR;
355 break;
356 case IES_PLUS:
357 case IES_LPAREN:
358 State = IES_REGISTER;
359 TmpReg = Reg;
360 IC.pushOperand(IC_REGISTER);
361 break;
Chad Rosier31246272013-04-17 21:01:45 +0000362 case IES_MULTIPLY:
363 // Index Register - Scale * Register
364 if (PrevState == IES_INTEGER) {
365 assert (!IndexReg && "IndexReg already set!");
366 State = IES_REGISTER;
367 IndexReg = Reg;
368 // Get the scale and replace the 'Scale * Register' with '0'.
369 Scale = IC.popOperand();
370 IC.pushOperand(IC_IMM);
371 IC.popOperator();
372 } else {
373 State = IES_ERROR;
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 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000379 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
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_PLUS:
386 case IES_MINUS:
387 State = IES_INTEGER;
388 Sym = SymRef;
389 SymName = SymRefName;
390 IC.pushOperand(IC_IMM);
391 break;
392 }
393 }
394 void onInteger(int64_t TmpInt) {
Chad Rosier31246272013-04-17 21:01:45 +0000395 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000396 switch (State) {
397 default:
398 State = IES_ERROR;
399 break;
400 case IES_PLUS:
401 case IES_MINUS:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000402 case IES_OR:
403 case IES_AND:
Chad Rosier5362af92013-04-16 18:15:40 +0000404 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000405 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000406 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000407 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000408 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
409 // Index Register - Register * Scale
410 assert (!IndexReg && "IndexReg already set!");
411 IndexReg = TmpReg;
412 Scale = TmpInt;
413 // Get the scale and replace the 'Register * Scale' with '0'.
414 IC.popOperator();
415 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000416 PrevState == IES_OR || PrevState == IES_AND ||
Chad Rosier31246272013-04-17 21:01:45 +0000417 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
418 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
419 CurrState == IES_MINUS) {
420 // Unary minus. No need to pop the minus operand because it was never
421 // pushed.
422 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
423 } else {
424 IC.pushOperand(IC_IMM, TmpInt);
425 }
Chad Rosier5362af92013-04-16 18:15:40 +0000426 break;
427 }
Chad Rosier31246272013-04-17 21:01:45 +0000428 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000429 }
430 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000431 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000432 switch (State) {
433 default:
434 State = IES_ERROR;
435 break;
436 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000437 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000438 case IES_RPAREN:
439 State = IES_MULTIPLY;
440 IC.pushOperator(IC_MULTIPLY);
441 break;
442 }
443 }
444 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000445 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000446 switch (State) {
447 default:
448 State = IES_ERROR;
449 break;
450 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000451 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000452 State = IES_DIVIDE;
453 IC.pushOperator(IC_DIVIDE);
454 break;
455 }
456 }
457 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000458 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000459 switch (State) {
460 default:
461 State = IES_ERROR;
462 break;
463 case IES_RBRAC:
464 State = IES_PLUS;
465 IC.pushOperator(IC_PLUS);
466 break;
467 }
468 }
469 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000470 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000471 switch (State) {
472 default:
473 State = IES_ERROR;
474 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000475 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000476 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000477 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000478 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000479 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
480 // If we already have a BaseReg, then assume this is the IndexReg with
481 // a scale of 1.
482 if (!BaseReg) {
483 BaseReg = TmpReg;
484 } else {
485 assert (!IndexReg && "BaseReg/IndexReg already set!");
486 IndexReg = TmpReg;
487 Scale = 1;
488 }
Chad Rosier5362af92013-04-16 18:15:40 +0000489 }
490 break;
491 }
Chad Rosier31246272013-04-17 21:01:45 +0000492 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000493 }
494 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000495 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000496 switch (State) {
497 default:
498 State = IES_ERROR;
499 break;
500 case IES_PLUS:
501 case IES_MINUS:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000502 case IES_OR:
503 case IES_AND:
Chad Rosier5362af92013-04-16 18:15:40 +0000504 case IES_MULTIPLY:
505 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000506 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000507 // FIXME: We don't handle this type of unary minus, yet.
508 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000509 PrevState == IES_OR || PrevState == IES_AND ||
Chad Rosierdb003992013-04-18 16:28:19 +0000510 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
511 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
512 CurrState == IES_MINUS) {
513 State = IES_ERROR;
514 break;
515 }
Chad Rosier5362af92013-04-16 18:15:40 +0000516 State = IES_LPAREN;
517 IC.pushOperator(IC_LPAREN);
518 break;
519 }
Chad Rosier31246272013-04-17 21:01:45 +0000520 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000521 }
522 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000523 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000524 switch (State) {
525 default:
526 State = IES_ERROR;
527 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000528 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000529 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000530 case IES_RPAREN:
531 State = IES_RPAREN;
532 IC.pushOperator(IC_RPAREN);
533 break;
534 }
535 }
536 };
537
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000538 MCAsmParser &getParser() const { return Parser; }
539
540 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
541
Chris Lattnera3a06812011-10-16 04:47:35 +0000542 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000543 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000544 bool MatchingInlineAsm = false) {
545 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000546 return Parser.Error(L, Msg, Ranges);
547 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000548
Devang Patel41b9dde2012-01-17 18:00:18 +0000549 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
550 Error(Loc, Msg);
551 return 0;
552 }
553
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000554 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000555 X86Operand *ParseATTOperand();
556 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000557 X86Operand *ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000558 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000559 X86Operand *ParseIntelOperator(unsigned OpKind);
David Majnemeraa34d792013-08-27 21:56:17 +0000560 X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
561 X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
562 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000563 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000564 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000565 int64_t ImmDisp, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000566 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
567 InlineAsmIdentifierInfo &Info,
568 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000569
Chris Lattnerb9270732010-04-17 18:56:34 +0000570 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000571
Chad Rosier175d0ae2013-04-12 18:21:18 +0000572 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
573 unsigned BaseReg, unsigned IndexReg,
574 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000575 unsigned Size, StringRef Identifier,
576 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000577
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000578 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000579 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000580
Devang Patelde47cce2012-01-18 22:42:29 +0000581 bool processInstruction(MCInst &Inst,
582 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
583
Chad Rosier49963552012-10-13 00:26:04 +0000584 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000585 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000586 MCStreamer &Out, unsigned &ErrorInfo,
587 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000588
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000589 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000590 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000591 bool isSrcOp(X86Operand &Op);
592
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000593 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
594 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000595 bool isDstOp(X86Operand &Op);
596
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000597 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000598 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000599 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000600 }
Craig Topper3c80d622014-01-06 04:55:54 +0000601 bool is32BitMode() const {
602 // FIXME: Can tablegen auto-generate this?
603 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
604 }
605 bool is16BitMode() const {
606 // FIXME: Can tablegen auto-generate this?
607 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
608 }
609 void SwitchMode(uint64_t mode) {
610 uint64_t oldMode = STI.getFeatureBits() &
611 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
612 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000613 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000614 assert(mode == (STI.getFeatureBits() &
615 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000616 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000617
Chad Rosierc2f055d2013-04-18 16:13:18 +0000618 bool isParsingIntelSyntax() {
619 return getParser().getAssemblerDialect();
620 }
621
Daniel Dunbareefe8612010-07-19 05:44:09 +0000622 /// @name Auto-generated Matcher Functions
623 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000624
Chris Lattner3e4582a2010-09-06 19:11:01 +0000625#define GET_ASSEMBLER_HEADER
626#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000627
Daniel Dunbar00331992009-07-29 00:02:19 +0000628 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000629
630public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000631 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
632 const MCInstrInfo &MII)
633 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000634
Daniel Dunbareefe8612010-07-19 05:44:09 +0000635 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000636 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000637 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000638 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000639
Chad Rosierf0e87202012-10-25 20:41:34 +0000640 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
641 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000642 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000643
644 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000645};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000646} // end anonymous namespace
647
Sean Callanan86c11812010-01-23 00:40:33 +0000648/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000649/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000650
Chris Lattner60db0a62010-02-09 00:34:28 +0000651static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000652
653/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000654
Craig Topper6bf3ed42012-07-18 04:59:16 +0000655static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000656 return (( Value <= 0x000000000000007FULL)||
657 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
658 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
659}
660
661static bool isImmSExti32i8Value(uint64_t Value) {
662 return (( Value <= 0x000000000000007FULL)||
663 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
664 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
665}
666
667static bool isImmZExtu32u8Value(uint64_t Value) {
668 return (Value <= 0x00000000000000FFULL);
669}
670
671static bool isImmSExti64i8Value(uint64_t Value) {
672 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000673 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000674}
675
676static bool isImmSExti64i32Value(uint64_t Value) {
677 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000678 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000679}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000680namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000681
682/// X86Operand - Instances of this class represent a parsed X86 machine
683/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000684struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000685 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000686 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000687 Register,
688 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000689 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000690 } Kind;
691
Chris Lattner0c2538f2010-01-15 18:51:29 +0000692 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000693 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000694 StringRef SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000695 void *OpDecl;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000696 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000697
Eric Christopher8996c5d2013-03-15 00:42:55 +0000698 struct TokOp {
699 const char *Data;
700 unsigned Length;
701 };
702
703 struct RegOp {
704 unsigned RegNo;
705 };
706
707 struct ImmOp {
708 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000709 };
710
711 struct MemOp {
712 unsigned SegReg;
713 const MCExpr *Disp;
714 unsigned BaseReg;
715 unsigned IndexReg;
716 unsigned Scale;
717 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000718 };
719
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000720 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000721 struct TokOp Tok;
722 struct RegOp Reg;
723 struct ImmOp Imm;
724 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000725 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000726
Chris Lattner015cfb12010-01-15 19:33:43 +0000727 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000728 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000729
Chad Rosiere81309b2013-04-09 17:53:49 +0000730 StringRef getSymName() { return SymName; }
Chad Rosier732b8372013-04-22 22:04:25 +0000731 void *getOpDecl() { return OpDecl; }
Chad Rosiere81309b2013-04-09 17:53:49 +0000732
Chris Lattner86e61532010-01-15 19:06:59 +0000733 /// getStartLoc - Get the location of the first token of this operand.
734 SMLoc getStartLoc() const { return StartLoc; }
735 /// getEndLoc - Get the location of the last token of this operand.
736 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000737 /// getLocRange - Get the range between the first and last token of this
738 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000739 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000740 /// getOffsetOfLoc - Get the location of the offset operator.
741 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000742
Jim Grosbach602aa902011-07-13 15:34:57 +0000743 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000744
Daniel Dunbare10787e2009-08-07 08:26:05 +0000745 StringRef getToken() const {
746 assert(Kind == Token && "Invalid access!");
747 return StringRef(Tok.Data, Tok.Length);
748 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000749 void setTokenValue(StringRef Value) {
750 assert(Kind == Token && "Invalid access!");
751 Tok.Data = Value.data();
752 Tok.Length = Value.size();
753 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000754
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000755 unsigned getReg() const {
756 assert(Kind == Register && "Invalid access!");
757 return Reg.RegNo;
758 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000759
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000760 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000761 assert(Kind == Immediate && "Invalid access!");
762 return Imm.Val;
763 }
764
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000765 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000766 assert(Kind == Memory && "Invalid access!");
767 return Mem.Disp;
768 }
769 unsigned getMemSegReg() const {
770 assert(Kind == Memory && "Invalid access!");
771 return Mem.SegReg;
772 }
773 unsigned getMemBaseReg() const {
774 assert(Kind == Memory && "Invalid access!");
775 return Mem.BaseReg;
776 }
777 unsigned getMemIndexReg() const {
778 assert(Kind == Memory && "Invalid access!");
779 return Mem.IndexReg;
780 }
781 unsigned getMemScale() const {
782 assert(Kind == Memory && "Invalid access!");
783 return Mem.Scale;
784 }
785
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000786 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000787
788 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000789
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000790 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000791 if (!isImm())
792 return false;
793
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000794 // If this isn't a constant expr, just assume it fits and let relaxation
795 // handle it.
796 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
797 if (!CE)
798 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000799
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000800 // Otherwise, check the value is in a range that makes sense for this
801 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000802 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000803 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000804 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000805 if (!isImm())
806 return false;
807
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000808 // If this isn't a constant expr, just assume it fits and let relaxation
809 // handle it.
810 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
811 if (!CE)
812 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000813
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000814 // Otherwise, check the value is in a range that makes sense for this
815 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000816 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000817 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000818 bool isImmZExtu32u8() const {
819 if (!isImm())
820 return false;
821
822 // If this isn't a constant expr, just assume it fits and let relaxation
823 // handle it.
824 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
825 if (!CE)
826 return true;
827
828 // Otherwise, check the value is in a range that makes sense for this
829 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000830 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000831 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000832 bool isImmSExti64i8() const {
833 if (!isImm())
834 return false;
835
836 // If this isn't a constant expr, just assume it fits and let relaxation
837 // handle it.
838 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
839 if (!CE)
840 return true;
841
842 // Otherwise, check the value is in a range that makes sense for this
843 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000844 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000845 }
846 bool isImmSExti64i32() const {
847 if (!isImm())
848 return false;
849
850 // If this isn't a constant expr, just assume it fits and let relaxation
851 // handle it.
852 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
853 if (!CE)
854 return true;
855
856 // Otherwise, check the value is in a range that makes sense for this
857 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000858 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000859 }
860
Chad Rosier5bca3f92012-10-22 19:50:35 +0000861 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000862 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000863 }
864
Chad Rosiera4bc9432013-01-10 22:10:27 +0000865 bool needAddressOf() const {
866 return AddressOf;
867 }
868
Daniel Dunbare10787e2009-08-07 08:26:05 +0000869 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000870 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000871 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000872 }
Chad Rosier51afe632012-06-27 22:34:28 +0000873 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000874 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000875 }
Chad Rosier51afe632012-06-27 22:34:28 +0000876 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000877 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000878 }
Chad Rosier51afe632012-06-27 22:34:28 +0000879 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000880 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000881 }
Chad Rosier51afe632012-06-27 22:34:28 +0000882 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000883 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000884 }
Chad Rosier51afe632012-06-27 22:34:28 +0000885 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000886 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000887 }
Chad Rosier51afe632012-06-27 22:34:28 +0000888 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000889 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000890 }
Craig Topper8c26c422013-08-25 23:18:05 +0000891 bool isMem512() const {
892 return Kind == Memory && (!Mem.Size || Mem.Size == 512);
893 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000894
Craig Topper01deb5f2012-07-18 04:11:12 +0000895 bool isMemVX32() const {
896 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
897 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
898 }
899 bool isMemVY32() const {
900 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
901 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
902 }
903 bool isMemVX64() const {
904 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
905 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
906 }
907 bool isMemVY64() const {
908 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
909 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
910 }
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000911 bool isMemVZ32() const {
912 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
913 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
914 }
915 bool isMemVZ64() const {
916 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
917 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
918 }
919
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000920 bool isAbsMem() const {
921 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000922 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000923 }
924
Craig Topper18854172013-08-25 22:23:38 +0000925 bool isMemOffs8() const {
926 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
927 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 8);
928 }
929 bool isMemOffs16() const {
930 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
931 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 16);
932 }
933 bool isMemOffs32() const {
934 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
935 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 32);
936 }
937 bool isMemOffs64() const {
938 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
939 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 64);
940 }
941
Daniel Dunbare10787e2009-08-07 08:26:05 +0000942 bool isReg() const { return Kind == Register; }
943
Craig Toppera422b092013-10-14 04:55:01 +0000944 bool isGR32orGR64() const {
945 return Kind == Register &&
946 (X86MCRegisterClasses[X86::GR32RegClassID].contains(getReg()) ||
947 X86MCRegisterClasses[X86::GR64RegClassID].contains(getReg()));
948 }
949
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000950 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
951 // Add as immediates when possible.
952 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
953 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
954 else
955 Inst.addOperand(MCOperand::CreateExpr(Expr));
956 }
957
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000958 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000959 assert(N == 1 && "Invalid number of operands!");
960 Inst.addOperand(MCOperand::CreateReg(getReg()));
961 }
962
Craig Toppera422b092013-10-14 04:55:01 +0000963 static unsigned getGR32FromGR64(unsigned RegNo) {
964 switch (RegNo) {
965 default: llvm_unreachable("Unexpected register");
966 case X86::RAX: return X86::EAX;
967 case X86::RCX: return X86::ECX;
968 case X86::RDX: return X86::EDX;
969 case X86::RBX: return X86::EBX;
970 case X86::RBP: return X86::EBP;
971 case X86::RSP: return X86::ESP;
972 case X86::RSI: return X86::ESI;
973 case X86::RDI: return X86::EDI;
974 case X86::R8: return X86::R8D;
975 case X86::R9: return X86::R9D;
976 case X86::R10: return X86::R10D;
977 case X86::R11: return X86::R11D;
978 case X86::R12: return X86::R12D;
979 case X86::R13: return X86::R13D;
980 case X86::R14: return X86::R14D;
981 case X86::R15: return X86::R15D;
982 case X86::RIP: return X86::EIP;
983 }
984 }
985
986 void addGR32orGR64Operands(MCInst &Inst, unsigned N) const {
987 assert(N == 1 && "Invalid number of operands!");
988 unsigned RegNo = getReg();
989 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo))
990 RegNo = getGR32FromGR64(RegNo);
991 Inst.addOperand(MCOperand::CreateReg(RegNo));
992 }
993
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000994 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000995 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000996 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000997 }
998
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000999 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +00001000 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +00001001 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
1002 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
1003 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +00001004 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +00001005 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
1006 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001007
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001008 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
1009 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001010 // Add as immediates when possible.
1011 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
1012 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1013 else
1014 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001015 }
1016
Craig Topper18854172013-08-25 22:23:38 +00001017 void addMemOffsOperands(MCInst &Inst, unsigned N) const {
1018 assert((N == 1) && "Invalid number of operands!");
1019 // Add as immediates when possible.
1020 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
1021 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1022 else
1023 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
1024 }
1025
Chris Lattner528d00b2010-01-15 19:28:38 +00001026 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001027 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +00001028 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001029 Res->Tok.Data = Str.data();
1030 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +00001031 return Res;
1032 }
1033
Chad Rosier91c82662012-10-24 17:22:29 +00001034 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +00001035 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +00001036 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +00001037 StringRef SymName = StringRef(),
1038 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +00001039 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001040 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001041 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +00001042 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +00001043 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +00001044 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001045 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001046 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001047
Chad Rosierf3c04f62013-03-19 21:58:18 +00001048 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +00001049 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001050 Res->Imm.Val = Val;
1051 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001052 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001053
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001054 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +00001055 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +00001056 unsigned Size = 0, StringRef SymName = StringRef(),
1057 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001058 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
1059 Res->Mem.SegReg = 0;
1060 Res->Mem.Disp = Disp;
1061 Res->Mem.BaseReg = 0;
1062 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +00001063 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +00001064 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001065 Res->SymName = SymName;
1066 Res->OpDecl = OpDecl;
1067 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001068 return Res;
1069 }
1070
1071 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001072 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
1073 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +00001074 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +00001075 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +00001076 StringRef SymName = StringRef(),
1077 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001078 // We should never just have a displacement, that should be parsed as an
1079 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001080 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
1081
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001082 // The scale should always be one of {1,2,4,8}.
1083 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001084 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +00001085 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001086 Res->Mem.SegReg = SegReg;
1087 Res->Mem.Disp = Disp;
1088 Res->Mem.BaseReg = BaseReg;
1089 Res->Mem.IndexReg = IndexReg;
1090 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +00001091 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001092 Res->SymName = SymName;
1093 Res->OpDecl = OpDecl;
1094 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001095 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001096 }
1097};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00001098
Chris Lattner4eb9df02009-07-29 06:33:53 +00001099} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +00001100
Devang Patel4a6e7782012-01-12 18:03:40 +00001101bool X86AsmParser::isSrcOp(X86Operand &Op) {
Craig Topper3c80d622014-01-06 04:55:54 +00001102 unsigned basereg =
1103 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001104
1105 return (Op.isMem() &&
1106 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
1107 isa<MCConstantExpr>(Op.Mem.Disp) &&
1108 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1109 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
1110}
1111
Devang Patel4a6e7782012-01-12 18:03:40 +00001112bool X86AsmParser::isDstOp(X86Operand &Op) {
Craig Topper3c80d622014-01-06 04:55:54 +00001113 unsigned basereg =
1114 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001115
Chad Rosier51afe632012-06-27 22:34:28 +00001116 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +00001117 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001118 isa<MCConstantExpr>(Op.Mem.Disp) &&
1119 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1120 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
1121}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001122
Devang Patel4a6e7782012-01-12 18:03:40 +00001123bool X86AsmParser::ParseRegister(unsigned &RegNo,
1124 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001125 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001126 const AsmToken &PercentTok = Parser.getTok();
1127 StartLoc = PercentTok.getLoc();
1128
1129 // If we encounter a %, ignore it. This code handles registers with and
1130 // without the prefix, unprefixed registers can occur in cfi directives.
1131 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001132 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001133
Sean Callanan936b0d32010-01-19 21:44:56 +00001134 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001135 EndLoc = Tok.getEndLoc();
1136
Devang Patelce6a2ca2012-01-20 22:32:05 +00001137 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001138 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001139 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001140 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001141 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001142
Kevin Enderby7d912182009-09-03 17:15:07 +00001143 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001144
Chris Lattner1261b812010-09-22 04:11:10 +00001145 // If the match failed, try the register name as lowercase.
1146 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001147 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001148
Evan Chengeda1d4f2011-07-27 23:22:03 +00001149 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +00001150 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +00001151 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1152 // checked.
1153 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1154 // REX prefix.
1155 if (RegNo == X86::RIZ ||
1156 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1157 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1158 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001159 return Error(StartLoc, "register %"
1160 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001161 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001162 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001163
Chris Lattner1261b812010-09-22 04:11:10 +00001164 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1165 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001166 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001167 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001168
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001169 // Check to see if we have '(4)' after %st.
1170 if (getLexer().isNot(AsmToken::LParen))
1171 return false;
1172 // Lex the paren.
1173 getParser().Lex();
1174
1175 const AsmToken &IntTok = Parser.getTok();
1176 if (IntTok.isNot(AsmToken::Integer))
1177 return Error(IntTok.getLoc(), "expected stack index");
1178 switch (IntTok.getIntVal()) {
1179 case 0: RegNo = X86::ST0; break;
1180 case 1: RegNo = X86::ST1; break;
1181 case 2: RegNo = X86::ST2; break;
1182 case 3: RegNo = X86::ST3; break;
1183 case 4: RegNo = X86::ST4; break;
1184 case 5: RegNo = X86::ST5; break;
1185 case 6: RegNo = X86::ST6; break;
1186 case 7: RegNo = X86::ST7; break;
1187 default: return Error(IntTok.getLoc(), "invalid stack index");
1188 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001189
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001190 if (getParser().Lex().isNot(AsmToken::RParen))
1191 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001192
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001193 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001194 Parser.Lex(); // Eat ')'
1195 return false;
1196 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001197
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001198 EndLoc = Parser.getTok().getEndLoc();
1199
Chris Lattner80486622010-06-24 07:29:18 +00001200 // If this is "db[0-7]", match it as an alias
1201 // for dr[0-7].
1202 if (RegNo == 0 && Tok.getString().size() == 3 &&
1203 Tok.getString().startswith("db")) {
1204 switch (Tok.getString()[2]) {
1205 case '0': RegNo = X86::DR0; break;
1206 case '1': RegNo = X86::DR1; break;
1207 case '2': RegNo = X86::DR2; break;
1208 case '3': RegNo = X86::DR3; break;
1209 case '4': RegNo = X86::DR4; break;
1210 case '5': RegNo = X86::DR5; break;
1211 case '6': RegNo = X86::DR6; break;
1212 case '7': RegNo = X86::DR7; break;
1213 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001214
Chris Lattner80486622010-06-24 07:29:18 +00001215 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001216 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001217 Parser.Lex(); // Eat it.
1218 return false;
1219 }
1220 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001221
Devang Patelce6a2ca2012-01-20 22:32:05 +00001222 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001223 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001224 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001225 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001226 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001227
Sean Callanana83fd7d2010-01-19 20:27:46 +00001228 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001229 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001230}
1231
Devang Patel4a6e7782012-01-12 18:03:40 +00001232X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001233 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001234 return ParseIntelOperand();
1235 return ParseATTOperand();
1236}
1237
Devang Patel41b9dde2012-01-17 18:00:18 +00001238/// getIntelMemOperandSize - Return intel memory operand size.
1239static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001240 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001241 .Cases("BYTE", "byte", 8)
1242 .Cases("WORD", "word", 16)
1243 .Cases("DWORD", "dword", 32)
1244 .Cases("QWORD", "qword", 64)
1245 .Cases("XWORD", "xword", 80)
1246 .Cases("XMMWORD", "xmmword", 128)
1247 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001248 .Default(0);
1249 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001250}
1251
Chad Rosier175d0ae2013-04-12 18:21:18 +00001252X86Operand *
1253X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1254 unsigned BaseReg, unsigned IndexReg,
1255 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001256 unsigned Size, StringRef Identifier,
1257 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001258 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001259 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1260 // reference. We need an 'r' constraint here, so we need to create register
1261 // operand to ensure proper matching. Just pick a GPR based on the size of
1262 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001263 if (!Info.IsVarDecl) {
Craig Topper3c80d622014-01-06 04:55:54 +00001264 unsigned RegNo =
1265 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001266 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001267 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001268 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001269 if (!Size) {
1270 Size = Info.Type * 8; // Size is in terms of bits in this context.
1271 if (Size)
1272 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1273 /*Len=*/0, Size));
1274 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001275 }
1276
Chad Rosier7ca135b2013-03-19 21:11:56 +00001277 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001278 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001279 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001280 BaseReg = BaseReg ? BaseReg : 1;
1281 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001282 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001283}
1284
Chad Rosierd383db52013-04-12 20:20:54 +00001285static void
1286RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1287 StringRef SymName, int64_t ImmDisp,
1288 int64_t FinalImmDisp, SMLoc &BracLoc,
1289 SMLoc &StartInBrac, SMLoc &End) {
1290 // Remove the '[' and ']' from the IR string.
1291 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1292 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1293
1294 // If ImmDisp is non-zero, then we parsed a displacement before the
1295 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1296 // If ImmDisp doesn't match the displacement computed by the state machine
1297 // then we have an additional displacement in the bracketed expression.
1298 if (ImmDisp != FinalImmDisp) {
1299 if (ImmDisp) {
1300 // We have an immediate displacement before the bracketed expression.
1301 // Adjust this to match the final immediate displacement.
1302 bool Found = false;
1303 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1304 E = AsmRewrites->end(); I != E; ++I) {
1305 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1306 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001307 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1308 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001309 (*I).Kind = AOK_Imm;
1310 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1311 (*I).Val = FinalImmDisp;
1312 Found = true;
1313 break;
1314 }
1315 }
1316 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001317 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001318 } else {
1319 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001320 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001321 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001322 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001323 }
1324 }
1325 // Remove all the ImmPrefix rewrites within the brackets.
1326 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1327 E = AsmRewrites->end(); I != E; ++I) {
1328 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1329 continue;
1330 if ((*I).Kind == AOK_ImmPrefix)
1331 (*I).Kind = AOK_Delete;
1332 }
1333 const char *SymLocPtr = SymName.data();
1334 // Skip everything before the symbol.
1335 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1336 assert(Len > 0 && "Expected a non-negative length.");
1337 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1338 }
1339 // Skip everything after the symbol.
1340 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1341 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1342 assert(Len > 0 && "Expected a non-negative length.");
1343 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1344 }
1345}
1346
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001347bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001348 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001349
Chad Rosier5c118fd2013-01-14 22:31:35 +00001350 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001351 while (!Done) {
1352 bool UpdateLocLex = true;
1353
1354 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1355 // identifier. Don't try an parse it as a register.
1356 if (Tok.getString().startswith("."))
1357 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001358
1359 // If we're parsing an immediate expression, we don't expect a '['.
1360 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1361 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001362
1363 switch (getLexer().getKind()) {
1364 default: {
1365 if (SM.isValidEndState()) {
1366 Done = true;
1367 break;
1368 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001369 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001370 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001371 case AsmToken::EndOfStatement: {
1372 Done = true;
1373 break;
1374 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001375 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001376 // This could be a register or a symbolic displacement.
1377 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001378 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001379 SMLoc IdentLoc = Tok.getLoc();
1380 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001381 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001382 SM.onRegister(TmpReg);
1383 UpdateLocLex = false;
1384 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001385 } else {
1386 if (!isParsingInlineAsm()) {
1387 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001388 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001389 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001390 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001391 if (ParseIntelIdentifier(Val, Identifier, Info,
1392 /*Unevaluated=*/false, End))
1393 return true;
Chad Rosier95ce8892013-04-19 18:39:50 +00001394 }
1395 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001396 UpdateLocLex = false;
1397 break;
1398 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001399 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001400 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001401 case AsmToken::Integer: {
Chad Rosierbfb70992013-04-17 00:11:46 +00001402 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001403 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1404 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001405 // Look for 'b' or 'f' following an Integer as a directional label
1406 SMLoc Loc = getTok().getLoc();
1407 int64_t IntVal = getTok().getIntVal();
1408 End = consumeToken();
1409 UpdateLocLex = false;
1410 if (getLexer().getKind() == AsmToken::Identifier) {
1411 StringRef IDVal = getTok().getString();
1412 if (IDVal == "f" || IDVal == "b") {
1413 MCSymbol *Sym =
1414 getContext().GetDirectionalLocalSymbol(IntVal,
1415 IDVal == "f" ? 1 : 0);
1416 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1417 const MCExpr *Val =
1418 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1419 if (IDVal == "b" && Sym->isUndefined())
1420 return Error(Loc, "invalid reference to undefined symbol");
1421 StringRef Identifier = Sym->getName();
1422 SM.onIdentifierExpr(Val, Identifier);
1423 End = consumeToken();
1424 } else {
1425 SM.onInteger(IntVal);
1426 }
1427 } else {
1428 SM.onInteger(IntVal);
1429 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001430 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001431 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001432 case AsmToken::Plus: SM.onPlus(); break;
1433 case AsmToken::Minus: SM.onMinus(); break;
1434 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001435 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001436 case AsmToken::Pipe: SM.onOr(); break;
1437 case AsmToken::Amp: SM.onAnd(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001438 case AsmToken::LBrac: SM.onLBrac(); break;
1439 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001440 case AsmToken::LParen: SM.onLParen(); break;
1441 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001442 }
Chad Rosier31246272013-04-17 21:01:45 +00001443 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001444 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001445
Alp Tokera5b88a52013-12-02 16:06:06 +00001446 if (!Done && UpdateLocLex)
1447 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001448 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001449 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001450}
1451
1452X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001453 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001454 unsigned Size) {
1455 const AsmToken &Tok = Parser.getTok();
1456 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1457 if (getLexer().isNot(AsmToken::LBrac))
1458 return ErrorOperand(BracLoc, "Expected '[' token!");
1459 Parser.Lex(); // Eat '['
1460
1461 SMLoc StartInBrac = Tok.getLoc();
1462 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1463 // may have already parsed an immediate displacement before the bracketed
1464 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001465 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001466 if (ParseIntelExpression(SM, End))
1467 return 0;
Devang Patel41b9dde2012-01-17 18:00:18 +00001468
Chad Rosier175d0ae2013-04-12 18:21:18 +00001469 const MCExpr *Disp;
1470 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001471 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001472 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001473 if (isParsingInlineAsm())
1474 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001475 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001476 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001477 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001478 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001479 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001480 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001481
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001482 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001483 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001484 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001485 if (ParseIntelDotOperator(Disp, NewDisp))
1486 return 0;
Chad Rosier911c1f32012-10-25 17:37:43 +00001487
Chad Rosier70f47592013-04-10 20:07:47 +00001488 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001489 Parser.Lex(); // Eat the field.
1490 Disp = NewDisp;
1491 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001492
Chad Rosier5c118fd2013-01-14 22:31:35 +00001493 int BaseReg = SM.getBaseReg();
1494 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001495 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001496 if (!isParsingInlineAsm()) {
1497 // handle [-42]
1498 if (!BaseReg && !IndexReg) {
1499 if (!SegReg)
1500 return X86Operand::CreateMem(Disp, Start, End, Size);
1501 else
1502 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1503 }
1504 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1505 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001506 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001507
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001508 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001509 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001510 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001511}
1512
Chad Rosier8a244662013-04-02 20:02:33 +00001513// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001514bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1515 StringRef &Identifier,
1516 InlineAsmIdentifierInfo &Info,
1517 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001518 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1519 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001520
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001521 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001522 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001523
Chad Rosier8a244662013-04-02 20:02:33 +00001524 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001525
1526 // Advance the token stream until the end of the current token is
1527 // after the end of what the frontend claimed.
1528 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1529 while (true) {
1530 End = Tok.getEndLoc();
1531 getLexer().Lex();
1532
1533 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1534 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001535 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001536
1537 // Create the symbol reference.
1538 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001539 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1540 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001541 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001542 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001543}
1544
David Majnemeraa34d792013-08-27 21:56:17 +00001545/// \brief Parse intel style segment override.
1546X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1547 SMLoc Start,
1548 unsigned Size) {
1549 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1550 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1551 if (Tok.isNot(AsmToken::Colon))
1552 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1553 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001554
David Majnemeraa34d792013-08-27 21:56:17 +00001555 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001556 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001557 ImmDisp = Tok.getIntVal();
1558 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1559
Chad Rosier1530ba52013-03-27 21:49:56 +00001560 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001561 InstInfo->AsmRewrites->push_back(
1562 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1563
1564 if (getLexer().isNot(AsmToken::LBrac)) {
1565 // An immediate following a 'segment register', 'colon' token sequence can
1566 // be followed by a bracketed expression. If it isn't we know we have our
1567 // final segment override.
1568 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1569 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1570 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1571 Size);
1572 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001573 }
1574
Chad Rosier91c82662012-10-24 17:22:29 +00001575 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001576 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001577
David Majnemeraa34d792013-08-27 21:56:17 +00001578 const MCExpr *Val;
1579 SMLoc End;
1580 if (!isParsingInlineAsm()) {
1581 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001582 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001583
1584 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001585 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001586
David Majnemeraa34d792013-08-27 21:56:17 +00001587 InlineAsmIdentifierInfo Info;
1588 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001589 if (ParseIntelIdentifier(Val, Identifier, Info,
1590 /*Unevaluated=*/false, End))
1591 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001592 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1593 /*Scale=*/1, Start, End, Size, Identifier, Info);
1594}
1595
1596/// ParseIntelMemOperand - Parse intel style memory operand.
1597X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1598 unsigned Size) {
1599 const AsmToken &Tok = Parser.getTok();
1600 SMLoc End;
1601
1602 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1603 if (getLexer().is(AsmToken::LBrac))
1604 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1605
Chad Rosier95ce8892013-04-19 18:39:50 +00001606 const MCExpr *Val;
1607 if (!isParsingInlineAsm()) {
1608 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001609 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001610
1611 return X86Operand::CreateMem(Val, Start, End, Size);
1612 }
1613
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001614 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001615 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001616 if (ParseIntelIdentifier(Val, Identifier, Info,
1617 /*Unevaluated=*/false, End))
1618 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001619 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001620 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001621}
1622
Chad Rosier5dcb4662012-10-24 22:21:50 +00001623/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001624bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001625 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001626 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001627 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001628
1629 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001630 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001631 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001632 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001633 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001634
1635 // Drop the '.'.
1636 StringRef DotDispStr = Tok.getString().drop_front(1);
1637
Chad Rosier5dcb4662012-10-24 22:21:50 +00001638 // .Imm gets lexed as a real.
1639 if (Tok.is(AsmToken::Real)) {
1640 APInt DotDisp;
1641 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001642 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001643 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001644 unsigned DotDisp;
1645 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1646 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001647 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001648 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001649 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001650 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001651 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001652
Chad Rosier240b7b92012-10-25 21:51:10 +00001653 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1654 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1655 unsigned Len = DotDispStr.size();
1656 unsigned Val = OrigDispVal + DotDispVal;
1657 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1658 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001659 }
1660
Chad Rosiercc541e82013-04-19 15:57:00 +00001661 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001662 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001663}
1664
Chad Rosier91c82662012-10-24 17:22:29 +00001665/// Parse the 'offset' operator. This operator is used to specify the
1666/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001667X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001668 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001669 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001670 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001671
Chad Rosier91c82662012-10-24 17:22:29 +00001672 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001673 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001674 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001675 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001676 if (ParseIntelIdentifier(Val, Identifier, Info,
1677 /*Unevaluated=*/false, End))
1678 return 0;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001679
Chad Rosiere2f03772012-10-26 16:09:20 +00001680 // Don't emit the offset operator.
1681 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1682
Chad Rosier91c82662012-10-24 17:22:29 +00001683 // The offset operator will have an 'r' constraint, thus we need to create
1684 // register operand to ensure proper matching. Just pick a GPR based on
1685 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001686 unsigned RegNo =
1687 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001688 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001689 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001690}
1691
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001692enum IntelOperatorKind {
1693 IOK_LENGTH,
1694 IOK_SIZE,
1695 IOK_TYPE
1696};
1697
1698/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1699/// returns the number of elements in an array. It returns the value 1 for
1700/// non-array variables. The SIZE operator returns the size of a C or C++
1701/// variable. A variable's size is the product of its LENGTH and TYPE. The
1702/// TYPE operator returns the size of a C or C++ type or variable. If the
1703/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001704X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001705 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001706 SMLoc TypeLoc = Tok.getLoc();
1707 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001708
Chad Rosier95ce8892013-04-19 18:39:50 +00001709 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001710 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001711 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001712 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001713 if (ParseIntelIdentifier(Val, Identifier, Info,
1714 /*Unevaluated=*/true, End))
1715 return 0;
1716
1717 if (!Info.OpDecl)
1718 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001719
Chad Rosierf6675c32013-04-22 17:01:46 +00001720 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001721 switch(OpKind) {
1722 default: llvm_unreachable("Unexpected operand kind!");
1723 case IOK_LENGTH: CVal = Info.Length; break;
1724 case IOK_SIZE: CVal = Info.Size; break;
1725 case IOK_TYPE: CVal = Info.Type; break;
1726 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001727
1728 // Rewrite the type operator and the C or C++ type or variable in terms of an
1729 // immediate. E.g. TYPE foo -> $$4
1730 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001731 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001732
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001733 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001734 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001735}
1736
Devang Patel41b9dde2012-01-17 18:00:18 +00001737X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001738 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001739 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001740
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001741 // Offset, length, type and size operators.
1742 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001743 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001744 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001745 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001746 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001747 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001748 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001749 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001750 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001751 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001752 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001753
David Majnemeraa34d792013-08-27 21:56:17 +00001754 unsigned Size = getIntelMemOperandSize(Tok.getString());
1755 if (Size) {
1756 Parser.Lex(); // Eat operand size (e.g., byte, word).
1757 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1758 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1759 Parser.Lex(); // Eat ptr.
1760 }
1761 Start = Tok.getLoc();
1762
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001763 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001764 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1765 getLexer().is(AsmToken::LParen)) {
1766 AsmToken StartTok = Tok;
1767 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1768 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001769 if (ParseIntelExpression(SM, End))
1770 return 0;
Chad Rosierbfb70992013-04-17 00:11:46 +00001771
1772 int64_t Imm = SM.getImm();
1773 if (isParsingInlineAsm()) {
1774 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1775 if (StartTok.getString().size() == Len)
1776 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001777 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001778 else
1779 // Otherwise, rewrite the complex expression as a single immediate.
1780 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001781 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001782
1783 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001784 // If a directional label (ie. 1f or 2b) was parsed above from
1785 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1786 // to the MCExpr with the directional local symbol and this is a
1787 // memory operand not an immediate operand.
1788 if (SM.getSym())
1789 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1790
Chad Rosierbfb70992013-04-17 00:11:46 +00001791 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1792 return X86Operand::CreateImm(ImmExpr, Start, End);
1793 }
1794
1795 // Only positive immediates are valid.
1796 if (Imm < 0)
1797 return ErrorOperand(Start, "expected a positive immediate displacement "
1798 "before bracketed expr.");
1799
1800 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001801 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001802 }
1803
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001804 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001805 unsigned RegNo = 0;
1806 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001807 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001808 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001809 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001810 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001811
David Majnemeraa34d792013-08-27 21:56:17 +00001812 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001813 }
1814
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001815 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001816 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001817}
1818
Devang Patel4a6e7782012-01-12 18:03:40 +00001819X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001820 switch (getLexer().getKind()) {
1821 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001822 // Parse a memory operand with no segment register.
1823 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001824 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001825 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001826 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001827 SMLoc Start, End;
1828 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001829 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001830 Error(Start, "%eiz and %riz can only be used as index registers",
1831 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001832 return 0;
1833 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001834
Chris Lattnerb9270732010-04-17 18:56:34 +00001835 // If this is a segment register followed by a ':', then this is the start
1836 // of a memory reference, otherwise this is a normal register reference.
1837 if (getLexer().isNot(AsmToken::Colon))
1838 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001839
Chris Lattnerb9270732010-04-17 18:56:34 +00001840 getParser().Lex(); // Eat the colon.
1841 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001842 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001843 case AsmToken::Dollar: {
1844 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001845 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001846 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001847 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001848 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001849 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001850 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001851 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001852 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001853}
1854
Chris Lattnerb9270732010-04-17 18:56:34 +00001855/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1856/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001857X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001858
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001859 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1860 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001861 // only way to do this without lookahead is to eat the '(' and see what is
1862 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001863 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001864 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001865 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001866 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001867
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001868 // After parsing the base expression we could either have a parenthesized
1869 // memory address or not. If not, return now. If so, eat the (.
1870 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001871 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001872 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001873 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001874 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001875 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001876
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001877 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001878 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001879 } else {
1880 // Okay, we have a '('. We don't know if this is an expression or not, but
1881 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001882 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001883 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001884
Kevin Enderby7d912182009-09-03 17:15:07 +00001885 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001886 // Nothing to do here, fall into the code below with the '(' part of the
1887 // memory operand consumed.
1888 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001889 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001890
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001891 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001892 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001893 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001894
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001895 // After parsing the base expression we could either have a parenthesized
1896 // memory address or not. If not, return now. If so, eat the (.
1897 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001898 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001899 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001900 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001901 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001902 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001903
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001904 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001905 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001906 }
1907 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001908
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001909 // If we reached here, then we just ate the ( of the memory operand. Process
1910 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001911 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001912 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001913
Chris Lattner0c2538f2010-01-15 18:51:29 +00001914 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001915 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001916 BaseLoc = Parser.getTok().getLoc();
Benjamin Kramer1930b002011-10-16 12:10:27 +00001917 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001918 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001919 Error(StartLoc, "eiz and riz can only be used as index registers",
1920 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001921 return 0;
1922 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001923 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001924
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001925 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001926 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001927 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001928
1929 // Following the comma we should have either an index register, or a scale
1930 // value. We don't support the later form, but we want to parse it
1931 // correctly.
1932 //
1933 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001934 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001935 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001936 SMLoc L;
1937 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001938
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001939 if (getLexer().isNot(AsmToken::RParen)) {
1940 // Parse the scale amount:
1941 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001942 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001943 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001944 "expected comma in scale expression");
1945 return 0;
1946 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001947 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001948
1949 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001950 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001951
1952 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001953 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001954 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001955 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001956 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001957
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001958 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001959 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1960 ScaleVal != 1) {
1961 Error(Loc, "scale factor in 16-bit address must be 1");
1962 return 0;
1963 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001964 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1965 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1966 return 0;
1967 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001968 Scale = (unsigned)ScaleVal;
1969 }
1970 }
1971 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001972 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001973 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001974 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001975
1976 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001977 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001978 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001979
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001980 if (Value != 1)
1981 Warning(Loc, "scale factor without index register is ignored");
1982 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001983 }
1984 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001985
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001986 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001987 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001988 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001989 return 0;
1990 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001991 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001992 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001993
David Woodhouse6dbda442014-01-08 12:58:28 +00001994 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1995 // and then only in non-64-bit modes. Except for DX, which is a special case
1996 // because an unofficial form of in/out instructions uses it.
1997 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1998 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1999 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2000 BaseReg != X86::DX) {
2001 Error(BaseLoc, "invalid 16-bit base register");
2002 return 0;
2003 }
2004 if (BaseReg == 0 &&
2005 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2006 Error(IndexLoc, "16-bit memory operand may not include only index register");
2007 return 0;
2008 }
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002009 // If we have both a base register and an index register make sure they are
2010 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00002011 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002012 if (BaseReg != 0 && IndexReg != 0) {
2013 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00002014 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
2015 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002016 IndexReg != X86::RIZ) {
David Woodhouse6dbda442014-01-08 12:58:28 +00002017 Error(BaseLoc, "base register is 64-bit, but index register is not");
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002018 return 0;
2019 }
2020 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00002021 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
2022 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002023 IndexReg != X86::EIZ){
David Woodhouse6dbda442014-01-08 12:58:28 +00002024 Error(BaseLoc, "base register is 32-bit, but index register is not");
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002025 return 0;
2026 }
David Woodhouse6dbda442014-01-08 12:58:28 +00002027 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
2028 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
2029 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
2030 Error(BaseLoc, "base register is 16-bit, but index register is not");
2031 return 0;
2032 }
2033 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
2034 IndexReg != X86::SI && IndexReg != X86::DI) ||
2035 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
2036 IndexReg != X86::BX && IndexReg != X86::BP)) {
2037 Error(BaseLoc, "invalid 16-bit base/index register combination");
2038 return 0;
2039 }
2040 }
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002041 }
2042
Chris Lattner015cfb12010-01-15 19:33:43 +00002043 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
2044 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002045}
2046
Devang Patel4a6e7782012-01-12 18:03:40 +00002047bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00002048ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002049 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00002050 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002051 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002052
Chris Lattner7e8a99b2010-11-28 20:23:50 +00002053 // FIXME: Hack to recognize setneb as setne.
2054 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
2055 PatchedName != "setb" && PatchedName != "setnb")
2056 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00002057
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002058 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
2059 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002060 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002061 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
2062 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00002063 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002064 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002065 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002066 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00002067 .Case("eq", 0x00)
2068 .Case("lt", 0x01)
2069 .Case("le", 0x02)
2070 .Case("unord", 0x03)
2071 .Case("neq", 0x04)
2072 .Case("nlt", 0x05)
2073 .Case("nle", 0x06)
2074 .Case("ord", 0x07)
2075 /* AVX only from here */
2076 .Case("eq_uq", 0x08)
2077 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002078 .Case("ngt", 0x0A)
2079 .Case("false", 0x0B)
2080 .Case("neq_oq", 0x0C)
2081 .Case("ge", 0x0D)
2082 .Case("gt", 0x0E)
2083 .Case("true", 0x0F)
2084 .Case("eq_os", 0x10)
2085 .Case("lt_oq", 0x11)
2086 .Case("le_oq", 0x12)
2087 .Case("unord_s", 0x13)
2088 .Case("neq_us", 0x14)
2089 .Case("nlt_uq", 0x15)
2090 .Case("nle_uq", 0x16)
2091 .Case("ord_s", 0x17)
2092 .Case("eq_us", 0x18)
2093 .Case("nge_uq", 0x19)
2094 .Case("ngt_uq", 0x1A)
2095 .Case("false_os", 0x1B)
2096 .Case("neq_os", 0x1C)
2097 .Case("ge_oq", 0x1D)
2098 .Case("gt_oq", 0x1E)
2099 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002100 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00002101 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002102 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
2103 getParser().getContext());
2104 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002105 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002106 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002107 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002108 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002109 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002110 } else {
2111 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002112 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002113 }
2114 }
2115 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002116
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002117 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002118
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002119 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002120 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00002121
Chris Lattner086a83a2010-09-08 05:17:37 +00002122 // Determine whether this is an instruction prefix.
2123 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00002124 Name == "lock" || Name == "rep" ||
2125 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00002126 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00002127 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00002128
2129
Chris Lattner086a83a2010-09-08 05:17:37 +00002130 // This does the actual operand parsing. Don't parse any more if we have a
2131 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2132 // just want to parse the "lock" as the first instruction and the "incl" as
2133 // the next one.
2134 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002135
2136 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002137 if (getLexer().is(AsmToken::Star))
2138 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002139
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002140 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002141 if (X86Operand *Op = ParseOperand())
2142 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002143 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002144 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002145 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002146 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002147
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002148 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002149 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002150
2151 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002152 if (X86Operand *Op = ParseOperand())
2153 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002154 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002155 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002156 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002157 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002158 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002159
Elena Demikhovsky89529742013-09-12 08:55:00 +00002160 if (STI.getFeatureBits() & X86::FeatureAVX512) {
2161 // Parse mask register {%k1}
2162 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002163 Operands.push_back(X86Operand::CreateToken("{", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002164 if (X86Operand *Op = ParseOperand()) {
2165 Operands.push_back(Op);
2166 if (!getLexer().is(AsmToken::RCurly)) {
2167 SMLoc Loc = getLexer().getLoc();
2168 Parser.eatToEndOfStatement();
2169 return Error(Loc, "Expected } at this point");
2170 }
Alp Tokera5b88a52013-12-02 16:06:06 +00002171 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002172 } else {
2173 Parser.eatToEndOfStatement();
2174 return true;
2175 }
2176 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +00002177 // TODO: add parsing of broadcasts {1to8}, {1to16}
Elena Demikhovsky89529742013-09-12 08:55:00 +00002178 // Parse "zeroing non-masked" semantic {z}
2179 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002180 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002181 if (!getLexer().is(AsmToken::Identifier) || getLexer().getTok().getIdentifier() != "z") {
2182 SMLoc Loc = getLexer().getLoc();
2183 Parser.eatToEndOfStatement();
2184 return Error(Loc, "Expected z at this point");
2185 }
2186 Parser.Lex(); // Eat the z
2187 if (!getLexer().is(AsmToken::RCurly)) {
2188 SMLoc Loc = getLexer().getLoc();
2189 Parser.eatToEndOfStatement();
2190 return Error(Loc, "Expected } at this point");
2191 }
2192 Parser.Lex(); // Eat the }
2193 }
2194 }
2195
Chris Lattnera2a9d162010-09-11 16:18:25 +00002196 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00002197 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002198 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00002199 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00002200 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002201 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002202
Chris Lattner086a83a2010-09-08 05:17:37 +00002203 if (getLexer().is(AsmToken::EndOfStatement))
2204 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00002205 else if (isPrefix && getLexer().is(AsmToken::Slash))
2206 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002207
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002208 if (ExtraImmOp && isParsingIntelSyntax())
2209 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2210
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002211 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2212 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2213 // documented form in various unofficial manuals, so a lot of code uses it.
2214 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2215 Operands.size() == 3) {
2216 X86Operand &Op = *(X86Operand*)Operands.back();
2217 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2218 isa<MCConstantExpr>(Op.Mem.Disp) &&
2219 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2220 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2221 SMLoc Loc = Op.getEndLoc();
2222 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2223 delete &Op;
2224 }
2225 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002226 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2227 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2228 Operands.size() == 3) {
2229 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2230 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2231 isa<MCConstantExpr>(Op.Mem.Disp) &&
2232 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2233 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2234 SMLoc Loc = Op.getEndLoc();
2235 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2236 delete &Op;
2237 }
2238 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002239 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
2240 if (Name.startswith("ins") && Operands.size() == 3 &&
2241 (Name == "insb" || Name == "insw" || Name == "insl")) {
2242 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2243 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2244 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
2245 Operands.pop_back();
2246 Operands.pop_back();
2247 delete &Op;
2248 delete &Op2;
2249 }
2250 }
2251
2252 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
2253 if (Name.startswith("outs") && Operands.size() == 3 &&
2254 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
2255 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2256 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2257 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2258 Operands.pop_back();
2259 Operands.pop_back();
2260 delete &Op;
2261 delete &Op2;
2262 }
2263 }
2264
2265 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2266 if (Name.startswith("movs") && Operands.size() == 3 &&
2267 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002268 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002269 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2270 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2271 if (isSrcOp(Op) && isDstOp(Op2)) {
2272 Operands.pop_back();
2273 Operands.pop_back();
2274 delete &Op;
2275 delete &Op2;
2276 }
2277 }
2278 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2279 if (Name.startswith("lods") && Operands.size() == 3 &&
2280 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002281 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002282 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2283 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2284 if (isSrcOp(*Op1) && Op2->isReg()) {
2285 const char *ins;
2286 unsigned reg = Op2->getReg();
2287 bool isLods = Name == "lods";
2288 if (reg == X86::AL && (isLods || Name == "lodsb"))
2289 ins = "lodsb";
2290 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2291 ins = "lodsw";
2292 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2293 ins = "lodsl";
2294 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2295 ins = "lodsq";
2296 else
2297 ins = NULL;
2298 if (ins != NULL) {
2299 Operands.pop_back();
2300 Operands.pop_back();
2301 delete Op1;
2302 delete Op2;
2303 if (Name != ins)
2304 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2305 }
2306 }
2307 }
2308 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2309 if (Name.startswith("stos") && Operands.size() == 3 &&
2310 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002311 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002312 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2313 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2314 if (isDstOp(*Op2) && Op1->isReg()) {
2315 const char *ins;
2316 unsigned reg = Op1->getReg();
2317 bool isStos = Name == "stos";
2318 if (reg == X86::AL && (isStos || Name == "stosb"))
2319 ins = "stosb";
2320 else if (reg == X86::AX && (isStos || Name == "stosw"))
2321 ins = "stosw";
2322 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2323 ins = "stosl";
2324 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2325 ins = "stosq";
2326 else
2327 ins = NULL;
2328 if (ins != NULL) {
2329 Operands.pop_back();
2330 Operands.pop_back();
2331 delete Op1;
2332 delete Op2;
2333 if (Name != ins)
2334 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2335 }
2336 }
2337 }
2338
Chris Lattner4bd21712010-09-15 04:33:27 +00002339 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002340 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002341 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002342 Name.startswith("shl") || Name.startswith("sal") ||
2343 Name.startswith("rcl") || Name.startswith("rcr") ||
2344 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002345 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002346 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002347 // Intel syntax
2348 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2349 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002350 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2351 delete Operands[2];
2352 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002353 }
2354 } else {
2355 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2356 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002357 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2358 delete Operands[1];
2359 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002360 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002361 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002362 }
Chad Rosier51afe632012-06-27 22:34:28 +00002363
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002364 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2365 // instalias with an immediate operand yet.
2366 if (Name == "int" && Operands.size() == 2) {
2367 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2368 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2369 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2370 delete Operands[1];
2371 Operands.erase(Operands.begin() + 1);
2372 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2373 }
2374 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002375
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002376 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002377}
2378
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002379static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2380 bool isCmp) {
2381 MCInst TmpInst;
2382 TmpInst.setOpcode(Opcode);
2383 if (!isCmp)
2384 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2385 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2386 TmpInst.addOperand(Inst.getOperand(0));
2387 Inst = TmpInst;
2388 return true;
2389}
2390
2391static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2392 bool isCmp = false) {
2393 if (!Inst.getOperand(0).isImm() ||
2394 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2395 return false;
2396
2397 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2398}
2399
2400static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2401 bool isCmp = false) {
2402 if (!Inst.getOperand(0).isImm() ||
2403 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2404 return false;
2405
2406 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2407}
2408
2409static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2410 bool isCmp = false) {
2411 if (!Inst.getOperand(0).isImm() ||
2412 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2413 return false;
2414
2415 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2416}
2417
Devang Patel4a6e7782012-01-12 18:03:40 +00002418bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002419processInstruction(MCInst &Inst,
2420 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2421 switch (Inst.getOpcode()) {
2422 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002423 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2424 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2425 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2426 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2427 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2428 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2429 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2430 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2431 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2432 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2433 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2434 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2435 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2436 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2437 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2438 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2439 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2440 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002441 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2442 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2443 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2444 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2445 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2446 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002447 case X86::VMOVAPDrr:
2448 case X86::VMOVAPDYrr:
2449 case X86::VMOVAPSrr:
2450 case X86::VMOVAPSYrr:
2451 case X86::VMOVDQArr:
2452 case X86::VMOVDQAYrr:
2453 case X86::VMOVDQUrr:
2454 case X86::VMOVDQUYrr:
2455 case X86::VMOVUPDrr:
2456 case X86::VMOVUPDYrr:
2457 case X86::VMOVUPSrr:
2458 case X86::VMOVUPSYrr: {
2459 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2460 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2461 return false;
2462
2463 unsigned NewOpc;
2464 switch (Inst.getOpcode()) {
2465 default: llvm_unreachable("Invalid opcode");
2466 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2467 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2468 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2469 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2470 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2471 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2472 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2473 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2474 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2475 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2476 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2477 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2478 }
2479 Inst.setOpcode(NewOpc);
2480 return true;
2481 }
2482 case X86::VMOVSDrr:
2483 case X86::VMOVSSrr: {
2484 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2485 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2486 return false;
2487 unsigned NewOpc;
2488 switch (Inst.getOpcode()) {
2489 default: llvm_unreachable("Invalid opcode");
2490 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2491 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2492 }
2493 Inst.setOpcode(NewOpc);
2494 return true;
2495 }
Devang Patelde47cce2012-01-18 22:42:29 +00002496 }
Devang Patelde47cce2012-01-18 22:42:29 +00002497}
2498
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002499static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002500bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002501MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002502 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002503 MCStreamer &Out, unsigned &ErrorInfo,
2504 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002505 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002506 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2507 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002508 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002509
Chris Lattnera63292a2010-09-29 01:50:45 +00002510 // First, handle aliases that expand to multiple instructions.
2511 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002512 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002513 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002514 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002515 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002516 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002517 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002518 MCInst Inst;
2519 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002520 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002521 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002522 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002523
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002524 const char *Repl =
2525 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002526 .Case("finit", "fninit")
2527 .Case("fsave", "fnsave")
2528 .Case("fstcw", "fnstcw")
2529 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002530 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002531 .Case("fstsw", "fnstsw")
2532 .Case("fstsww", "fnstsw")
2533 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002534 .Default(0);
2535 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002536 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002537 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002538 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002539
Chris Lattner628fbec2010-09-06 21:54:15 +00002540 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002541 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002542
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002543 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002544 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002545 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002546 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002547 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002548 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002549 // Some instructions need post-processing to, for example, tweak which
2550 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002551 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002552 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002553 while (processInstruction(Inst, Operands))
2554 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002555
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002556 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002557 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002558 Out.EmitInstruction(Inst);
2559 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002560 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002561 case Match_MissingFeature: {
2562 assert(ErrorInfo && "Unknown missing feature!");
2563 // Special case the error message for the very common case where only
2564 // a single subtarget feature is missing.
2565 std::string Msg = "instruction requires:";
2566 unsigned Mask = 1;
2567 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2568 if (ErrorInfo & Mask) {
2569 Msg += " ";
2570 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2571 }
2572 Mask <<= 1;
2573 }
2574 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2575 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002576 case Match_InvalidOperand:
2577 WasOriginallyInvalidOperand = true;
2578 break;
2579 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002580 break;
2581 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002582
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002583 // FIXME: Ideally, we would only attempt suffix matches for things which are
2584 // valid prefixes, and we could just infer the right unambiguous
2585 // type. However, that requires substantially more matcher support than the
2586 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002587
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002588 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002589 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002590 SmallString<16> Tmp;
2591 Tmp += Base;
2592 Tmp += ' ';
2593 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002594
Chris Lattnerfab94132010-11-06 18:28:02 +00002595 // If this instruction starts with an 'f', then it is a floating point stack
2596 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2597 // 80-bit floating point, which use the suffixes s,l,t respectively.
2598 //
2599 // Otherwise, we assume that this may be an integer instruction, which comes
2600 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2601 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002602
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002603 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002604 Tmp[Base.size()] = Suffixes[0];
2605 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002606 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002607 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002608
Chad Rosier2f480a82012-10-12 22:53:36 +00002609 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002610 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002611 // If this returned as a missing feature failure, remember that.
2612 if (Match1 == Match_MissingFeature)
2613 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002614 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002615 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002616 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002617 // If this returned as a missing feature failure, remember that.
2618 if (Match2 == Match_MissingFeature)
2619 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002620 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002621 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002622 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002623 // If this returned as a missing feature failure, remember that.
2624 if (Match3 == Match_MissingFeature)
2625 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002626 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002627 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002628 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002629 // If this returned as a missing feature failure, remember that.
2630 if (Match4 == Match_MissingFeature)
2631 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002632
2633 // Restore the old token.
2634 Op->setTokenValue(Base);
2635
2636 // If exactly one matched, then we treat that as a successful match (and the
2637 // instruction will already have been filled in correctly, since the failing
2638 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002639 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002640 (Match1 == Match_Success) + (Match2 == Match_Success) +
2641 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002642 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002643 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002644 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002645 Out.EmitInstruction(Inst);
2646 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002647 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002648 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002649
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002650 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002651
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002652 // If we had multiple suffix matches, then identify this as an ambiguous
2653 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002654 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002655 char MatchChars[4];
2656 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002657 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2658 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2659 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2660 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002661
2662 SmallString<126> Msg;
2663 raw_svector_ostream OS(Msg);
2664 OS << "ambiguous instructions require an explicit suffix (could be ";
2665 for (unsigned i = 0; i != NumMatches; ++i) {
2666 if (i != 0)
2667 OS << ", ";
2668 if (i + 1 == NumMatches)
2669 OS << "or ";
2670 OS << "'" << Base << MatchChars[i] << "'";
2671 }
2672 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002673 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002674 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002675 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002676
Chris Lattner628fbec2010-09-06 21:54:15 +00002677 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002678
Chris Lattner628fbec2010-09-06 21:54:15 +00002679 // If all of the instructions reported an invalid mnemonic, then the original
2680 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002681 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2682 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002683 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002684 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002685 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002686 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002687 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002688 }
2689
2690 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002691 if (ErrorInfo != ~0U) {
2692 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002693 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002694 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002695
Chad Rosier49963552012-10-13 00:26:04 +00002696 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002697 if (Operand->getStartLoc().isValid()) {
2698 SMRange OperandRange = Operand->getLocRange();
2699 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002700 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002701 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002702 }
2703
Chad Rosier3d4bc622012-08-21 19:36:59 +00002704 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002705 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002706 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002707
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002708 // If one instruction matched with a missing feature, report this as a
2709 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002710 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2711 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002712 std::string Msg = "instruction requires:";
2713 unsigned Mask = 1;
2714 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2715 if (ErrorInfoMissingFeature & Mask) {
2716 Msg += " ";
2717 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2718 }
2719 Mask <<= 1;
2720 }
2721 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002722 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002723
Chris Lattner628fbec2010-09-06 21:54:15 +00002724 // If one instruction matched with an invalid operand, report this as an
2725 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002726 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2727 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002728 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002729 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002730 return true;
2731 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002732
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002733 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002734 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002735 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002736 return true;
2737}
2738
2739
Devang Patel4a6e7782012-01-12 18:03:40 +00002740bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002741 StringRef IDVal = DirectiveID.getIdentifier();
2742 if (IDVal == ".word")
2743 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002744 else if (IDVal.startswith(".code"))
2745 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002746 else if (IDVal.startswith(".att_syntax")) {
2747 getParser().setAssemblerDialect(0);
2748 return false;
2749 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002750 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002751 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002752 // FIXME: Handle noprefix
2753 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002754 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002755 }
2756 return false;
2757 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002758 return true;
2759}
2760
2761/// ParseDirectiveWord
2762/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002763bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002764 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2765 for (;;) {
2766 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002767 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002768 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002769
Eric Christopherbf7bc492013-01-09 03:52:05 +00002770 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002771
Chris Lattner72c0b592010-10-30 17:38:55 +00002772 if (getLexer().is(AsmToken::EndOfStatement))
2773 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002774
Chris Lattner72c0b592010-10-30 17:38:55 +00002775 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002776 if (getLexer().isNot(AsmToken::Comma)) {
2777 Error(L, "unexpected token in directive");
2778 return false;
2779 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002780 Parser.Lex();
2781 }
2782 }
Chad Rosier51afe632012-06-27 22:34:28 +00002783
Chris Lattner72c0b592010-10-30 17:38:55 +00002784 Parser.Lex();
2785 return false;
2786}
2787
Evan Cheng481ebb02011-07-27 00:38:12 +00002788/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002789/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002790bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Craig Topper3c80d622014-01-06 04:55:54 +00002791 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002792 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002793 if (!is16BitMode()) {
2794 SwitchMode(X86::Mode16Bit);
2795 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2796 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002797 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002798 Parser.Lex();
2799 if (!is32BitMode()) {
2800 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002801 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2802 }
2803 } else if (IDVal == ".code64") {
2804 Parser.Lex();
2805 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002806 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002807 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2808 }
2809 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002810 Error(L, "unknown directive " + IDVal);
2811 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002812 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002813
Evan Cheng481ebb02011-07-27 00:38:12 +00002814 return false;
2815}
Chris Lattner72c0b592010-10-30 17:38:55 +00002816
Daniel Dunbar71475772009-07-17 20:42:00 +00002817// Force static initialization.
2818extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002819 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2820 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002821}
Daniel Dunbar00331992009-07-29 00:02:19 +00002822
Chris Lattner3e4582a2010-09-06 19:11:01 +00002823#define GET_REGISTER_MATCHER
2824#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002825#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002826#include "X86GenAsmMatcher.inc"