blob: 59d78461f495726e6d535520241f832b0d833e81 [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"
Evgeniy Stepanove3804d42014-02-28 12:28:07 +000011#include "X86AsmParserCommon.h"
12#include "X86Operand.h"
Chad Rosier6844ea02012-10-24 22:13:37 +000013#include "llvm/ADT/APFloat.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000014#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000019#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCParser/MCAsmLexer.h"
23#include "llvm/MC/MCParser/MCAsmParser.h"
24#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
25#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000030#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000031#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000032#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000033
Daniel Dunbar71475772009-07-17 20:42:00 +000034using namespace llvm;
35
36namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000037
Chad Rosier5362af92013-04-16 18:15:40 +000038static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000039 0, // IC_OR
40 1, // IC_AND
Kevin Enderbyd6b10712014-02-06 01:21:15 +000041 2, // IC_LSHIFT
42 2, // IC_RSHIFT
43 3, // IC_PLUS
44 3, // IC_MINUS
45 4, // IC_MULTIPLY
46 4, // IC_DIVIDE
47 5, // IC_RPAREN
48 6, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000049 0, // IC_IMM
50 0 // IC_REGISTER
51};
52
Devang Patel4a6e7782012-01-12 18:03:40 +000053class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000054 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000055 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000056 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000057private:
Alp Tokera5b88a52013-12-02 16:06:06 +000058 SMLoc consumeToken() {
59 SMLoc Result = Parser.getTok().getLoc();
60 Parser.Lex();
61 return Result;
62 }
63
Chad Rosier5362af92013-04-16 18:15:40 +000064 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000065 IC_OR = 0,
66 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +000067 IC_LSHIFT,
68 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000069 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000070 IC_MINUS,
71 IC_MULTIPLY,
72 IC_DIVIDE,
73 IC_RPAREN,
74 IC_LPAREN,
75 IC_IMM,
76 IC_REGISTER
77 };
78
79 class InfixCalculator {
80 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
81 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
82 SmallVector<ICToken, 4> PostfixStack;
83
84 public:
85 int64_t popOperand() {
86 assert (!PostfixStack.empty() && "Poped an empty stack!");
87 ICToken Op = PostfixStack.pop_back_val();
88 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
89 && "Expected and immediate or register!");
90 return Op.second;
91 }
92 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
93 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
94 "Unexpected operand!");
95 PostfixStack.push_back(std::make_pair(Op, Val));
96 }
97
Jakub Staszak9c349222013-08-08 15:48:46 +000098 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +000099 void pushOperator(InfixCalculatorTok Op) {
100 // Push the new operator if the stack is empty.
101 if (InfixOperatorStack.empty()) {
102 InfixOperatorStack.push_back(Op);
103 return;
104 }
105
106 // Push the new operator if it has a higher precedence than the operator
107 // on the top of the stack or the operator on the top of the stack is a
108 // left parentheses.
109 unsigned Idx = InfixOperatorStack.size() - 1;
110 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
111 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
112 InfixOperatorStack.push_back(Op);
113 return;
114 }
115
116 // The operator on the top of the stack has higher precedence than the
117 // new operator.
118 unsigned ParenCount = 0;
119 while (1) {
120 // Nothing to process.
121 if (InfixOperatorStack.empty())
122 break;
123
124 Idx = InfixOperatorStack.size() - 1;
125 StackOp = InfixOperatorStack[Idx];
126 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
127 break;
128
129 // If we have an even parentheses count and we see a left parentheses,
130 // then stop processing.
131 if (!ParenCount && StackOp == IC_LPAREN)
132 break;
133
134 if (StackOp == IC_RPAREN) {
135 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000136 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000137 } else if (StackOp == IC_LPAREN) {
138 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000139 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000140 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000141 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000142 PostfixStack.push_back(std::make_pair(StackOp, 0));
143 }
144 }
145 // Push the new operator.
146 InfixOperatorStack.push_back(Op);
147 }
148 int64_t execute() {
149 // Push any remaining operators onto the postfix stack.
150 while (!InfixOperatorStack.empty()) {
151 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
152 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
153 PostfixStack.push_back(std::make_pair(StackOp, 0));
154 }
155
156 if (PostfixStack.empty())
157 return 0;
158
159 SmallVector<ICToken, 16> OperandStack;
160 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
161 ICToken Op = PostfixStack[i];
162 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
163 OperandStack.push_back(Op);
164 } else {
165 assert (OperandStack.size() > 1 && "Too few operands.");
166 int64_t Val;
167 ICToken Op2 = OperandStack.pop_back_val();
168 ICToken Op1 = OperandStack.pop_back_val();
169 switch (Op.first) {
170 default:
171 report_fatal_error("Unexpected operator!");
172 break;
173 case IC_PLUS:
174 Val = Op1.second + Op2.second;
175 OperandStack.push_back(std::make_pair(IC_IMM, Val));
176 break;
177 case IC_MINUS:
178 Val = Op1.second - Op2.second;
179 OperandStack.push_back(std::make_pair(IC_IMM, Val));
180 break;
181 case IC_MULTIPLY:
182 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
183 "Multiply operation with an immediate and a register!");
184 Val = Op1.second * Op2.second;
185 OperandStack.push_back(std::make_pair(IC_IMM, Val));
186 break;
187 case IC_DIVIDE:
188 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
189 "Divide operation with an immediate and a register!");
190 assert (Op2.second != 0 && "Division by zero!");
191 Val = Op1.second / Op2.second;
192 OperandStack.push_back(std::make_pair(IC_IMM, Val));
193 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000194 case IC_OR:
195 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
196 "Or operation with an immediate and a register!");
197 Val = Op1.second | Op2.second;
198 OperandStack.push_back(std::make_pair(IC_IMM, Val));
199 break;
200 case IC_AND:
201 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
202 "And operation with an immediate and a register!");
203 Val = Op1.second & Op2.second;
204 OperandStack.push_back(std::make_pair(IC_IMM, Val));
205 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000206 case IC_LSHIFT:
207 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
208 "Left shift operation with an immediate and a register!");
209 Val = Op1.second << Op2.second;
210 OperandStack.push_back(std::make_pair(IC_IMM, Val));
211 break;
212 case IC_RSHIFT:
213 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
214 "Right shift operation with an immediate and a register!");
215 Val = Op1.second >> Op2.second;
216 OperandStack.push_back(std::make_pair(IC_IMM, Val));
217 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000218 }
219 }
220 }
221 assert (OperandStack.size() == 1 && "Expected a single result.");
222 return OperandStack.pop_back_val().second;
223 }
224 };
225
226 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000227 IES_OR,
228 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000229 IES_LSHIFT,
230 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000231 IES_PLUS,
232 IES_MINUS,
233 IES_MULTIPLY,
234 IES_DIVIDE,
235 IES_LBRAC,
236 IES_RBRAC,
237 IES_LPAREN,
238 IES_RPAREN,
239 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000240 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000241 IES_IDENTIFIER,
242 IES_ERROR
243 };
244
245 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000246 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000247 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000248 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000249 const MCExpr *Sym;
250 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000251 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000252 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000253 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000254 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000255 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000256 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
257 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000258 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000259
260 unsigned getBaseReg() { return BaseReg; }
261 unsigned getIndexReg() { return IndexReg; }
262 unsigned getScale() { return Scale; }
263 const MCExpr *getSym() { return Sym; }
264 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000265 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000266 bool isValidEndState() {
267 return State == IES_RBRAC || State == IES_INTEGER;
268 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000269 bool getStopOnLBrac() { return StopOnLBrac; }
270 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000271 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000272
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000273 InlineAsmIdentifierInfo &getIdentifierInfo() {
274 return Info;
275 }
276
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000277 void onOr() {
278 IntelExprState CurrState = State;
279 switch (State) {
280 default:
281 State = IES_ERROR;
282 break;
283 case IES_INTEGER:
284 case IES_RPAREN:
285 case IES_REGISTER:
286 State = IES_OR;
287 IC.pushOperator(IC_OR);
288 break;
289 }
290 PrevState = CurrState;
291 }
292 void onAnd() {
293 IntelExprState CurrState = State;
294 switch (State) {
295 default:
296 State = IES_ERROR;
297 break;
298 case IES_INTEGER:
299 case IES_RPAREN:
300 case IES_REGISTER:
301 State = IES_AND;
302 IC.pushOperator(IC_AND);
303 break;
304 }
305 PrevState = CurrState;
306 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000307 void onLShift() {
308 IntelExprState CurrState = State;
309 switch (State) {
310 default:
311 State = IES_ERROR;
312 break;
313 case IES_INTEGER:
314 case IES_RPAREN:
315 case IES_REGISTER:
316 State = IES_LSHIFT;
317 IC.pushOperator(IC_LSHIFT);
318 break;
319 }
320 PrevState = CurrState;
321 }
322 void onRShift() {
323 IntelExprState CurrState = State;
324 switch (State) {
325 default:
326 State = IES_ERROR;
327 break;
328 case IES_INTEGER:
329 case IES_RPAREN:
330 case IES_REGISTER:
331 State = IES_RSHIFT;
332 IC.pushOperator(IC_RSHIFT);
333 break;
334 }
335 PrevState = CurrState;
336 }
Chad Rosier5362af92013-04-16 18:15:40 +0000337 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000338 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000339 switch (State) {
340 default:
341 State = IES_ERROR;
342 break;
343 case IES_INTEGER:
344 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000345 case IES_REGISTER:
346 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000347 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000348 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
349 // If we already have a BaseReg, then assume this is the IndexReg with
350 // a scale of 1.
351 if (!BaseReg) {
352 BaseReg = TmpReg;
353 } else {
354 assert (!IndexReg && "BaseReg/IndexReg already set!");
355 IndexReg = TmpReg;
356 Scale = 1;
357 }
358 }
Chad Rosier5362af92013-04-16 18:15:40 +0000359 break;
360 }
Chad Rosier31246272013-04-17 21:01:45 +0000361 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000362 }
363 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000364 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000365 switch (State) {
366 default:
367 State = IES_ERROR;
368 break;
369 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000370 case IES_MULTIPLY:
371 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000372 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000373 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000374 case IES_LBRAC:
375 case IES_RBRAC:
376 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000377 case IES_REGISTER:
378 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000379 // Only push the minus operator if it is not a unary operator.
380 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
381 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
382 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
383 IC.pushOperator(IC_MINUS);
384 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
385 // If we already have a BaseReg, then assume this is the IndexReg with
386 // a scale of 1.
387 if (!BaseReg) {
388 BaseReg = TmpReg;
389 } else {
390 assert (!IndexReg && "BaseReg/IndexReg already set!");
391 IndexReg = TmpReg;
392 Scale = 1;
393 }
Chad Rosier5362af92013-04-16 18:15:40 +0000394 }
Chad Rosier5362af92013-04-16 18:15:40 +0000395 break;
396 }
Chad Rosier31246272013-04-17 21:01:45 +0000397 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000398 }
399 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000400 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000401 switch (State) {
402 default:
403 State = IES_ERROR;
404 break;
405 case IES_PLUS:
406 case IES_LPAREN:
407 State = IES_REGISTER;
408 TmpReg = Reg;
409 IC.pushOperand(IC_REGISTER);
410 break;
Chad Rosier31246272013-04-17 21:01:45 +0000411 case IES_MULTIPLY:
412 // Index Register - Scale * Register
413 if (PrevState == IES_INTEGER) {
414 assert (!IndexReg && "IndexReg already set!");
415 State = IES_REGISTER;
416 IndexReg = Reg;
417 // Get the scale and replace the 'Scale * Register' with '0'.
418 Scale = IC.popOperand();
419 IC.pushOperand(IC_IMM);
420 IC.popOperator();
421 } else {
422 State = IES_ERROR;
423 }
Chad Rosier5362af92013-04-16 18:15:40 +0000424 break;
425 }
Chad Rosier31246272013-04-17 21:01:45 +0000426 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000427 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000428 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000429 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000430 switch (State) {
431 default:
432 State = IES_ERROR;
433 break;
434 case IES_PLUS:
435 case IES_MINUS:
436 State = IES_INTEGER;
437 Sym = SymRef;
438 SymName = SymRefName;
439 IC.pushOperand(IC_IMM);
440 break;
441 }
442 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000443 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000444 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000445 switch (State) {
446 default:
447 State = IES_ERROR;
448 break;
449 case IES_PLUS:
450 case IES_MINUS:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000451 case IES_OR:
452 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000453 case IES_LSHIFT:
454 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000455 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000456 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000457 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000458 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000459 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
460 // Index Register - Register * Scale
461 assert (!IndexReg && "IndexReg already set!");
462 IndexReg = TmpReg;
463 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000464 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
465 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
466 return true;
467 }
Chad Rosier31246272013-04-17 21:01:45 +0000468 // Get the scale and replace the 'Register * Scale' with '0'.
469 IC.popOperator();
470 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000471 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000472 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosier31246272013-04-17 21:01:45 +0000473 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
474 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
475 CurrState == IES_MINUS) {
476 // Unary minus. No need to pop the minus operand because it was never
477 // pushed.
478 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
479 } else {
480 IC.pushOperand(IC_IMM, TmpInt);
481 }
Chad Rosier5362af92013-04-16 18:15:40 +0000482 break;
483 }
Chad Rosier31246272013-04-17 21:01:45 +0000484 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000485 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000486 }
487 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000488 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000489 switch (State) {
490 default:
491 State = IES_ERROR;
492 break;
493 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000494 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000495 case IES_RPAREN:
496 State = IES_MULTIPLY;
497 IC.pushOperator(IC_MULTIPLY);
498 break;
499 }
500 }
501 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000502 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000503 switch (State) {
504 default:
505 State = IES_ERROR;
506 break;
507 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000508 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000509 State = IES_DIVIDE;
510 IC.pushOperator(IC_DIVIDE);
511 break;
512 }
513 }
514 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000515 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000516 switch (State) {
517 default:
518 State = IES_ERROR;
519 break;
520 case IES_RBRAC:
521 State = IES_PLUS;
522 IC.pushOperator(IC_PLUS);
523 break;
524 }
525 }
526 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000527 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000528 switch (State) {
529 default:
530 State = IES_ERROR;
531 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000532 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000533 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000534 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000535 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000536 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
537 // If we already have a BaseReg, then assume this is the IndexReg with
538 // a scale of 1.
539 if (!BaseReg) {
540 BaseReg = TmpReg;
541 } else {
542 assert (!IndexReg && "BaseReg/IndexReg already set!");
543 IndexReg = TmpReg;
544 Scale = 1;
545 }
Chad Rosier5362af92013-04-16 18:15:40 +0000546 }
547 break;
548 }
Chad Rosier31246272013-04-17 21:01:45 +0000549 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000550 }
551 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000552 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000553 switch (State) {
554 default:
555 State = IES_ERROR;
556 break;
557 case IES_PLUS:
558 case IES_MINUS:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000559 case IES_OR:
560 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000561 case IES_LSHIFT:
562 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000563 case IES_MULTIPLY:
564 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000565 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000566 // FIXME: We don't handle this type of unary minus, yet.
567 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000568 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000569 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosierdb003992013-04-18 16:28:19 +0000570 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
571 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
572 CurrState == IES_MINUS) {
573 State = IES_ERROR;
574 break;
575 }
Chad Rosier5362af92013-04-16 18:15:40 +0000576 State = IES_LPAREN;
577 IC.pushOperator(IC_LPAREN);
578 break;
579 }
Chad Rosier31246272013-04-17 21:01:45 +0000580 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000581 }
582 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000583 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000584 switch (State) {
585 default:
586 State = IES_ERROR;
587 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000588 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000589 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000590 case IES_RPAREN:
591 State = IES_RPAREN;
592 IC.pushOperator(IC_RPAREN);
593 break;
594 }
595 }
596 };
597
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000598 MCAsmParser &getParser() const { return Parser; }
599
600 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
601
Chris Lattnera3a06812011-10-16 04:47:35 +0000602 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000603 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000604 bool MatchingInlineAsm = false) {
605 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000606 return Parser.Error(L, Msg, Ranges);
607 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000608
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000609 bool ErrorAndEatStatement(SMLoc L, const Twine &Msg,
610 ArrayRef<SMRange> Ranges = None,
611 bool MatchingInlineAsm = false) {
612 Parser.eatToEndOfStatement();
613 return Error(L, Msg, Ranges, MatchingInlineAsm);
614 }
615
Devang Patel41b9dde2012-01-17 18:00:18 +0000616 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
617 Error(Loc, Msg);
618 return 0;
619 }
620
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000621 X86Operand *DefaultMemSIOperand(SMLoc Loc);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000622 X86Operand *DefaultMemDIOperand(SMLoc Loc);
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000623 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000624 X86Operand *ParseATTOperand();
625 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000626 X86Operand *ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000627 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000628 X86Operand *ParseIntelOperator(unsigned OpKind);
David Majnemeraa34d792013-08-27 21:56:17 +0000629 X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
630 X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
631 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000632 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000633 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000634 int64_t ImmDisp, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000635 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
636 InlineAsmIdentifierInfo &Info,
637 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000638
Chris Lattnerb9270732010-04-17 18:56:34 +0000639 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000640
Chad Rosier175d0ae2013-04-12 18:21:18 +0000641 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
642 unsigned BaseReg, unsigned IndexReg,
643 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000644 unsigned Size, StringRef Identifier,
645 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000646
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000647 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000648 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000649
Devang Patelde47cce2012-01-18 22:42:29 +0000650 bool processInstruction(MCInst &Inst,
651 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
652
Chad Rosier49963552012-10-13 00:26:04 +0000653 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000654 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000655 MCStreamer &Out, unsigned &ErrorInfo,
656 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000657
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000658 /// doSrcDstMatch - Returns true if operands are matching in their
659 /// word size (%si and %di, %esi and %edi, etc.). Order depends on
660 /// the parsing mode (Intel vs. AT&T).
661 bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
662
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000663 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
664 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
665 /// \return \c true if no parsing errors occurred, \c false otherwise.
666 bool HandleAVX512Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
667 const MCParsedAsmOperand &Op);
668
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000669 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000670 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000671 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000672 }
Craig Topper3c80d622014-01-06 04:55:54 +0000673 bool is32BitMode() const {
674 // FIXME: Can tablegen auto-generate this?
675 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
676 }
677 bool is16BitMode() const {
678 // FIXME: Can tablegen auto-generate this?
679 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
680 }
681 void SwitchMode(uint64_t mode) {
682 uint64_t oldMode = STI.getFeatureBits() &
683 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
684 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000685 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000686 assert(mode == (STI.getFeatureBits() &
687 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000688 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000689
Chad Rosierc2f055d2013-04-18 16:13:18 +0000690 bool isParsingIntelSyntax() {
691 return getParser().getAssemblerDialect();
692 }
693
Daniel Dunbareefe8612010-07-19 05:44:09 +0000694 /// @name Auto-generated Matcher Functions
695 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000696
Chris Lattner3e4582a2010-09-06 19:11:01 +0000697#define GET_ASSEMBLER_HEADER
698#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000699
Daniel Dunbar00331992009-07-29 00:02:19 +0000700 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000701
702public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000703 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
704 const MCInstrInfo &MII)
705 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000706
Daniel Dunbareefe8612010-07-19 05:44:09 +0000707 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000708 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000709 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000710 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000711
Chad Rosierf0e87202012-10-25 20:41:34 +0000712 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
713 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000714 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000715
716 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000717};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000718} // end anonymous namespace
719
Sean Callanan86c11812010-01-23 00:40:33 +0000720/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000721/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000722
Chris Lattner60db0a62010-02-09 00:34:28 +0000723static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000724
725/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000726
Kevin Enderbybc570f22014-01-23 22:34:42 +0000727static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
728 StringRef &ErrMsg) {
729 // If we have both a base register and an index register make sure they are
730 // both 64-bit or 32-bit registers.
731 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
732 if (BaseReg != 0 && IndexReg != 0) {
733 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
734 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
735 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
736 IndexReg != X86::RIZ) {
737 ErrMsg = "base register is 64-bit, but index register is not";
738 return true;
739 }
740 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
741 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
742 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
743 IndexReg != X86::EIZ){
744 ErrMsg = "base register is 32-bit, but index register is not";
745 return true;
746 }
747 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
748 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
749 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
750 ErrMsg = "base register is 16-bit, but index register is not";
751 return true;
752 }
753 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
754 IndexReg != X86::SI && IndexReg != X86::DI) ||
755 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
756 IndexReg != X86::BX && IndexReg != X86::BP)) {
757 ErrMsg = "invalid 16-bit base/index register combination";
758 return true;
759 }
760 }
761 }
762 return false;
763}
764
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000765bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
766{
767 // Return true and let a normal complaint about bogus operands happen.
768 if (!Op1.isMem() || !Op2.isMem())
769 return true;
770
771 // Actually these might be the other way round if Intel syntax is
772 // being used. It doesn't matter.
773 unsigned diReg = Op1.Mem.BaseReg;
774 unsigned siReg = Op2.Mem.BaseReg;
775
776 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
777 return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
778 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
779 return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
780 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
781 return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
782 // Again, return true and let another error happen.
783 return true;
784}
785
Devang Patel4a6e7782012-01-12 18:03:40 +0000786bool X86AsmParser::ParseRegister(unsigned &RegNo,
787 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000788 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000789 const AsmToken &PercentTok = Parser.getTok();
790 StartLoc = PercentTok.getLoc();
791
792 // If we encounter a %, ignore it. This code handles registers with and
793 // without the prefix, unprefixed registers can occur in cfi directives.
794 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000795 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000796
Sean Callanan936b0d32010-01-19 21:44:56 +0000797 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000798 EndLoc = Tok.getEndLoc();
799
Devang Patelce6a2ca2012-01-20 22:32:05 +0000800 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000801 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000802 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000803 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000804 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000805
Kevin Enderby7d912182009-09-03 17:15:07 +0000806 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000807
Chris Lattner1261b812010-09-22 04:11:10 +0000808 // If the match failed, try the register name as lowercase.
809 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000810 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000811
Evan Chengeda1d4f2011-07-27 23:22:03 +0000812 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000813 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000814 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
815 // checked.
816 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
817 // REX prefix.
818 if (RegNo == X86::RIZ ||
819 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
820 X86II::isX86_64NonExtLowByteReg(RegNo) ||
821 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000822 return Error(StartLoc, "register %"
823 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000824 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000825 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000826
Chris Lattner1261b812010-09-22 04:11:10 +0000827 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
828 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000829 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000830 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000831
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000832 // Check to see if we have '(4)' after %st.
833 if (getLexer().isNot(AsmToken::LParen))
834 return false;
835 // Lex the paren.
836 getParser().Lex();
837
838 const AsmToken &IntTok = Parser.getTok();
839 if (IntTok.isNot(AsmToken::Integer))
840 return Error(IntTok.getLoc(), "expected stack index");
841 switch (IntTok.getIntVal()) {
842 case 0: RegNo = X86::ST0; break;
843 case 1: RegNo = X86::ST1; break;
844 case 2: RegNo = X86::ST2; break;
845 case 3: RegNo = X86::ST3; break;
846 case 4: RegNo = X86::ST4; break;
847 case 5: RegNo = X86::ST5; break;
848 case 6: RegNo = X86::ST6; break;
849 case 7: RegNo = X86::ST7; break;
850 default: return Error(IntTok.getLoc(), "invalid stack index");
851 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000852
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000853 if (getParser().Lex().isNot(AsmToken::RParen))
854 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000855
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000856 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000857 Parser.Lex(); // Eat ')'
858 return false;
859 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000860
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000861 EndLoc = Parser.getTok().getEndLoc();
862
Chris Lattner80486622010-06-24 07:29:18 +0000863 // If this is "db[0-7]", match it as an alias
864 // for dr[0-7].
865 if (RegNo == 0 && Tok.getString().size() == 3 &&
866 Tok.getString().startswith("db")) {
867 switch (Tok.getString()[2]) {
868 case '0': RegNo = X86::DR0; break;
869 case '1': RegNo = X86::DR1; break;
870 case '2': RegNo = X86::DR2; break;
871 case '3': RegNo = X86::DR3; break;
872 case '4': RegNo = X86::DR4; break;
873 case '5': RegNo = X86::DR5; break;
874 case '6': RegNo = X86::DR6; break;
875 case '7': RegNo = X86::DR7; break;
876 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000877
Chris Lattner80486622010-06-24 07:29:18 +0000878 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000879 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000880 Parser.Lex(); // Eat it.
881 return false;
882 }
883 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000884
Devang Patelce6a2ca2012-01-20 22:32:05 +0000885 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000886 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000887 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000888 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000889 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000890
Sean Callanana83fd7d2010-01-19 20:27:46 +0000891 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000892 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000893}
894
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000895X86Operand *X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
896 unsigned basereg =
897 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
898 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
899 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
900 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
901}
902
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000903X86Operand *X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
904 unsigned basereg =
905 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
906 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
907 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
908 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
909}
910
Devang Patel4a6e7782012-01-12 18:03:40 +0000911X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000912 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000913 return ParseIntelOperand();
914 return ParseATTOperand();
915}
916
Devang Patel41b9dde2012-01-17 18:00:18 +0000917/// getIntelMemOperandSize - Return intel memory operand size.
918static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000919 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000920 .Cases("BYTE", "byte", 8)
921 .Cases("WORD", "word", 16)
922 .Cases("DWORD", "dword", 32)
923 .Cases("QWORD", "qword", 64)
924 .Cases("XWORD", "xword", 80)
925 .Cases("XMMWORD", "xmmword", 128)
926 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +0000927 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +0000928 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +0000929 .Default(0);
930 return Size;
Devang Patel46831de2012-01-12 01:36:43 +0000931}
932
Chad Rosier175d0ae2013-04-12 18:21:18 +0000933X86Operand *
934X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
935 unsigned BaseReg, unsigned IndexReg,
936 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000937 unsigned Size, StringRef Identifier,
938 InlineAsmIdentifierInfo &Info){
Reid Klecknerd84e70e2014-03-04 00:33:17 +0000939 // If this is not a VarDecl then assume it is a FuncDecl or some other label
940 // reference. We need an 'r' constraint here, so we need to create register
941 // operand to ensure proper matching. Just pick a GPR based on the size of
942 // a pointer.
943 if (isa<MCSymbolRefExpr>(Disp) && !Info.IsVarDecl) {
944 unsigned RegNo =
945 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
946 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
947 SMLoc(), Identifier, Info.OpDecl);
948 }
949
950 // We either have a direct symbol reference, or an offset from a symbol. The
951 // parser always puts the symbol on the LHS, so look there for size
952 // calculation purposes.
953 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
954 bool IsSymRef =
955 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
956 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000957 if (!Size) {
958 Size = Info.Type * 8; // Size is in terms of bits in this context.
959 if (Size)
960 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
961 /*Len=*/0, Size));
962 }
Chad Rosier7ca135b2013-03-19 21:11:56 +0000963 }
964
Chad Rosier7ca135b2013-03-19 21:11:56 +0000965 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +0000966 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +0000967 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +0000968 BaseReg = BaseReg ? BaseReg : 1;
969 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +0000970 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000971}
972
Chad Rosierd383db52013-04-12 20:20:54 +0000973static void
974RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
975 StringRef SymName, int64_t ImmDisp,
976 int64_t FinalImmDisp, SMLoc &BracLoc,
977 SMLoc &StartInBrac, SMLoc &End) {
978 // Remove the '[' and ']' from the IR string.
979 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
980 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
981
982 // If ImmDisp is non-zero, then we parsed a displacement before the
983 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
984 // If ImmDisp doesn't match the displacement computed by the state machine
985 // then we have an additional displacement in the bracketed expression.
986 if (ImmDisp != FinalImmDisp) {
987 if (ImmDisp) {
988 // We have an immediate displacement before the bracketed expression.
989 // Adjust this to match the final immediate displacement.
990 bool Found = false;
991 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
992 E = AsmRewrites->end(); I != E; ++I) {
993 if ((*I).Loc.getPointer() > BracLoc.getPointer())
994 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +0000995 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
996 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +0000997 (*I).Kind = AOK_Imm;
998 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
999 (*I).Val = FinalImmDisp;
1000 Found = true;
1001 break;
1002 }
1003 }
1004 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001005 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001006 } else {
1007 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001008 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001009 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001010 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001011 }
1012 }
1013 // Remove all the ImmPrefix rewrites within the brackets.
1014 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1015 E = AsmRewrites->end(); I != E; ++I) {
1016 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1017 continue;
1018 if ((*I).Kind == AOK_ImmPrefix)
1019 (*I).Kind = AOK_Delete;
1020 }
1021 const char *SymLocPtr = SymName.data();
1022 // Skip everything before the symbol.
1023 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1024 assert(Len > 0 && "Expected a non-negative length.");
1025 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1026 }
1027 // Skip everything after the symbol.
1028 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1029 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1030 assert(Len > 0 && "Expected a non-negative length.");
1031 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1032 }
1033}
1034
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001035bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001036 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001037
Chad Rosier5c118fd2013-01-14 22:31:35 +00001038 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001039 while (!Done) {
1040 bool UpdateLocLex = true;
1041
1042 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1043 // identifier. Don't try an parse it as a register.
1044 if (Tok.getString().startswith("."))
1045 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001046
1047 // If we're parsing an immediate expression, we don't expect a '['.
1048 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1049 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001050
1051 switch (getLexer().getKind()) {
1052 default: {
1053 if (SM.isValidEndState()) {
1054 Done = true;
1055 break;
1056 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001057 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001058 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001059 case AsmToken::EndOfStatement: {
1060 Done = true;
1061 break;
1062 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001063 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001064 // This could be a register or a symbolic displacement.
1065 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001066 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001067 SMLoc IdentLoc = Tok.getLoc();
1068 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001069 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001070 SM.onRegister(TmpReg);
1071 UpdateLocLex = false;
1072 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001073 } else {
1074 if (!isParsingInlineAsm()) {
1075 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001076 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001077 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001078 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001079 if (ParseIntelIdentifier(Val, Identifier, Info,
1080 /*Unevaluated=*/false, End))
1081 return true;
Chad Rosier95ce8892013-04-19 18:39:50 +00001082 }
1083 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001084 UpdateLocLex = false;
1085 break;
1086 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001087 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001088 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001089 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001090 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001091 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001092 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1093 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001094 // Look for 'b' or 'f' following an Integer as a directional label
1095 SMLoc Loc = getTok().getLoc();
1096 int64_t IntVal = getTok().getIntVal();
1097 End = consumeToken();
1098 UpdateLocLex = false;
1099 if (getLexer().getKind() == AsmToken::Identifier) {
1100 StringRef IDVal = getTok().getString();
1101 if (IDVal == "f" || IDVal == "b") {
1102 MCSymbol *Sym =
1103 getContext().GetDirectionalLocalSymbol(IntVal,
1104 IDVal == "f" ? 1 : 0);
1105 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1106 const MCExpr *Val =
1107 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1108 if (IDVal == "b" && Sym->isUndefined())
1109 return Error(Loc, "invalid reference to undefined symbol");
1110 StringRef Identifier = Sym->getName();
1111 SM.onIdentifierExpr(Val, Identifier);
1112 End = consumeToken();
1113 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001114 if (SM.onInteger(IntVal, ErrMsg))
1115 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001116 }
1117 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001118 if (SM.onInteger(IntVal, ErrMsg))
1119 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001120 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001121 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001122 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001123 case AsmToken::Plus: SM.onPlus(); break;
1124 case AsmToken::Minus: SM.onMinus(); break;
1125 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001126 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001127 case AsmToken::Pipe: SM.onOr(); break;
1128 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001129 case AsmToken::LessLess:
1130 SM.onLShift(); break;
1131 case AsmToken::GreaterGreater:
1132 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001133 case AsmToken::LBrac: SM.onLBrac(); break;
1134 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001135 case AsmToken::LParen: SM.onLParen(); break;
1136 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001137 }
Chad Rosier31246272013-04-17 21:01:45 +00001138 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001139 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001140
Alp Tokera5b88a52013-12-02 16:06:06 +00001141 if (!Done && UpdateLocLex)
1142 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001143 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001144 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001145}
1146
1147X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001148 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001149 unsigned Size) {
1150 const AsmToken &Tok = Parser.getTok();
1151 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1152 if (getLexer().isNot(AsmToken::LBrac))
1153 return ErrorOperand(BracLoc, "Expected '[' token!");
1154 Parser.Lex(); // Eat '['
1155
1156 SMLoc StartInBrac = Tok.getLoc();
1157 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1158 // may have already parsed an immediate displacement before the bracketed
1159 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001160 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001161 if (ParseIntelExpression(SM, End))
1162 return 0;
Devang Patel41b9dde2012-01-17 18:00:18 +00001163
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001164 const MCExpr *Disp = 0;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001165 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001166 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001167 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001168 if (isParsingInlineAsm())
1169 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001170 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001171 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001172 }
1173
1174 if (SM.getImm() || !Disp) {
1175 const MCExpr *Imm = MCConstantExpr::Create(SM.getImm(), getContext());
1176 if (Disp)
1177 Disp = MCBinaryExpr::CreateAdd(Disp, Imm, getContext());
1178 else
1179 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001180 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001181
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001182 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001183 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001184 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001185 if (ParseIntelDotOperator(Disp, NewDisp))
1186 return 0;
Chad Rosier911c1f32012-10-25 17:37:43 +00001187
Chad Rosier70f47592013-04-10 20:07:47 +00001188 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001189 Parser.Lex(); // Eat the field.
1190 Disp = NewDisp;
1191 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001192
Chad Rosier5c118fd2013-01-14 22:31:35 +00001193 int BaseReg = SM.getBaseReg();
1194 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001195 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001196 if (!isParsingInlineAsm()) {
1197 // handle [-42]
1198 if (!BaseReg && !IndexReg) {
1199 if (!SegReg)
1200 return X86Operand::CreateMem(Disp, Start, End, Size);
1201 else
1202 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1203 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001204 StringRef ErrMsg;
1205 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1206 Error(StartInBrac, ErrMsg);
1207 return 0;
1208 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001209 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1210 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001211 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001212
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001213 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001214 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001215 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001216}
1217
Chad Rosier8a244662013-04-02 20:02:33 +00001218// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001219bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1220 StringRef &Identifier,
1221 InlineAsmIdentifierInfo &Info,
1222 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001223 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1224 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001225
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001226 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001227 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001228
Chad Rosier8a244662013-04-02 20:02:33 +00001229 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001230
1231 // Advance the token stream until the end of the current token is
1232 // after the end of what the frontend claimed.
1233 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1234 while (true) {
1235 End = Tok.getEndLoc();
1236 getLexer().Lex();
1237
1238 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1239 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001240 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001241
1242 // Create the symbol reference.
1243 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001244 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1245 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001246 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001247 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001248}
1249
David Majnemeraa34d792013-08-27 21:56:17 +00001250/// \brief Parse intel style segment override.
1251X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1252 SMLoc Start,
1253 unsigned Size) {
1254 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1255 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1256 if (Tok.isNot(AsmToken::Colon))
1257 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1258 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001259
David Majnemeraa34d792013-08-27 21:56:17 +00001260 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001261 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001262 ImmDisp = Tok.getIntVal();
1263 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1264
Chad Rosier1530ba52013-03-27 21:49:56 +00001265 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001266 InstInfo->AsmRewrites->push_back(
1267 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1268
1269 if (getLexer().isNot(AsmToken::LBrac)) {
1270 // An immediate following a 'segment register', 'colon' token sequence can
1271 // be followed by a bracketed expression. If it isn't we know we have our
1272 // final segment override.
1273 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1274 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1275 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1276 Size);
1277 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001278 }
1279
Chad Rosier91c82662012-10-24 17:22:29 +00001280 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001281 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001282
David Majnemeraa34d792013-08-27 21:56:17 +00001283 const MCExpr *Val;
1284 SMLoc End;
1285 if (!isParsingInlineAsm()) {
1286 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001287 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001288
1289 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001290 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001291
David Majnemeraa34d792013-08-27 21:56:17 +00001292 InlineAsmIdentifierInfo Info;
1293 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001294 if (ParseIntelIdentifier(Val, Identifier, Info,
1295 /*Unevaluated=*/false, End))
1296 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001297 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1298 /*Scale=*/1, Start, End, Size, Identifier, Info);
1299}
1300
1301/// ParseIntelMemOperand - Parse intel style memory operand.
1302X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1303 unsigned Size) {
1304 const AsmToken &Tok = Parser.getTok();
1305 SMLoc End;
1306
1307 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1308 if (getLexer().is(AsmToken::LBrac))
1309 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1310
Chad Rosier95ce8892013-04-19 18:39:50 +00001311 const MCExpr *Val;
1312 if (!isParsingInlineAsm()) {
1313 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001314 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001315
1316 return X86Operand::CreateMem(Val, Start, End, Size);
1317 }
1318
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001319 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001320 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001321 if (ParseIntelIdentifier(Val, Identifier, Info,
1322 /*Unevaluated=*/false, End))
1323 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001324 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001325 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001326}
1327
Chad Rosier5dcb4662012-10-24 22:21:50 +00001328/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001329bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001330 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001331 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001332 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001333
1334 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001335 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001336 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001337 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001338 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001339
1340 // Drop the '.'.
1341 StringRef DotDispStr = Tok.getString().drop_front(1);
1342
Chad Rosier5dcb4662012-10-24 22:21:50 +00001343 // .Imm gets lexed as a real.
1344 if (Tok.is(AsmToken::Real)) {
1345 APInt DotDisp;
1346 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001347 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001348 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001349 unsigned DotDisp;
1350 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1351 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001352 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001353 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001354 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001355 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001356 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001357
Chad Rosier240b7b92012-10-25 21:51:10 +00001358 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1359 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1360 unsigned Len = DotDispStr.size();
1361 unsigned Val = OrigDispVal + DotDispVal;
1362 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1363 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001364 }
1365
Chad Rosiercc541e82013-04-19 15:57:00 +00001366 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001367 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001368}
1369
Chad Rosier91c82662012-10-24 17:22:29 +00001370/// Parse the 'offset' operator. This operator is used to specify the
1371/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001372X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001373 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001374 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001375 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001376
Chad Rosier91c82662012-10-24 17:22:29 +00001377 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001378 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001379 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001380 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001381 if (ParseIntelIdentifier(Val, Identifier, Info,
1382 /*Unevaluated=*/false, End))
1383 return 0;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001384
Chad Rosiere2f03772012-10-26 16:09:20 +00001385 // Don't emit the offset operator.
1386 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1387
Chad Rosier91c82662012-10-24 17:22:29 +00001388 // The offset operator will have an 'r' constraint, thus we need to create
1389 // register operand to ensure proper matching. Just pick a GPR based on
1390 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001391 unsigned RegNo =
1392 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001393 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001394 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001395}
1396
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001397enum IntelOperatorKind {
1398 IOK_LENGTH,
1399 IOK_SIZE,
1400 IOK_TYPE
1401};
1402
1403/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1404/// returns the number of elements in an array. It returns the value 1 for
1405/// non-array variables. The SIZE operator returns the size of a C or C++
1406/// variable. A variable's size is the product of its LENGTH and TYPE. The
1407/// TYPE operator returns the size of a C or C++ type or variable. If the
1408/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001409X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001410 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001411 SMLoc TypeLoc = Tok.getLoc();
1412 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001413
Chad Rosier95ce8892013-04-19 18:39:50 +00001414 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001415 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001416 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001417 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001418 if (ParseIntelIdentifier(Val, Identifier, Info,
1419 /*Unevaluated=*/true, End))
1420 return 0;
1421
1422 if (!Info.OpDecl)
1423 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001424
Chad Rosierf6675c32013-04-22 17:01:46 +00001425 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001426 switch(OpKind) {
1427 default: llvm_unreachable("Unexpected operand kind!");
1428 case IOK_LENGTH: CVal = Info.Length; break;
1429 case IOK_SIZE: CVal = Info.Size; break;
1430 case IOK_TYPE: CVal = Info.Type; break;
1431 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001432
1433 // Rewrite the type operator and the C or C++ type or variable in terms of an
1434 // immediate. E.g. TYPE foo -> $$4
1435 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001436 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001437
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001438 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001439 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001440}
1441
Devang Patel41b9dde2012-01-17 18:00:18 +00001442X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001443 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001444 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001445
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001446 // Offset, length, type and size operators.
1447 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001448 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001449 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001450 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001451 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001452 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001453 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001454 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001455 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001456 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001457 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001458
David Majnemeraa34d792013-08-27 21:56:17 +00001459 unsigned Size = getIntelMemOperandSize(Tok.getString());
1460 if (Size) {
1461 Parser.Lex(); // Eat operand size (e.g., byte, word).
1462 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1463 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1464 Parser.Lex(); // Eat ptr.
1465 }
1466 Start = Tok.getLoc();
1467
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001468 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001469 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1470 getLexer().is(AsmToken::LParen)) {
1471 AsmToken StartTok = Tok;
1472 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1473 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001474 if (ParseIntelExpression(SM, End))
1475 return 0;
Chad Rosierbfb70992013-04-17 00:11:46 +00001476
1477 int64_t Imm = SM.getImm();
1478 if (isParsingInlineAsm()) {
1479 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1480 if (StartTok.getString().size() == Len)
1481 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001482 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001483 else
1484 // Otherwise, rewrite the complex expression as a single immediate.
1485 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001486 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001487
1488 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001489 // If a directional label (ie. 1f or 2b) was parsed above from
1490 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1491 // to the MCExpr with the directional local symbol and this is a
1492 // memory operand not an immediate operand.
1493 if (SM.getSym())
1494 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1495
Chad Rosierbfb70992013-04-17 00:11:46 +00001496 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1497 return X86Operand::CreateImm(ImmExpr, Start, End);
1498 }
1499
1500 // Only positive immediates are valid.
1501 if (Imm < 0)
1502 return ErrorOperand(Start, "expected a positive immediate displacement "
1503 "before bracketed expr.");
1504
1505 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001506 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001507 }
1508
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001509 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001510 unsigned RegNo = 0;
1511 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001512 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001513 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001514 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001515 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001516
David Majnemeraa34d792013-08-27 21:56:17 +00001517 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001518 }
1519
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001520 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001521 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001522}
1523
Devang Patel4a6e7782012-01-12 18:03:40 +00001524X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001525 switch (getLexer().getKind()) {
1526 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001527 // Parse a memory operand with no segment register.
1528 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001529 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001530 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001531 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001532 SMLoc Start, End;
1533 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001534 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001535 Error(Start, "%eiz and %riz can only be used as index registers",
1536 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001537 return 0;
1538 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001539
Chris Lattnerb9270732010-04-17 18:56:34 +00001540 // If this is a segment register followed by a ':', then this is the start
1541 // of a memory reference, otherwise this is a normal register reference.
1542 if (getLexer().isNot(AsmToken::Colon))
1543 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001544
Chris Lattnerb9270732010-04-17 18:56:34 +00001545 getParser().Lex(); // Eat the colon.
1546 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001547 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001548 case AsmToken::Dollar: {
1549 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001550 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001551 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001552 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001553 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001554 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001555 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001556 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001557 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001558}
1559
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001560bool
1561X86AsmParser::HandleAVX512Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1562 const MCParsedAsmOperand &Op) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001563 if(STI.getFeatureBits() & X86::FeatureAVX512) {
1564 if (getLexer().is(AsmToken::LCurly)) {
1565 // Eat "{" and mark the current place.
1566 const SMLoc consumedToken = consumeToken();
1567 // Distinguish {1to<NUM>} from {%k<NUM>}.
1568 if(getLexer().is(AsmToken::Integer)) {
1569 // Parse memory broadcasting ({1to<NUM>}).
1570 if (getLexer().getTok().getIntVal() != 1)
1571 return !ErrorAndEatStatement(getLexer().getLoc(),
1572 "Expected 1to<NUM> at this point");
1573 Parser.Lex(); // Eat "1" of 1to8
1574 if (!getLexer().is(AsmToken::Identifier) ||
1575 !getLexer().getTok().getIdentifier().startswith("to"))
1576 return !ErrorAndEatStatement(getLexer().getLoc(),
1577 "Expected 1to<NUM> at this point");
1578 // Recognize only reasonable suffixes.
1579 const char *BroadcastPrimitive =
1580 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
1581 .Case("to8", "{1to8}")
1582 .Case("to16", "{1to16}")
1583 .Default(0);
1584 if (!BroadcastPrimitive)
1585 return !ErrorAndEatStatement(getLexer().getLoc(),
1586 "Invalid memory broadcast primitive.");
1587 Parser.Lex(); // Eat "toN" of 1toN
1588 if (!getLexer().is(AsmToken::RCurly))
1589 return !ErrorAndEatStatement(getLexer().getLoc(),
1590 "Expected } at this point");
1591 Parser.Lex(); // Eat "}"
1592 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1593 consumedToken));
1594 // No AVX512 specific primitives can pass
1595 // after memory broadcasting, so return.
1596 return true;
1597 } else {
1598 // Parse mask register {%k1}
1599 Operands.push_back(X86Operand::CreateToken("{", consumedToken));
1600 if (X86Operand *Op = ParseOperand()) {
1601 Operands.push_back(Op);
1602 if (!getLexer().is(AsmToken::RCurly))
1603 return !ErrorAndEatStatement(getLexer().getLoc(),
1604 "Expected } at this point");
1605 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1606
1607 // Parse "zeroing non-masked" semantic {z}
1608 if (getLexer().is(AsmToken::LCurly)) {
1609 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1610 if (!getLexer().is(AsmToken::Identifier) ||
1611 getLexer().getTok().getIdentifier() != "z")
1612 return !ErrorAndEatStatement(getLexer().getLoc(),
1613 "Expected z at this point");
1614 Parser.Lex(); // Eat the z
1615 if (!getLexer().is(AsmToken::RCurly))
1616 return !ErrorAndEatStatement(getLexer().getLoc(),
1617 "Expected } at this point");
1618 Parser.Lex(); // Eat the }
1619 }
1620 }
1621 }
1622 }
1623 }
1624 return true;
1625}
1626
Chris Lattnerb9270732010-04-17 18:56:34 +00001627/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1628/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001629X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001630
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001631 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1632 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001633 // only way to do this without lookahead is to eat the '(' and see what is
1634 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001635 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001636 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001637 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001638 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001639
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001640 // After parsing the base expression we could either have a parenthesized
1641 // memory address or not. If not, return now. If so, eat the (.
1642 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001643 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001644 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001645 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001646 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001647 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001648
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001649 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001650 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001651 } else {
1652 // Okay, we have a '('. We don't know if this is an expression or not, but
1653 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001654 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001655 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001656
Kevin Enderby7d912182009-09-03 17:15:07 +00001657 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001658 // Nothing to do here, fall into the code below with the '(' part of the
1659 // memory operand consumed.
1660 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001661 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001662
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001663 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001664 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001665 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001666
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001667 // After parsing the base expression we could either have a parenthesized
1668 // memory address or not. If not, return now. If so, eat the (.
1669 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001670 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001671 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001672 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001673 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001674 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001675
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001676 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001677 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001678 }
1679 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001680
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001681 // If we reached here, then we just ate the ( of the memory operand. Process
1682 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001683 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001684 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001685
Chris Lattner0c2538f2010-01-15 18:51:29 +00001686 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001687 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001688 BaseLoc = Parser.getTok().getLoc();
Benjamin Kramer1930b002011-10-16 12:10:27 +00001689 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001690 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001691 Error(StartLoc, "eiz and riz can only be used as index registers",
1692 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001693 return 0;
1694 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001695 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001696
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001697 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001698 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001699 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001700
1701 // Following the comma we should have either an index register, or a scale
1702 // value. We don't support the later form, but we want to parse it
1703 // correctly.
1704 //
1705 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001706 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001707 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001708 SMLoc L;
1709 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001710
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001711 if (getLexer().isNot(AsmToken::RParen)) {
1712 // Parse the scale amount:
1713 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001714 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001715 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001716 "expected comma in scale expression");
1717 return 0;
1718 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001719 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001720
1721 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001722 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001723
1724 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001725 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001726 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001727 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001728 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001729
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001730 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001731 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1732 ScaleVal != 1) {
1733 Error(Loc, "scale factor in 16-bit address must be 1");
1734 return 0;
1735 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001736 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1737 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1738 return 0;
1739 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001740 Scale = (unsigned)ScaleVal;
1741 }
1742 }
1743 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001744 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001745 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001746 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001747
1748 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001749 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001750 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001751
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001752 if (Value != 1)
1753 Warning(Loc, "scale factor without index register is ignored");
1754 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001755 }
1756 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001757
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001758 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001759 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001760 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001761 return 0;
1762 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001763 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001764 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001765
David Woodhouse6dbda442014-01-08 12:58:28 +00001766 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1767 // and then only in non-64-bit modes. Except for DX, which is a special case
1768 // because an unofficial form of in/out instructions uses it.
1769 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1770 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1771 BaseReg != X86::SI && BaseReg != X86::DI)) &&
1772 BaseReg != X86::DX) {
1773 Error(BaseLoc, "invalid 16-bit base register");
1774 return 0;
1775 }
1776 if (BaseReg == 0 &&
1777 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
1778 Error(IndexLoc, "16-bit memory operand may not include only index register");
1779 return 0;
1780 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001781
1782 StringRef ErrMsg;
1783 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1784 Error(BaseLoc, ErrMsg);
1785 return 0;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001786 }
1787
Chris Lattner015cfb12010-01-15 19:33:43 +00001788 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1789 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001790}
1791
Devang Patel4a6e7782012-01-12 18:03:40 +00001792bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001793ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001794 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001795 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001796 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001797
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001798 // FIXME: Hack to recognize setneb as setne.
1799 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1800 PatchedName != "setb" && PatchedName != "setnb")
1801 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001802
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001803 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1804 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001805 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001806 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1807 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001808 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001809 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001810 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001811 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001812 .Case("eq", 0x00)
1813 .Case("lt", 0x01)
1814 .Case("le", 0x02)
1815 .Case("unord", 0x03)
1816 .Case("neq", 0x04)
1817 .Case("nlt", 0x05)
1818 .Case("nle", 0x06)
1819 .Case("ord", 0x07)
1820 /* AVX only from here */
1821 .Case("eq_uq", 0x08)
1822 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001823 .Case("ngt", 0x0A)
1824 .Case("false", 0x0B)
1825 .Case("neq_oq", 0x0C)
1826 .Case("ge", 0x0D)
1827 .Case("gt", 0x0E)
1828 .Case("true", 0x0F)
1829 .Case("eq_os", 0x10)
1830 .Case("lt_oq", 0x11)
1831 .Case("le_oq", 0x12)
1832 .Case("unord_s", 0x13)
1833 .Case("neq_us", 0x14)
1834 .Case("nlt_uq", 0x15)
1835 .Case("nle_uq", 0x16)
1836 .Case("ord_s", 0x17)
1837 .Case("eq_us", 0x18)
1838 .Case("nge_uq", 0x19)
1839 .Case("ngt_uq", 0x1A)
1840 .Case("false_os", 0x1B)
1841 .Case("neq_os", 0x1C)
1842 .Case("ge_oq", 0x1D)
1843 .Case("gt_oq", 0x1E)
1844 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001845 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001846 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001847 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1848 getParser().getContext());
1849 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001850 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001851 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001852 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001853 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001854 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001855 } else {
1856 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001857 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001858 }
1859 }
1860 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001861
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001862 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001863
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001864 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001865 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001866
Chris Lattner086a83a2010-09-08 05:17:37 +00001867 // Determine whether this is an instruction prefix.
1868 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001869 Name == "lock" || Name == "rep" ||
1870 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001871 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001872 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001873
1874
Chris Lattner086a83a2010-09-08 05:17:37 +00001875 // This does the actual operand parsing. Don't parse any more if we have a
1876 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1877 // just want to parse the "lock" as the first instruction and the "incl" as
1878 // the next one.
1879 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001880
1881 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00001882 if (getLexer().is(AsmToken::Star))
1883 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00001884
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001885 // Read the operands.
1886 while(1) {
1887 if (X86Operand *Op = ParseOperand()) {
1888 Operands.push_back(Op);
1889 if (!HandleAVX512Operand(Operands, *Op))
Elena Demikhovsky89529742013-09-12 08:55:00 +00001890 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001891 } else {
1892 Parser.eatToEndOfStatement();
1893 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00001894 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001895 // check for comma and eat it
1896 if (getLexer().is(AsmToken::Comma))
1897 Parser.Lex();
1898 else
1899 break;
1900 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00001901
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001902 if (getLexer().isNot(AsmToken::EndOfStatement))
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001903 return ErrorAndEatStatement(getLexer().getLoc(),
1904 "unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001905 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001906
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001907 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001908 if (getLexer().is(AsmToken::EndOfStatement) ||
1909 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001910 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001911
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001912 if (ExtraImmOp && isParsingIntelSyntax())
1913 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1914
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001915 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1916 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1917 // documented form in various unofficial manuals, so a lot of code uses it.
1918 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1919 Operands.size() == 3) {
1920 X86Operand &Op = *(X86Operand*)Operands.back();
1921 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1922 isa<MCConstantExpr>(Op.Mem.Disp) &&
1923 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1924 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1925 SMLoc Loc = Op.getEndLoc();
1926 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1927 delete &Op;
1928 }
1929 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00001930 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1931 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1932 Operands.size() == 3) {
1933 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1934 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1935 isa<MCConstantExpr>(Op.Mem.Disp) &&
1936 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1937 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1938 SMLoc Loc = Op.getEndLoc();
1939 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1940 delete &Op;
1941 }
1942 }
David Woodhouse4ce66062014-01-22 15:08:55 +00001943
1944 // Append default arguments to "ins[bwld]"
1945 if (Name.startswith("ins") && Operands.size() == 1 &&
1946 (Name == "insb" || Name == "insw" || Name == "insl" ||
1947 Name == "insd" )) {
1948 if (isParsingIntelSyntax()) {
1949 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
1950 Operands.push_back(DefaultMemDIOperand(NameLoc));
1951 } else {
1952 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
1953 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001954 }
1955 }
1956
David Woodhousec472b812014-01-22 15:08:49 +00001957 // Append default arguments to "outs[bwld]"
1958 if (Name.startswith("outs") && Operands.size() == 1 &&
1959 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
1960 Name == "outsd" )) {
1961 if (isParsingIntelSyntax()) {
1962 Operands.push_back(DefaultMemSIOperand(NameLoc));
1963 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
1964 } else {
1965 Operands.push_back(DefaultMemSIOperand(NameLoc));
1966 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001967 }
1968 }
1969
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001970 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
1971 // values of $SIREG according to the mode. It would be nice if this
1972 // could be achieved with InstAlias in the tables.
1973 if (Name.startswith("lods") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001974 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001975 Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
1976 Operands.push_back(DefaultMemSIOperand(NameLoc));
1977
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001978 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
1979 // values of $DIREG according to the mode. It would be nice if this
1980 // could be achieved with InstAlias in the tables.
1981 if (Name.startswith("stos") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001982 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001983 Name == "stosl" || Name == "stosd" || Name == "stosq"))
1984 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001985
David Woodhouse20fe4802014-01-22 15:08:27 +00001986 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
1987 // values of $DIREG according to the mode. It would be nice if this
1988 // could be achieved with InstAlias in the tables.
1989 if (Name.startswith("scas") && Operands.size() == 1 &&
1990 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
1991 Name == "scasl" || Name == "scasd" || Name == "scasq"))
1992 Operands.push_back(DefaultMemDIOperand(NameLoc));
1993
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00001994 // Add default SI and DI operands to "cmps[bwlq]".
1995 if (Name.startswith("cmps") &&
1996 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
1997 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
1998 if (Operands.size() == 1) {
1999 if (isParsingIntelSyntax()) {
2000 Operands.push_back(DefaultMemSIOperand(NameLoc));
2001 Operands.push_back(DefaultMemDIOperand(NameLoc));
2002 } else {
2003 Operands.push_back(DefaultMemDIOperand(NameLoc));
2004 Operands.push_back(DefaultMemSIOperand(NameLoc));
2005 }
2006 } else if (Operands.size() == 3) {
2007 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2008 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2009 if (!doSrcDstMatch(Op, Op2))
2010 return Error(Op.getStartLoc(),
2011 "mismatching source and destination index registers");
2012 }
2013 }
2014
David Woodhouse6f417de2014-01-22 15:08:42 +00002015 // Add default SI and DI operands to "movs[bwlq]".
2016 if ((Name.startswith("movs") &&
2017 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2018 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2019 (Name.startswith("smov") &&
2020 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2021 Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2022 if (Operands.size() == 1) {
2023 if (Name == "movsd")
2024 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2025 if (isParsingIntelSyntax()) {
2026 Operands.push_back(DefaultMemDIOperand(NameLoc));
2027 Operands.push_back(DefaultMemSIOperand(NameLoc));
2028 } else {
2029 Operands.push_back(DefaultMemSIOperand(NameLoc));
2030 Operands.push_back(DefaultMemDIOperand(NameLoc));
2031 }
2032 } else if (Operands.size() == 3) {
2033 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2034 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2035 if (!doSrcDstMatch(Op, Op2))
2036 return Error(Op.getStartLoc(),
2037 "mismatching source and destination index registers");
2038 }
2039 }
2040
Chris Lattner4bd21712010-09-15 04:33:27 +00002041 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002042 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002043 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002044 Name.startswith("shl") || Name.startswith("sal") ||
2045 Name.startswith("rcl") || Name.startswith("rcr") ||
2046 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002047 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002048 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002049 // Intel syntax
2050 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2051 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002052 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2053 delete Operands[2];
2054 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002055 }
2056 } else {
2057 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2058 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002059 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2060 delete Operands[1];
2061 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002062 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002063 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002064 }
Chad Rosier51afe632012-06-27 22:34:28 +00002065
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002066 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2067 // instalias with an immediate operand yet.
2068 if (Name == "int" && Operands.size() == 2) {
2069 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2070 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2071 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2072 delete Operands[1];
2073 Operands.erase(Operands.begin() + 1);
2074 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2075 }
2076 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002077
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002078 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002079}
2080
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002081static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2082 bool isCmp) {
2083 MCInst TmpInst;
2084 TmpInst.setOpcode(Opcode);
2085 if (!isCmp)
2086 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2087 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2088 TmpInst.addOperand(Inst.getOperand(0));
2089 Inst = TmpInst;
2090 return true;
2091}
2092
2093static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2094 bool isCmp = false) {
2095 if (!Inst.getOperand(0).isImm() ||
2096 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2097 return false;
2098
2099 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2100}
2101
2102static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2103 bool isCmp = false) {
2104 if (!Inst.getOperand(0).isImm() ||
2105 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2106 return false;
2107
2108 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2109}
2110
2111static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2112 bool isCmp = false) {
2113 if (!Inst.getOperand(0).isImm() ||
2114 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2115 return false;
2116
2117 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2118}
2119
Devang Patel4a6e7782012-01-12 18:03:40 +00002120bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002121processInstruction(MCInst &Inst,
2122 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2123 switch (Inst.getOpcode()) {
2124 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002125 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2126 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2127 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2128 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2129 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2130 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2131 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2132 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2133 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2134 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2135 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2136 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2137 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2138 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2139 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2140 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2141 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2142 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002143 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2144 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2145 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2146 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2147 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2148 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002149 case X86::VMOVAPDrr:
2150 case X86::VMOVAPDYrr:
2151 case X86::VMOVAPSrr:
2152 case X86::VMOVAPSYrr:
2153 case X86::VMOVDQArr:
2154 case X86::VMOVDQAYrr:
2155 case X86::VMOVDQUrr:
2156 case X86::VMOVDQUYrr:
2157 case X86::VMOVUPDrr:
2158 case X86::VMOVUPDYrr:
2159 case X86::VMOVUPSrr:
2160 case X86::VMOVUPSYrr: {
2161 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2162 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2163 return false;
2164
2165 unsigned NewOpc;
2166 switch (Inst.getOpcode()) {
2167 default: llvm_unreachable("Invalid opcode");
2168 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2169 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2170 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2171 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2172 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2173 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2174 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2175 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2176 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2177 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2178 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2179 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2180 }
2181 Inst.setOpcode(NewOpc);
2182 return true;
2183 }
2184 case X86::VMOVSDrr:
2185 case X86::VMOVSSrr: {
2186 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2187 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2188 return false;
2189 unsigned NewOpc;
2190 switch (Inst.getOpcode()) {
2191 default: llvm_unreachable("Invalid opcode");
2192 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2193 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2194 }
2195 Inst.setOpcode(NewOpc);
2196 return true;
2197 }
Devang Patelde47cce2012-01-18 22:42:29 +00002198 }
Devang Patelde47cce2012-01-18 22:42:29 +00002199}
2200
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002201static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002202bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002203MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002204 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002205 MCStreamer &Out, unsigned &ErrorInfo,
2206 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002207 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002208 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2209 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002210 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002211
Chris Lattnera63292a2010-09-29 01:50:45 +00002212 // First, handle aliases that expand to multiple instructions.
2213 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002214 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002215 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002216 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002217 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002218 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002219 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002220 MCInst Inst;
2221 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002222 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002223 if (!MatchingInlineAsm)
David Woodhousee6c13e42014-01-28 23:12:42 +00002224 Out.EmitInstruction(Inst, STI);
Chris Lattnera63292a2010-09-29 01:50:45 +00002225
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002226 const char *Repl =
2227 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002228 .Case("finit", "fninit")
2229 .Case("fsave", "fnsave")
2230 .Case("fstcw", "fnstcw")
2231 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002232 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002233 .Case("fstsw", "fnstsw")
2234 .Case("fstsww", "fnstsw")
2235 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002236 .Default(0);
2237 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002238 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002239 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002240 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002241
Chris Lattner628fbec2010-09-06 21:54:15 +00002242 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002243 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002244
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002245 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002246 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002247 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002248 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002249 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002250 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002251 // Some instructions need post-processing to, for example, tweak which
2252 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002253 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002254 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002255 while (processInstruction(Inst, Operands))
2256 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002257
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002258 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002259 if (!MatchingInlineAsm)
David Woodhousee6c13e42014-01-28 23:12:42 +00002260 Out.EmitInstruction(Inst, STI);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002261 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002262 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002263 case Match_MissingFeature: {
2264 assert(ErrorInfo && "Unknown missing feature!");
2265 // Special case the error message for the very common case where only
2266 // a single subtarget feature is missing.
2267 std::string Msg = "instruction requires:";
2268 unsigned Mask = 1;
2269 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2270 if (ErrorInfo & Mask) {
2271 Msg += " ";
2272 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2273 }
2274 Mask <<= 1;
2275 }
2276 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2277 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002278 case Match_InvalidOperand:
2279 WasOriginallyInvalidOperand = true;
2280 break;
2281 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002282 break;
2283 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002284
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002285 // FIXME: Ideally, we would only attempt suffix matches for things which are
2286 // valid prefixes, and we could just infer the right unambiguous
2287 // type. However, that requires substantially more matcher support than the
2288 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002289
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002290 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002291 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002292 SmallString<16> Tmp;
2293 Tmp += Base;
2294 Tmp += ' ';
2295 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002296
Chris Lattnerfab94132010-11-06 18:28:02 +00002297 // If this instruction starts with an 'f', then it is a floating point stack
2298 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2299 // 80-bit floating point, which use the suffixes s,l,t respectively.
2300 //
2301 // Otherwise, we assume that this may be an integer instruction, which comes
2302 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2303 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002304
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002305 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002306 Tmp[Base.size()] = Suffixes[0];
2307 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002308 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002309 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002310
Chad Rosier2f480a82012-10-12 22:53:36 +00002311 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002312 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002313 // If this returned as a missing feature failure, remember that.
2314 if (Match1 == Match_MissingFeature)
2315 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002316 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002317 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002318 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002319 // If this returned as a missing feature failure, remember that.
2320 if (Match2 == Match_MissingFeature)
2321 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002322 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002323 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002324 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002325 // If this returned as a missing feature failure, remember that.
2326 if (Match3 == Match_MissingFeature)
2327 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002328 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002329 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002330 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002331 // If this returned as a missing feature failure, remember that.
2332 if (Match4 == Match_MissingFeature)
2333 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002334
2335 // Restore the old token.
2336 Op->setTokenValue(Base);
2337
2338 // If exactly one matched, then we treat that as a successful match (and the
2339 // instruction will already have been filled in correctly, since the failing
2340 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002341 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002342 (Match1 == Match_Success) + (Match2 == Match_Success) +
2343 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002344 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002345 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002346 if (!MatchingInlineAsm)
David Woodhousee6c13e42014-01-28 23:12:42 +00002347 Out.EmitInstruction(Inst, STI);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002348 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002349 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002350 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002351
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002352 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002353
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002354 // If we had multiple suffix matches, then identify this as an ambiguous
2355 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002356 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002357 char MatchChars[4];
2358 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002359 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2360 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2361 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2362 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002363
2364 SmallString<126> Msg;
2365 raw_svector_ostream OS(Msg);
2366 OS << "ambiguous instructions require an explicit suffix (could be ";
2367 for (unsigned i = 0; i != NumMatches; ++i) {
2368 if (i != 0)
2369 OS << ", ";
2370 if (i + 1 == NumMatches)
2371 OS << "or ";
2372 OS << "'" << Base << MatchChars[i] << "'";
2373 }
2374 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002375 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002376 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002377 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002378
Chris Lattner628fbec2010-09-06 21:54:15 +00002379 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002380
Chris Lattner628fbec2010-09-06 21:54:15 +00002381 // If all of the instructions reported an invalid mnemonic, then the original
2382 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002383 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2384 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002385 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002386 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002387 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002388 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002389 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002390 }
2391
2392 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002393 if (ErrorInfo != ~0U) {
2394 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002395 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002396 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002397
Chad Rosier49963552012-10-13 00:26:04 +00002398 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002399 if (Operand->getStartLoc().isValid()) {
2400 SMRange OperandRange = Operand->getLocRange();
2401 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002402 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002403 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002404 }
2405
Chad Rosier3d4bc622012-08-21 19:36:59 +00002406 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002407 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002408 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002409
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002410 // If one instruction matched with a missing feature, report this as a
2411 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002412 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2413 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002414 std::string Msg = "instruction requires:";
2415 unsigned Mask = 1;
2416 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2417 if (ErrorInfoMissingFeature & Mask) {
2418 Msg += " ";
2419 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2420 }
2421 Mask <<= 1;
2422 }
2423 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002424 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002425
Chris Lattner628fbec2010-09-06 21:54:15 +00002426 // If one instruction matched with an invalid operand, report this as an
2427 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002428 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2429 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002430 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002431 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002432 return true;
2433 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002434
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002435 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002436 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002437 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002438 return true;
2439}
2440
2441
Devang Patel4a6e7782012-01-12 18:03:40 +00002442bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002443 StringRef IDVal = DirectiveID.getIdentifier();
2444 if (IDVal == ".word")
2445 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002446 else if (IDVal.startswith(".code"))
2447 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002448 else if (IDVal.startswith(".att_syntax")) {
2449 getParser().setAssemblerDialect(0);
2450 return false;
2451 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002452 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002453 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002454 // FIXME: Handle noprefix
2455 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002456 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002457 }
2458 return false;
2459 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002460 return true;
2461}
2462
2463/// ParseDirectiveWord
2464/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002465bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2467 for (;;) {
2468 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002469 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002470 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002471
Eric Christopherbf7bc492013-01-09 03:52:05 +00002472 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002473
Chris Lattner72c0b592010-10-30 17:38:55 +00002474 if (getLexer().is(AsmToken::EndOfStatement))
2475 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002476
Chris Lattner72c0b592010-10-30 17:38:55 +00002477 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002478 if (getLexer().isNot(AsmToken::Comma)) {
2479 Error(L, "unexpected token in directive");
2480 return false;
2481 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002482 Parser.Lex();
2483 }
2484 }
Chad Rosier51afe632012-06-27 22:34:28 +00002485
Chris Lattner72c0b592010-10-30 17:38:55 +00002486 Parser.Lex();
2487 return false;
2488}
2489
Evan Cheng481ebb02011-07-27 00:38:12 +00002490/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002491/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002492bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Craig Topper3c80d622014-01-06 04:55:54 +00002493 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002494 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002495 if (!is16BitMode()) {
2496 SwitchMode(X86::Mode16Bit);
2497 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2498 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002499 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002500 Parser.Lex();
2501 if (!is32BitMode()) {
2502 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002503 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2504 }
2505 } else if (IDVal == ".code64") {
2506 Parser.Lex();
2507 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002508 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002509 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2510 }
2511 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002512 Error(L, "unknown directive " + IDVal);
2513 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002514 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002515
Evan Cheng481ebb02011-07-27 00:38:12 +00002516 return false;
2517}
Chris Lattner72c0b592010-10-30 17:38:55 +00002518
Daniel Dunbar71475772009-07-17 20:42:00 +00002519// Force static initialization.
2520extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002521 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2522 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002523}
Daniel Dunbar00331992009-07-29 00:02:19 +00002524
Chris Lattner3e4582a2010-09-06 19:11:01 +00002525#define GET_REGISTER_MATCHER
2526#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002527#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002528#include "X86GenAsmMatcher.inc"