blob: aa857f087d8985af8e5d6df65677f6187ca09a9c [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 {
Craig Topper35da3d12014-01-16 07:36:58 +0000926 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000927 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 8);
928 }
929 bool isMemOffs16() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000930 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000931 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 16);
932 }
933 bool isMemOffs32() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000934 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000935 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 32);
936 }
937 bool isMemOffs64() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000938 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000939 !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 {
Craig Topper35da3d12014-01-16 07:36:58 +00001018 assert((N == 2) && "Invalid number of operands!");
Craig Topper18854172013-08-25 22:23:38 +00001019 // 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()));
Craig Topper35da3d12014-01-16 07:36:58 +00001024 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
Craig Topper18854172013-08-25 22:23:38 +00001025 }
1026
Chris Lattner528d00b2010-01-15 19:28:38 +00001027 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001028 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +00001029 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001030 Res->Tok.Data = Str.data();
1031 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +00001032 return Res;
1033 }
1034
Chad Rosier91c82662012-10-24 17:22:29 +00001035 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +00001036 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +00001037 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +00001038 StringRef SymName = StringRef(),
1039 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +00001040 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001041 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001042 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +00001043 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +00001044 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +00001045 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001046 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001047 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001048
Chad Rosierf3c04f62013-03-19 21:58:18 +00001049 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +00001050 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001051 Res->Imm.Val = Val;
1052 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001053 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001054
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001055 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +00001056 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +00001057 unsigned Size = 0, StringRef SymName = StringRef(),
1058 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001059 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
1060 Res->Mem.SegReg = 0;
1061 Res->Mem.Disp = Disp;
1062 Res->Mem.BaseReg = 0;
1063 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +00001064 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +00001065 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001066 Res->SymName = SymName;
1067 Res->OpDecl = OpDecl;
1068 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001069 return Res;
1070 }
1071
1072 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001073 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
1074 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +00001075 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +00001076 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +00001077 StringRef SymName = StringRef(),
1078 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001079 // We should never just have a displacement, that should be parsed as an
1080 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001081 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
1082
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001083 // The scale should always be one of {1,2,4,8}.
1084 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001085 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +00001086 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001087 Res->Mem.SegReg = SegReg;
1088 Res->Mem.Disp = Disp;
1089 Res->Mem.BaseReg = BaseReg;
1090 Res->Mem.IndexReg = IndexReg;
1091 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +00001092 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001093 Res->SymName = SymName;
1094 Res->OpDecl = OpDecl;
1095 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001096 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001097 }
1098};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00001099
Chris Lattner4eb9df02009-07-29 06:33:53 +00001100} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +00001101
Devang Patel4a6e7782012-01-12 18:03:40 +00001102bool X86AsmParser::isSrcOp(X86Operand &Op) {
Craig Topper3c80d622014-01-06 04:55:54 +00001103 unsigned basereg =
1104 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001105
1106 return (Op.isMem() &&
1107 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
1108 isa<MCConstantExpr>(Op.Mem.Disp) &&
1109 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1110 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
1111}
1112
Devang Patel4a6e7782012-01-12 18:03:40 +00001113bool X86AsmParser::isDstOp(X86Operand &Op) {
Craig Topper3c80d622014-01-06 04:55:54 +00001114 unsigned basereg =
1115 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001116
Chad Rosier51afe632012-06-27 22:34:28 +00001117 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +00001118 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001119 isa<MCConstantExpr>(Op.Mem.Disp) &&
1120 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1121 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
1122}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001123
Devang Patel4a6e7782012-01-12 18:03:40 +00001124bool X86AsmParser::ParseRegister(unsigned &RegNo,
1125 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001126 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001127 const AsmToken &PercentTok = Parser.getTok();
1128 StartLoc = PercentTok.getLoc();
1129
1130 // If we encounter a %, ignore it. This code handles registers with and
1131 // without the prefix, unprefixed registers can occur in cfi directives.
1132 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001133 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001134
Sean Callanan936b0d32010-01-19 21:44:56 +00001135 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001136 EndLoc = Tok.getEndLoc();
1137
Devang Patelce6a2ca2012-01-20 22:32:05 +00001138 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001139 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001140 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001141 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001142 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001143
Kevin Enderby7d912182009-09-03 17:15:07 +00001144 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001145
Chris Lattner1261b812010-09-22 04:11:10 +00001146 // If the match failed, try the register name as lowercase.
1147 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001148 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001149
Evan Chengeda1d4f2011-07-27 23:22:03 +00001150 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +00001151 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +00001152 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1153 // checked.
1154 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1155 // REX prefix.
1156 if (RegNo == X86::RIZ ||
1157 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1158 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1159 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001160 return Error(StartLoc, "register %"
1161 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001162 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001163 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001164
Chris Lattner1261b812010-09-22 04:11:10 +00001165 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1166 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001167 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001168 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001169
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001170 // Check to see if we have '(4)' after %st.
1171 if (getLexer().isNot(AsmToken::LParen))
1172 return false;
1173 // Lex the paren.
1174 getParser().Lex();
1175
1176 const AsmToken &IntTok = Parser.getTok();
1177 if (IntTok.isNot(AsmToken::Integer))
1178 return Error(IntTok.getLoc(), "expected stack index");
1179 switch (IntTok.getIntVal()) {
1180 case 0: RegNo = X86::ST0; break;
1181 case 1: RegNo = X86::ST1; break;
1182 case 2: RegNo = X86::ST2; break;
1183 case 3: RegNo = X86::ST3; break;
1184 case 4: RegNo = X86::ST4; break;
1185 case 5: RegNo = X86::ST5; break;
1186 case 6: RegNo = X86::ST6; break;
1187 case 7: RegNo = X86::ST7; break;
1188 default: return Error(IntTok.getLoc(), "invalid stack index");
1189 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001190
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001191 if (getParser().Lex().isNot(AsmToken::RParen))
1192 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001193
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001194 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001195 Parser.Lex(); // Eat ')'
1196 return false;
1197 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001198
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001199 EndLoc = Parser.getTok().getEndLoc();
1200
Chris Lattner80486622010-06-24 07:29:18 +00001201 // If this is "db[0-7]", match it as an alias
1202 // for dr[0-7].
1203 if (RegNo == 0 && Tok.getString().size() == 3 &&
1204 Tok.getString().startswith("db")) {
1205 switch (Tok.getString()[2]) {
1206 case '0': RegNo = X86::DR0; break;
1207 case '1': RegNo = X86::DR1; break;
1208 case '2': RegNo = X86::DR2; break;
1209 case '3': RegNo = X86::DR3; break;
1210 case '4': RegNo = X86::DR4; break;
1211 case '5': RegNo = X86::DR5; break;
1212 case '6': RegNo = X86::DR6; break;
1213 case '7': RegNo = X86::DR7; break;
1214 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001215
Chris Lattner80486622010-06-24 07:29:18 +00001216 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001217 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001218 Parser.Lex(); // Eat it.
1219 return false;
1220 }
1221 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001222
Devang Patelce6a2ca2012-01-20 22:32:05 +00001223 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001224 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001225 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001226 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001227 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001228
Sean Callanana83fd7d2010-01-19 20:27:46 +00001229 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001230 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001231}
1232
Devang Patel4a6e7782012-01-12 18:03:40 +00001233X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001234 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001235 return ParseIntelOperand();
1236 return ParseATTOperand();
1237}
1238
Devang Patel41b9dde2012-01-17 18:00:18 +00001239/// getIntelMemOperandSize - Return intel memory operand size.
1240static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001241 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001242 .Cases("BYTE", "byte", 8)
1243 .Cases("WORD", "word", 16)
1244 .Cases("DWORD", "dword", 32)
1245 .Cases("QWORD", "qword", 64)
1246 .Cases("XWORD", "xword", 80)
1247 .Cases("XMMWORD", "xmmword", 128)
1248 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001249 .Default(0);
1250 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001251}
1252
Chad Rosier175d0ae2013-04-12 18:21:18 +00001253X86Operand *
1254X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1255 unsigned BaseReg, unsigned IndexReg,
1256 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001257 unsigned Size, StringRef Identifier,
1258 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001259 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001260 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1261 // reference. We need an 'r' constraint here, so we need to create register
1262 // operand to ensure proper matching. Just pick a GPR based on the size of
1263 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001264 if (!Info.IsVarDecl) {
Craig Topper3c80d622014-01-06 04:55:54 +00001265 unsigned RegNo =
1266 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001267 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001268 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001269 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001270 if (!Size) {
1271 Size = Info.Type * 8; // Size is in terms of bits in this context.
1272 if (Size)
1273 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1274 /*Len=*/0, Size));
1275 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001276 }
1277
Chad Rosier7ca135b2013-03-19 21:11:56 +00001278 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001279 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001280 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001281 BaseReg = BaseReg ? BaseReg : 1;
1282 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001283 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001284}
1285
Chad Rosierd383db52013-04-12 20:20:54 +00001286static void
1287RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1288 StringRef SymName, int64_t ImmDisp,
1289 int64_t FinalImmDisp, SMLoc &BracLoc,
1290 SMLoc &StartInBrac, SMLoc &End) {
1291 // Remove the '[' and ']' from the IR string.
1292 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1293 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1294
1295 // If ImmDisp is non-zero, then we parsed a displacement before the
1296 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1297 // If ImmDisp doesn't match the displacement computed by the state machine
1298 // then we have an additional displacement in the bracketed expression.
1299 if (ImmDisp != FinalImmDisp) {
1300 if (ImmDisp) {
1301 // We have an immediate displacement before the bracketed expression.
1302 // Adjust this to match the final immediate displacement.
1303 bool Found = false;
1304 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1305 E = AsmRewrites->end(); I != E; ++I) {
1306 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1307 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001308 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1309 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001310 (*I).Kind = AOK_Imm;
1311 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1312 (*I).Val = FinalImmDisp;
1313 Found = true;
1314 break;
1315 }
1316 }
1317 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001318 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001319 } else {
1320 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001321 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001322 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001323 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001324 }
1325 }
1326 // Remove all the ImmPrefix rewrites within the brackets.
1327 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1328 E = AsmRewrites->end(); I != E; ++I) {
1329 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1330 continue;
1331 if ((*I).Kind == AOK_ImmPrefix)
1332 (*I).Kind = AOK_Delete;
1333 }
1334 const char *SymLocPtr = SymName.data();
1335 // Skip everything before the symbol.
1336 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1337 assert(Len > 0 && "Expected a non-negative length.");
1338 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1339 }
1340 // Skip everything after the symbol.
1341 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1342 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1343 assert(Len > 0 && "Expected a non-negative length.");
1344 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1345 }
1346}
1347
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001348bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001349 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001350
Chad Rosier5c118fd2013-01-14 22:31:35 +00001351 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001352 while (!Done) {
1353 bool UpdateLocLex = true;
1354
1355 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1356 // identifier. Don't try an parse it as a register.
1357 if (Tok.getString().startswith("."))
1358 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001359
1360 // If we're parsing an immediate expression, we don't expect a '['.
1361 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1362 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001363
1364 switch (getLexer().getKind()) {
1365 default: {
1366 if (SM.isValidEndState()) {
1367 Done = true;
1368 break;
1369 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001370 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001371 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001372 case AsmToken::EndOfStatement: {
1373 Done = true;
1374 break;
1375 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001376 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001377 // This could be a register or a symbolic displacement.
1378 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001379 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001380 SMLoc IdentLoc = Tok.getLoc();
1381 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001382 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001383 SM.onRegister(TmpReg);
1384 UpdateLocLex = false;
1385 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001386 } else {
1387 if (!isParsingInlineAsm()) {
1388 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001389 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001390 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001391 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001392 if (ParseIntelIdentifier(Val, Identifier, Info,
1393 /*Unevaluated=*/false, End))
1394 return true;
Chad Rosier95ce8892013-04-19 18:39:50 +00001395 }
1396 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001397 UpdateLocLex = false;
1398 break;
1399 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001400 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001401 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001402 case AsmToken::Integer: {
Chad Rosierbfb70992013-04-17 00:11:46 +00001403 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001404 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1405 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001406 // Look for 'b' or 'f' following an Integer as a directional label
1407 SMLoc Loc = getTok().getLoc();
1408 int64_t IntVal = getTok().getIntVal();
1409 End = consumeToken();
1410 UpdateLocLex = false;
1411 if (getLexer().getKind() == AsmToken::Identifier) {
1412 StringRef IDVal = getTok().getString();
1413 if (IDVal == "f" || IDVal == "b") {
1414 MCSymbol *Sym =
1415 getContext().GetDirectionalLocalSymbol(IntVal,
1416 IDVal == "f" ? 1 : 0);
1417 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1418 const MCExpr *Val =
1419 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1420 if (IDVal == "b" && Sym->isUndefined())
1421 return Error(Loc, "invalid reference to undefined symbol");
1422 StringRef Identifier = Sym->getName();
1423 SM.onIdentifierExpr(Val, Identifier);
1424 End = consumeToken();
1425 } else {
1426 SM.onInteger(IntVal);
1427 }
1428 } else {
1429 SM.onInteger(IntVal);
1430 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001431 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001432 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001433 case AsmToken::Plus: SM.onPlus(); break;
1434 case AsmToken::Minus: SM.onMinus(); break;
1435 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001436 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001437 case AsmToken::Pipe: SM.onOr(); break;
1438 case AsmToken::Amp: SM.onAnd(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001439 case AsmToken::LBrac: SM.onLBrac(); break;
1440 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001441 case AsmToken::LParen: SM.onLParen(); break;
1442 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001443 }
Chad Rosier31246272013-04-17 21:01:45 +00001444 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001445 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001446
Alp Tokera5b88a52013-12-02 16:06:06 +00001447 if (!Done && UpdateLocLex)
1448 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001449 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001450 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001451}
1452
1453X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001454 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001455 unsigned Size) {
1456 const AsmToken &Tok = Parser.getTok();
1457 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1458 if (getLexer().isNot(AsmToken::LBrac))
1459 return ErrorOperand(BracLoc, "Expected '[' token!");
1460 Parser.Lex(); // Eat '['
1461
1462 SMLoc StartInBrac = Tok.getLoc();
1463 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1464 // may have already parsed an immediate displacement before the bracketed
1465 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001466 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001467 if (ParseIntelExpression(SM, End))
1468 return 0;
Devang Patel41b9dde2012-01-17 18:00:18 +00001469
Chad Rosier175d0ae2013-04-12 18:21:18 +00001470 const MCExpr *Disp;
1471 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001472 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001473 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001474 if (isParsingInlineAsm())
1475 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001476 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001477 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001478 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001479 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001480 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001481 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001482
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001483 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001484 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001485 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001486 if (ParseIntelDotOperator(Disp, NewDisp))
1487 return 0;
Chad Rosier911c1f32012-10-25 17:37:43 +00001488
Chad Rosier70f47592013-04-10 20:07:47 +00001489 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001490 Parser.Lex(); // Eat the field.
1491 Disp = NewDisp;
1492 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001493
Chad Rosier5c118fd2013-01-14 22:31:35 +00001494 int BaseReg = SM.getBaseReg();
1495 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001496 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001497 if (!isParsingInlineAsm()) {
1498 // handle [-42]
1499 if (!BaseReg && !IndexReg) {
1500 if (!SegReg)
1501 return X86Operand::CreateMem(Disp, Start, End, Size);
1502 else
1503 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1504 }
1505 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1506 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001507 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001508
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001509 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001510 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001511 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001512}
1513
Chad Rosier8a244662013-04-02 20:02:33 +00001514// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001515bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1516 StringRef &Identifier,
1517 InlineAsmIdentifierInfo &Info,
1518 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001519 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1520 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001521
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001522 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001523 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001524
Chad Rosier8a244662013-04-02 20:02:33 +00001525 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001526
1527 // Advance the token stream until the end of the current token is
1528 // after the end of what the frontend claimed.
1529 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1530 while (true) {
1531 End = Tok.getEndLoc();
1532 getLexer().Lex();
1533
1534 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1535 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001536 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001537
1538 // Create the symbol reference.
1539 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001540 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1541 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001542 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001543 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001544}
1545
David Majnemeraa34d792013-08-27 21:56:17 +00001546/// \brief Parse intel style segment override.
1547X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1548 SMLoc Start,
1549 unsigned Size) {
1550 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1551 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1552 if (Tok.isNot(AsmToken::Colon))
1553 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1554 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001555
David Majnemeraa34d792013-08-27 21:56:17 +00001556 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001557 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001558 ImmDisp = Tok.getIntVal();
1559 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1560
Chad Rosier1530ba52013-03-27 21:49:56 +00001561 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001562 InstInfo->AsmRewrites->push_back(
1563 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1564
1565 if (getLexer().isNot(AsmToken::LBrac)) {
1566 // An immediate following a 'segment register', 'colon' token sequence can
1567 // be followed by a bracketed expression. If it isn't we know we have our
1568 // final segment override.
1569 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1570 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1571 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1572 Size);
1573 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001574 }
1575
Chad Rosier91c82662012-10-24 17:22:29 +00001576 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001577 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001578
David Majnemeraa34d792013-08-27 21:56:17 +00001579 const MCExpr *Val;
1580 SMLoc End;
1581 if (!isParsingInlineAsm()) {
1582 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001583 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001584
1585 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001586 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001587
David Majnemeraa34d792013-08-27 21:56:17 +00001588 InlineAsmIdentifierInfo Info;
1589 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001590 if (ParseIntelIdentifier(Val, Identifier, Info,
1591 /*Unevaluated=*/false, End))
1592 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001593 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1594 /*Scale=*/1, Start, End, Size, Identifier, Info);
1595}
1596
1597/// ParseIntelMemOperand - Parse intel style memory operand.
1598X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1599 unsigned Size) {
1600 const AsmToken &Tok = Parser.getTok();
1601 SMLoc End;
1602
1603 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1604 if (getLexer().is(AsmToken::LBrac))
1605 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1606
Chad Rosier95ce8892013-04-19 18:39:50 +00001607 const MCExpr *Val;
1608 if (!isParsingInlineAsm()) {
1609 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001610 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001611
1612 return X86Operand::CreateMem(Val, Start, End, Size);
1613 }
1614
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001615 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001616 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001617 if (ParseIntelIdentifier(Val, Identifier, Info,
1618 /*Unevaluated=*/false, End))
1619 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001620 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001621 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001622}
1623
Chad Rosier5dcb4662012-10-24 22:21:50 +00001624/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001625bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001626 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001627 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001628 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001629
1630 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001631 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001632 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001633 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001634 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001635
1636 // Drop the '.'.
1637 StringRef DotDispStr = Tok.getString().drop_front(1);
1638
Chad Rosier5dcb4662012-10-24 22:21:50 +00001639 // .Imm gets lexed as a real.
1640 if (Tok.is(AsmToken::Real)) {
1641 APInt DotDisp;
1642 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001643 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001644 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001645 unsigned DotDisp;
1646 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1647 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001648 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001649 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001650 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001651 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001652 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001653
Chad Rosier240b7b92012-10-25 21:51:10 +00001654 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1655 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1656 unsigned Len = DotDispStr.size();
1657 unsigned Val = OrigDispVal + DotDispVal;
1658 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1659 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001660 }
1661
Chad Rosiercc541e82013-04-19 15:57:00 +00001662 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001663 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001664}
1665
Chad Rosier91c82662012-10-24 17:22:29 +00001666/// Parse the 'offset' operator. This operator is used to specify the
1667/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001668X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001669 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001670 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001671 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001672
Chad Rosier91c82662012-10-24 17:22:29 +00001673 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001674 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001675 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001676 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001677 if (ParseIntelIdentifier(Val, Identifier, Info,
1678 /*Unevaluated=*/false, End))
1679 return 0;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001680
Chad Rosiere2f03772012-10-26 16:09:20 +00001681 // Don't emit the offset operator.
1682 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1683
Chad Rosier91c82662012-10-24 17:22:29 +00001684 // The offset operator will have an 'r' constraint, thus we need to create
1685 // register operand to ensure proper matching. Just pick a GPR based on
1686 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001687 unsigned RegNo =
1688 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001689 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001690 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001691}
1692
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001693enum IntelOperatorKind {
1694 IOK_LENGTH,
1695 IOK_SIZE,
1696 IOK_TYPE
1697};
1698
1699/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1700/// returns the number of elements in an array. It returns the value 1 for
1701/// non-array variables. The SIZE operator returns the size of a C or C++
1702/// variable. A variable's size is the product of its LENGTH and TYPE. The
1703/// TYPE operator returns the size of a C or C++ type or variable. If the
1704/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001705X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001706 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001707 SMLoc TypeLoc = Tok.getLoc();
1708 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001709
Chad Rosier95ce8892013-04-19 18:39:50 +00001710 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001711 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001712 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001713 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001714 if (ParseIntelIdentifier(Val, Identifier, Info,
1715 /*Unevaluated=*/true, End))
1716 return 0;
1717
1718 if (!Info.OpDecl)
1719 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001720
Chad Rosierf6675c32013-04-22 17:01:46 +00001721 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001722 switch(OpKind) {
1723 default: llvm_unreachable("Unexpected operand kind!");
1724 case IOK_LENGTH: CVal = Info.Length; break;
1725 case IOK_SIZE: CVal = Info.Size; break;
1726 case IOK_TYPE: CVal = Info.Type; break;
1727 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001728
1729 // Rewrite the type operator and the C or C++ type or variable in terms of an
1730 // immediate. E.g. TYPE foo -> $$4
1731 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001732 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001733
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001734 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001735 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001736}
1737
Devang Patel41b9dde2012-01-17 18:00:18 +00001738X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001739 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001740 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001741
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001742 // Offset, length, type and size operators.
1743 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001744 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001745 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001746 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001747 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001748 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001749 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001750 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001751 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001752 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001753 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001754
David Majnemeraa34d792013-08-27 21:56:17 +00001755 unsigned Size = getIntelMemOperandSize(Tok.getString());
1756 if (Size) {
1757 Parser.Lex(); // Eat operand size (e.g., byte, word).
1758 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1759 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1760 Parser.Lex(); // Eat ptr.
1761 }
1762 Start = Tok.getLoc();
1763
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001764 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001765 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1766 getLexer().is(AsmToken::LParen)) {
1767 AsmToken StartTok = Tok;
1768 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1769 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001770 if (ParseIntelExpression(SM, End))
1771 return 0;
Chad Rosierbfb70992013-04-17 00:11:46 +00001772
1773 int64_t Imm = SM.getImm();
1774 if (isParsingInlineAsm()) {
1775 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1776 if (StartTok.getString().size() == Len)
1777 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001778 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001779 else
1780 // Otherwise, rewrite the complex expression as a single immediate.
1781 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001782 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001783
1784 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001785 // If a directional label (ie. 1f or 2b) was parsed above from
1786 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1787 // to the MCExpr with the directional local symbol and this is a
1788 // memory operand not an immediate operand.
1789 if (SM.getSym())
1790 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1791
Chad Rosierbfb70992013-04-17 00:11:46 +00001792 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1793 return X86Operand::CreateImm(ImmExpr, Start, End);
1794 }
1795
1796 // Only positive immediates are valid.
1797 if (Imm < 0)
1798 return ErrorOperand(Start, "expected a positive immediate displacement "
1799 "before bracketed expr.");
1800
1801 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001802 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001803 }
1804
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001805 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001806 unsigned RegNo = 0;
1807 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001808 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001809 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001810 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001811 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001812
David Majnemeraa34d792013-08-27 21:56:17 +00001813 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001814 }
1815
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001816 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001817 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001818}
1819
Devang Patel4a6e7782012-01-12 18:03:40 +00001820X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001821 switch (getLexer().getKind()) {
1822 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001823 // Parse a memory operand with no segment register.
1824 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001825 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001826 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001827 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001828 SMLoc Start, End;
1829 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001830 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001831 Error(Start, "%eiz and %riz can only be used as index registers",
1832 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001833 return 0;
1834 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001835
Chris Lattnerb9270732010-04-17 18:56:34 +00001836 // If this is a segment register followed by a ':', then this is the start
1837 // of a memory reference, otherwise this is a normal register reference.
1838 if (getLexer().isNot(AsmToken::Colon))
1839 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001840
Chris Lattnerb9270732010-04-17 18:56:34 +00001841 getParser().Lex(); // Eat the colon.
1842 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001843 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001844 case AsmToken::Dollar: {
1845 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001846 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001847 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001848 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001849 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001850 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001851 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001852 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001853 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001854}
1855
Chris Lattnerb9270732010-04-17 18:56:34 +00001856/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1857/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001858X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001859
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001860 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1861 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001862 // only way to do this without lookahead is to eat the '(' and see what is
1863 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001864 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001865 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001866 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001867 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001868
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001869 // After parsing the base expression we could either have a parenthesized
1870 // memory address or not. If not, return now. If so, eat the (.
1871 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001872 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001873 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001874 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001875 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001876 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001877
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001878 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001879 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001880 } else {
1881 // Okay, we have a '('. We don't know if this is an expression or not, but
1882 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001883 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001884 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001885
Kevin Enderby7d912182009-09-03 17:15:07 +00001886 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001887 // Nothing to do here, fall into the code below with the '(' part of the
1888 // memory operand consumed.
1889 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001890 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001891
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001892 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001893 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001894 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001895
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001896 // After parsing the base expression we could either have a parenthesized
1897 // memory address or not. If not, return now. If so, eat the (.
1898 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001899 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001900 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001901 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001902 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001903 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001904
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001905 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001906 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001907 }
1908 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001909
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001910 // If we reached here, then we just ate the ( of the memory operand. Process
1911 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001912 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001913 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001914
Chris Lattner0c2538f2010-01-15 18:51:29 +00001915 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001916 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001917 BaseLoc = Parser.getTok().getLoc();
Benjamin Kramer1930b002011-10-16 12:10:27 +00001918 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001919 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001920 Error(StartLoc, "eiz and riz can only be used as index registers",
1921 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001922 return 0;
1923 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001924 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001925
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001926 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001927 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001928 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001929
1930 // Following the comma we should have either an index register, or a scale
1931 // value. We don't support the later form, but we want to parse it
1932 // correctly.
1933 //
1934 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001935 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001936 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001937 SMLoc L;
1938 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001939
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001940 if (getLexer().isNot(AsmToken::RParen)) {
1941 // Parse the scale amount:
1942 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001943 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001944 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001945 "expected comma in scale expression");
1946 return 0;
1947 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001948 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001949
1950 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001951 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001952
1953 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001954 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001955 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001956 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001957 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001958
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001959 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001960 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1961 ScaleVal != 1) {
1962 Error(Loc, "scale factor in 16-bit address must be 1");
1963 return 0;
1964 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001965 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1966 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1967 return 0;
1968 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001969 Scale = (unsigned)ScaleVal;
1970 }
1971 }
1972 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001973 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001974 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001975 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001976
1977 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001978 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001979 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001980
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001981 if (Value != 1)
1982 Warning(Loc, "scale factor without index register is ignored");
1983 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001984 }
1985 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001986
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001987 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001988 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001989 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001990 return 0;
1991 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001992 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001993 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001994
David Woodhouse6dbda442014-01-08 12:58:28 +00001995 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1996 // and then only in non-64-bit modes. Except for DX, which is a special case
1997 // because an unofficial form of in/out instructions uses it.
1998 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1999 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
2000 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2001 BaseReg != X86::DX) {
2002 Error(BaseLoc, "invalid 16-bit base register");
2003 return 0;
2004 }
2005 if (BaseReg == 0 &&
2006 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2007 Error(IndexLoc, "16-bit memory operand may not include only index register");
2008 return 0;
2009 }
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002010 // If we have both a base register and an index register make sure they are
2011 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00002012 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002013 if (BaseReg != 0 && IndexReg != 0) {
2014 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00002015 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
2016 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002017 IndexReg != X86::RIZ) {
David Woodhouse6dbda442014-01-08 12:58:28 +00002018 Error(BaseLoc, "base register is 64-bit, but index register is not");
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002019 return 0;
2020 }
2021 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00002022 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
2023 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002024 IndexReg != X86::EIZ){
David Woodhouse6dbda442014-01-08 12:58:28 +00002025 Error(BaseLoc, "base register is 32-bit, but index register is not");
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002026 return 0;
2027 }
David Woodhouse6dbda442014-01-08 12:58:28 +00002028 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
2029 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
2030 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
2031 Error(BaseLoc, "base register is 16-bit, but index register is not");
2032 return 0;
2033 }
2034 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
2035 IndexReg != X86::SI && IndexReg != X86::DI) ||
2036 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
2037 IndexReg != X86::BX && IndexReg != X86::BP)) {
2038 Error(BaseLoc, "invalid 16-bit base/index register combination");
2039 return 0;
2040 }
2041 }
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002042 }
2043
Chris Lattner015cfb12010-01-15 19:33:43 +00002044 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
2045 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002046}
2047
Devang Patel4a6e7782012-01-12 18:03:40 +00002048bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00002049ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002050 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00002051 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002052 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002053
Chris Lattner7e8a99b2010-11-28 20:23:50 +00002054 // FIXME: Hack to recognize setneb as setne.
2055 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
2056 PatchedName != "setb" && PatchedName != "setnb")
2057 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00002058
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002059 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
2060 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002061 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002062 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
2063 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00002064 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002065 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002066 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002067 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00002068 .Case("eq", 0x00)
2069 .Case("lt", 0x01)
2070 .Case("le", 0x02)
2071 .Case("unord", 0x03)
2072 .Case("neq", 0x04)
2073 .Case("nlt", 0x05)
2074 .Case("nle", 0x06)
2075 .Case("ord", 0x07)
2076 /* AVX only from here */
2077 .Case("eq_uq", 0x08)
2078 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002079 .Case("ngt", 0x0A)
2080 .Case("false", 0x0B)
2081 .Case("neq_oq", 0x0C)
2082 .Case("ge", 0x0D)
2083 .Case("gt", 0x0E)
2084 .Case("true", 0x0F)
2085 .Case("eq_os", 0x10)
2086 .Case("lt_oq", 0x11)
2087 .Case("le_oq", 0x12)
2088 .Case("unord_s", 0x13)
2089 .Case("neq_us", 0x14)
2090 .Case("nlt_uq", 0x15)
2091 .Case("nle_uq", 0x16)
2092 .Case("ord_s", 0x17)
2093 .Case("eq_us", 0x18)
2094 .Case("nge_uq", 0x19)
2095 .Case("ngt_uq", 0x1A)
2096 .Case("false_os", 0x1B)
2097 .Case("neq_os", 0x1C)
2098 .Case("ge_oq", 0x1D)
2099 .Case("gt_oq", 0x1E)
2100 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002101 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00002102 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002103 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
2104 getParser().getContext());
2105 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002106 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002107 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002108 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002109 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002110 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002111 } else {
2112 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002113 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002114 }
2115 }
2116 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002117
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002118 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002119
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002120 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002121 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00002122
Chris Lattner086a83a2010-09-08 05:17:37 +00002123 // Determine whether this is an instruction prefix.
2124 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00002125 Name == "lock" || Name == "rep" ||
2126 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00002127 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00002128 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00002129
2130
Chris Lattner086a83a2010-09-08 05:17:37 +00002131 // This does the actual operand parsing. Don't parse any more if we have a
2132 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2133 // just want to parse the "lock" as the first instruction and the "incl" as
2134 // the next one.
2135 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002136
2137 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002138 if (getLexer().is(AsmToken::Star))
2139 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002140
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002141 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002142 if (X86Operand *Op = ParseOperand())
2143 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002144 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002145 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002146 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002147 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002148
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002149 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002150 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002151
2152 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002153 if (X86Operand *Op = ParseOperand())
2154 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002155 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002156 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002157 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002158 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002159 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002160
Elena Demikhovsky89529742013-09-12 08:55:00 +00002161 if (STI.getFeatureBits() & X86::FeatureAVX512) {
2162 // Parse mask register {%k1}
2163 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002164 Operands.push_back(X86Operand::CreateToken("{", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002165 if (X86Operand *Op = ParseOperand()) {
2166 Operands.push_back(Op);
2167 if (!getLexer().is(AsmToken::RCurly)) {
2168 SMLoc Loc = getLexer().getLoc();
2169 Parser.eatToEndOfStatement();
2170 return Error(Loc, "Expected } at this point");
2171 }
Alp Tokera5b88a52013-12-02 16:06:06 +00002172 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002173 } else {
2174 Parser.eatToEndOfStatement();
2175 return true;
2176 }
2177 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +00002178 // TODO: add parsing of broadcasts {1to8}, {1to16}
Elena Demikhovsky89529742013-09-12 08:55:00 +00002179 // Parse "zeroing non-masked" semantic {z}
2180 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002181 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002182 if (!getLexer().is(AsmToken::Identifier) || getLexer().getTok().getIdentifier() != "z") {
2183 SMLoc Loc = getLexer().getLoc();
2184 Parser.eatToEndOfStatement();
2185 return Error(Loc, "Expected z at this point");
2186 }
2187 Parser.Lex(); // Eat the z
2188 if (!getLexer().is(AsmToken::RCurly)) {
2189 SMLoc Loc = getLexer().getLoc();
2190 Parser.eatToEndOfStatement();
2191 return Error(Loc, "Expected } at this point");
2192 }
2193 Parser.Lex(); // Eat the }
2194 }
2195 }
2196
Chris Lattnera2a9d162010-09-11 16:18:25 +00002197 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00002198 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002199 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00002200 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00002201 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002202 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002203
Chris Lattner086a83a2010-09-08 05:17:37 +00002204 if (getLexer().is(AsmToken::EndOfStatement))
2205 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00002206 else if (isPrefix && getLexer().is(AsmToken::Slash))
2207 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002208
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002209 if (ExtraImmOp && isParsingIntelSyntax())
2210 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2211
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002212 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2213 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2214 // documented form in various unofficial manuals, so a lot of code uses it.
2215 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2216 Operands.size() == 3) {
2217 X86Operand &Op = *(X86Operand*)Operands.back();
2218 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2219 isa<MCConstantExpr>(Op.Mem.Disp) &&
2220 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2221 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2222 SMLoc Loc = Op.getEndLoc();
2223 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2224 delete &Op;
2225 }
2226 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002227 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2228 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2229 Operands.size() == 3) {
2230 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2231 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2232 isa<MCConstantExpr>(Op.Mem.Disp) &&
2233 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2234 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2235 SMLoc Loc = Op.getEndLoc();
2236 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2237 delete &Op;
2238 }
2239 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002240 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
2241 if (Name.startswith("ins") && Operands.size() == 3 &&
2242 (Name == "insb" || Name == "insw" || Name == "insl")) {
2243 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2244 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2245 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
2246 Operands.pop_back();
2247 Operands.pop_back();
2248 delete &Op;
2249 delete &Op2;
2250 }
2251 }
2252
2253 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
2254 if (Name.startswith("outs") && Operands.size() == 3 &&
2255 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
2256 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2257 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2258 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2259 Operands.pop_back();
2260 Operands.pop_back();
2261 delete &Op;
2262 delete &Op2;
2263 }
2264 }
2265
2266 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2267 if (Name.startswith("movs") && Operands.size() == 3 &&
2268 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002269 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002270 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2271 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2272 if (isSrcOp(Op) && isDstOp(Op2)) {
2273 Operands.pop_back();
2274 Operands.pop_back();
2275 delete &Op;
2276 delete &Op2;
2277 }
2278 }
2279 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2280 if (Name.startswith("lods") && Operands.size() == 3 &&
2281 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002282 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002283 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2284 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2285 if (isSrcOp(*Op1) && Op2->isReg()) {
2286 const char *ins;
2287 unsigned reg = Op2->getReg();
2288 bool isLods = Name == "lods";
2289 if (reg == X86::AL && (isLods || Name == "lodsb"))
2290 ins = "lodsb";
2291 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2292 ins = "lodsw";
2293 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2294 ins = "lodsl";
2295 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2296 ins = "lodsq";
2297 else
2298 ins = NULL;
2299 if (ins != NULL) {
2300 Operands.pop_back();
2301 Operands.pop_back();
2302 delete Op1;
2303 delete Op2;
2304 if (Name != ins)
2305 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2306 }
2307 }
2308 }
2309 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2310 if (Name.startswith("stos") && Operands.size() == 3 &&
2311 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002312 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002313 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2314 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2315 if (isDstOp(*Op2) && Op1->isReg()) {
2316 const char *ins;
2317 unsigned reg = Op1->getReg();
2318 bool isStos = Name == "stos";
2319 if (reg == X86::AL && (isStos || Name == "stosb"))
2320 ins = "stosb";
2321 else if (reg == X86::AX && (isStos || Name == "stosw"))
2322 ins = "stosw";
2323 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2324 ins = "stosl";
2325 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2326 ins = "stosq";
2327 else
2328 ins = NULL;
2329 if (ins != NULL) {
2330 Operands.pop_back();
2331 Operands.pop_back();
2332 delete Op1;
2333 delete Op2;
2334 if (Name != ins)
2335 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2336 }
2337 }
2338 }
2339
Chris Lattner4bd21712010-09-15 04:33:27 +00002340 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002341 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002342 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002343 Name.startswith("shl") || Name.startswith("sal") ||
2344 Name.startswith("rcl") || Name.startswith("rcr") ||
2345 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002346 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002347 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002348 // Intel syntax
2349 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2350 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002351 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2352 delete Operands[2];
2353 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002354 }
2355 } else {
2356 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2357 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002358 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2359 delete Operands[1];
2360 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002361 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002362 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002363 }
Chad Rosier51afe632012-06-27 22:34:28 +00002364
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002365 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2366 // instalias with an immediate operand yet.
2367 if (Name == "int" && Operands.size() == 2) {
2368 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2369 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2370 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2371 delete Operands[1];
2372 Operands.erase(Operands.begin() + 1);
2373 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2374 }
2375 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002376
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002377 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002378}
2379
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002380static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2381 bool isCmp) {
2382 MCInst TmpInst;
2383 TmpInst.setOpcode(Opcode);
2384 if (!isCmp)
2385 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2386 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2387 TmpInst.addOperand(Inst.getOperand(0));
2388 Inst = TmpInst;
2389 return true;
2390}
2391
2392static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2393 bool isCmp = false) {
2394 if (!Inst.getOperand(0).isImm() ||
2395 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2396 return false;
2397
2398 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2399}
2400
2401static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2402 bool isCmp = false) {
2403 if (!Inst.getOperand(0).isImm() ||
2404 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2405 return false;
2406
2407 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2408}
2409
2410static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2411 bool isCmp = false) {
2412 if (!Inst.getOperand(0).isImm() ||
2413 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2414 return false;
2415
2416 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2417}
2418
Devang Patel4a6e7782012-01-12 18:03:40 +00002419bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002420processInstruction(MCInst &Inst,
2421 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2422 switch (Inst.getOpcode()) {
2423 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002424 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2425 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2426 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2427 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2428 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2429 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2430 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2431 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2432 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2433 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2434 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2435 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2436 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2437 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2438 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2439 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2440 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2441 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002442 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2443 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2444 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2445 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2446 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2447 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002448 case X86::VMOVAPDrr:
2449 case X86::VMOVAPDYrr:
2450 case X86::VMOVAPSrr:
2451 case X86::VMOVAPSYrr:
2452 case X86::VMOVDQArr:
2453 case X86::VMOVDQAYrr:
2454 case X86::VMOVDQUrr:
2455 case X86::VMOVDQUYrr:
2456 case X86::VMOVUPDrr:
2457 case X86::VMOVUPDYrr:
2458 case X86::VMOVUPSrr:
2459 case X86::VMOVUPSYrr: {
2460 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2461 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2462 return false;
2463
2464 unsigned NewOpc;
2465 switch (Inst.getOpcode()) {
2466 default: llvm_unreachable("Invalid opcode");
2467 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2468 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2469 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2470 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2471 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2472 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2473 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2474 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2475 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2476 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2477 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2478 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2479 }
2480 Inst.setOpcode(NewOpc);
2481 return true;
2482 }
2483 case X86::VMOVSDrr:
2484 case X86::VMOVSSrr: {
2485 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2486 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2487 return false;
2488 unsigned NewOpc;
2489 switch (Inst.getOpcode()) {
2490 default: llvm_unreachable("Invalid opcode");
2491 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2492 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2493 }
2494 Inst.setOpcode(NewOpc);
2495 return true;
2496 }
Devang Patelde47cce2012-01-18 22:42:29 +00002497 }
Devang Patelde47cce2012-01-18 22:42:29 +00002498}
2499
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002500static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002501bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002502MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002503 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002504 MCStreamer &Out, unsigned &ErrorInfo,
2505 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002506 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002507 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2508 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002509 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002510
Chris Lattnera63292a2010-09-29 01:50:45 +00002511 // First, handle aliases that expand to multiple instructions.
2512 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002513 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002514 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002515 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002516 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002517 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002518 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002519 MCInst Inst;
2520 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002521 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002522 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002523 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002524
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002525 const char *Repl =
2526 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002527 .Case("finit", "fninit")
2528 .Case("fsave", "fnsave")
2529 .Case("fstcw", "fnstcw")
2530 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002531 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002532 .Case("fstsw", "fnstsw")
2533 .Case("fstsww", "fnstsw")
2534 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002535 .Default(0);
2536 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002537 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002538 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002539 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002540
Chris Lattner628fbec2010-09-06 21:54:15 +00002541 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002542 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002543
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002544 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002545 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002546 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002547 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002548 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002549 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002550 // Some instructions need post-processing to, for example, tweak which
2551 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002552 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002553 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002554 while (processInstruction(Inst, Operands))
2555 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002556
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002557 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002558 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002559 Out.EmitInstruction(Inst);
2560 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002561 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002562 case Match_MissingFeature: {
2563 assert(ErrorInfo && "Unknown missing feature!");
2564 // Special case the error message for the very common case where only
2565 // a single subtarget feature is missing.
2566 std::string Msg = "instruction requires:";
2567 unsigned Mask = 1;
2568 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2569 if (ErrorInfo & Mask) {
2570 Msg += " ";
2571 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2572 }
2573 Mask <<= 1;
2574 }
2575 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2576 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002577 case Match_InvalidOperand:
2578 WasOriginallyInvalidOperand = true;
2579 break;
2580 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002581 break;
2582 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002583
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002584 // FIXME: Ideally, we would only attempt suffix matches for things which are
2585 // valid prefixes, and we could just infer the right unambiguous
2586 // type. However, that requires substantially more matcher support than the
2587 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002588
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002589 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002590 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002591 SmallString<16> Tmp;
2592 Tmp += Base;
2593 Tmp += ' ';
2594 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002595
Chris Lattnerfab94132010-11-06 18:28:02 +00002596 // If this instruction starts with an 'f', then it is a floating point stack
2597 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2598 // 80-bit floating point, which use the suffixes s,l,t respectively.
2599 //
2600 // Otherwise, we assume that this may be an integer instruction, which comes
2601 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2602 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002603
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002604 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002605 Tmp[Base.size()] = Suffixes[0];
2606 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002607 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002608 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002609
Chad Rosier2f480a82012-10-12 22:53:36 +00002610 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002611 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002612 // If this returned as a missing feature failure, remember that.
2613 if (Match1 == Match_MissingFeature)
2614 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002615 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002616 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002617 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002618 // If this returned as a missing feature failure, remember that.
2619 if (Match2 == Match_MissingFeature)
2620 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002621 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002622 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002623 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002624 // If this returned as a missing feature failure, remember that.
2625 if (Match3 == Match_MissingFeature)
2626 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002627 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002628 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002629 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002630 // If this returned as a missing feature failure, remember that.
2631 if (Match4 == Match_MissingFeature)
2632 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002633
2634 // Restore the old token.
2635 Op->setTokenValue(Base);
2636
2637 // If exactly one matched, then we treat that as a successful match (and the
2638 // instruction will already have been filled in correctly, since the failing
2639 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002640 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002641 (Match1 == Match_Success) + (Match2 == Match_Success) +
2642 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002643 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002644 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002645 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002646 Out.EmitInstruction(Inst);
2647 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002648 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002649 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002650
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002651 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002652
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002653 // If we had multiple suffix matches, then identify this as an ambiguous
2654 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002655 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002656 char MatchChars[4];
2657 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002658 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2659 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2660 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2661 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002662
2663 SmallString<126> Msg;
2664 raw_svector_ostream OS(Msg);
2665 OS << "ambiguous instructions require an explicit suffix (could be ";
2666 for (unsigned i = 0; i != NumMatches; ++i) {
2667 if (i != 0)
2668 OS << ", ";
2669 if (i + 1 == NumMatches)
2670 OS << "or ";
2671 OS << "'" << Base << MatchChars[i] << "'";
2672 }
2673 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002674 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002675 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002676 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002677
Chris Lattner628fbec2010-09-06 21:54:15 +00002678 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002679
Chris Lattner628fbec2010-09-06 21:54:15 +00002680 // If all of the instructions reported an invalid mnemonic, then the original
2681 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002682 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2683 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002684 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002685 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002686 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002687 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002688 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002689 }
2690
2691 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002692 if (ErrorInfo != ~0U) {
2693 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002694 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002695 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002696
Chad Rosier49963552012-10-13 00:26:04 +00002697 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002698 if (Operand->getStartLoc().isValid()) {
2699 SMRange OperandRange = Operand->getLocRange();
2700 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002701 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002702 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002703 }
2704
Chad Rosier3d4bc622012-08-21 19:36:59 +00002705 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002706 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002707 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002708
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002709 // If one instruction matched with a missing feature, report this as a
2710 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002711 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2712 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002713 std::string Msg = "instruction requires:";
2714 unsigned Mask = 1;
2715 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2716 if (ErrorInfoMissingFeature & Mask) {
2717 Msg += " ";
2718 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2719 }
2720 Mask <<= 1;
2721 }
2722 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002723 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002724
Chris Lattner628fbec2010-09-06 21:54:15 +00002725 // If one instruction matched with an invalid operand, report this as an
2726 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002727 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2728 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002729 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002730 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002731 return true;
2732 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002733
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002734 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002735 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002736 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002737 return true;
2738}
2739
2740
Devang Patel4a6e7782012-01-12 18:03:40 +00002741bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002742 StringRef IDVal = DirectiveID.getIdentifier();
2743 if (IDVal == ".word")
2744 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002745 else if (IDVal.startswith(".code"))
2746 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002747 else if (IDVal.startswith(".att_syntax")) {
2748 getParser().setAssemblerDialect(0);
2749 return false;
2750 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002751 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002752 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002753 // FIXME: Handle noprefix
2754 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002755 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002756 }
2757 return false;
2758 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002759 return true;
2760}
2761
2762/// ParseDirectiveWord
2763/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002764bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002765 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2766 for (;;) {
2767 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002768 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002769 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002770
Eric Christopherbf7bc492013-01-09 03:52:05 +00002771 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002772
Chris Lattner72c0b592010-10-30 17:38:55 +00002773 if (getLexer().is(AsmToken::EndOfStatement))
2774 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002775
Chris Lattner72c0b592010-10-30 17:38:55 +00002776 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002777 if (getLexer().isNot(AsmToken::Comma)) {
2778 Error(L, "unexpected token in directive");
2779 return false;
2780 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002781 Parser.Lex();
2782 }
2783 }
Chad Rosier51afe632012-06-27 22:34:28 +00002784
Chris Lattner72c0b592010-10-30 17:38:55 +00002785 Parser.Lex();
2786 return false;
2787}
2788
Evan Cheng481ebb02011-07-27 00:38:12 +00002789/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002790/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002791bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Craig Topper3c80d622014-01-06 04:55:54 +00002792 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002793 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002794 if (!is16BitMode()) {
2795 SwitchMode(X86::Mode16Bit);
2796 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2797 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002798 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002799 Parser.Lex();
2800 if (!is32BitMode()) {
2801 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002802 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2803 }
2804 } else if (IDVal == ".code64") {
2805 Parser.Lex();
2806 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002807 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002808 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2809 }
2810 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002811 Error(L, "unknown directive " + IDVal);
2812 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002813 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002814
Evan Cheng481ebb02011-07-27 00:38:12 +00002815 return false;
2816}
Chris Lattner72c0b592010-10-30 17:38:55 +00002817
Daniel Dunbar71475772009-07-17 20:42:00 +00002818// Force static initialization.
2819extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002820 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2821 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002822}
Daniel Dunbar00331992009-07-29 00:02:19 +00002823
Chris Lattner3e4582a2010-09-06 19:11:01 +00002824#define GET_REGISTER_MATCHER
2825#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002826#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002827#include "X86GenAsmMatcher.inc"