blob: a259c96320322ebf79806f044824f349a753cc71 [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 Stepanov49e26252014-03-14 08:58:04 +000011#include "X86AsmInstrumentation.h"
Evgeniy Stepanove3804d42014-02-28 12:28:07 +000012#include "X86AsmParserCommon.h"
13#include "X86Operand.h"
Chad Rosier6844ea02012-10-24 22:13:37 +000014#include "llvm/ADT/APFloat.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000015#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000018#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000020#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000023#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCParser/MCAsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSubtargetInfo.h"
30#include "llvm/MC/MCSymbol.h"
31#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000032#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000034#include "llvm/Support/raw_ostream.h"
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000035#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000036
Daniel Dunbar71475772009-07-17 20:42:00 +000037using namespace llvm;
38
39namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000040
Chad Rosier5362af92013-04-16 18:15:40 +000041static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000042 0, // IC_OR
43 1, // IC_AND
Kevin Enderbyd6b10712014-02-06 01:21:15 +000044 2, // IC_LSHIFT
45 2, // IC_RSHIFT
46 3, // IC_PLUS
47 3, // IC_MINUS
48 4, // IC_MULTIPLY
49 4, // IC_DIVIDE
50 5, // IC_RPAREN
51 6, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000052 0, // IC_IMM
53 0 // IC_REGISTER
54};
55
Devang Patel4a6e7782012-01-12 18:03:40 +000056class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000057 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000058 MCAsmParser &Parser;
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000059 const MCInstrInfo &MII;
Chad Rosierf0e87202012-10-25 20:41:34 +000060 ParseInstructionInfo *InstInfo;
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000061 std::unique_ptr<X86AsmInstrumentation> Instrumentation;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000062private:
Alp Tokera5b88a52013-12-02 16:06:06 +000063 SMLoc consumeToken() {
64 SMLoc Result = Parser.getTok().getLoc();
65 Parser.Lex();
66 return Result;
67 }
68
Chad Rosier5362af92013-04-16 18:15:40 +000069 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000070 IC_OR = 0,
71 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +000072 IC_LSHIFT,
73 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000074 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000075 IC_MINUS,
76 IC_MULTIPLY,
77 IC_DIVIDE,
78 IC_RPAREN,
79 IC_LPAREN,
80 IC_IMM,
81 IC_REGISTER
82 };
83
84 class InfixCalculator {
85 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
86 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
87 SmallVector<ICToken, 4> PostfixStack;
88
89 public:
90 int64_t popOperand() {
91 assert (!PostfixStack.empty() && "Poped an empty stack!");
92 ICToken Op = PostfixStack.pop_back_val();
93 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
94 && "Expected and immediate or register!");
95 return Op.second;
96 }
97 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
98 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
99 "Unexpected operand!");
100 PostfixStack.push_back(std::make_pair(Op, Val));
101 }
102
Jakub Staszak9c349222013-08-08 15:48:46 +0000103 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000104 void pushOperator(InfixCalculatorTok Op) {
105 // Push the new operator if the stack is empty.
106 if (InfixOperatorStack.empty()) {
107 InfixOperatorStack.push_back(Op);
108 return;
109 }
110
111 // Push the new operator if it has a higher precedence than the operator
112 // on the top of the stack or the operator on the top of the stack is a
113 // left parentheses.
114 unsigned Idx = InfixOperatorStack.size() - 1;
115 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
116 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
117 InfixOperatorStack.push_back(Op);
118 return;
119 }
120
121 // The operator on the top of the stack has higher precedence than the
122 // new operator.
123 unsigned ParenCount = 0;
124 while (1) {
125 // Nothing to process.
126 if (InfixOperatorStack.empty())
127 break;
128
129 Idx = InfixOperatorStack.size() - 1;
130 StackOp = InfixOperatorStack[Idx];
131 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
132 break;
133
134 // If we have an even parentheses count and we see a left parentheses,
135 // then stop processing.
136 if (!ParenCount && StackOp == IC_LPAREN)
137 break;
138
139 if (StackOp == IC_RPAREN) {
140 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000141 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000142 } else if (StackOp == IC_LPAREN) {
143 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000144 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000145 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000146 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000147 PostfixStack.push_back(std::make_pair(StackOp, 0));
148 }
149 }
150 // Push the new operator.
151 InfixOperatorStack.push_back(Op);
152 }
153 int64_t execute() {
154 // Push any remaining operators onto the postfix stack.
155 while (!InfixOperatorStack.empty()) {
156 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
157 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
158 PostfixStack.push_back(std::make_pair(StackOp, 0));
159 }
160
161 if (PostfixStack.empty())
162 return 0;
163
164 SmallVector<ICToken, 16> OperandStack;
165 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
166 ICToken Op = PostfixStack[i];
167 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
168 OperandStack.push_back(Op);
169 } else {
170 assert (OperandStack.size() > 1 && "Too few operands.");
171 int64_t Val;
172 ICToken Op2 = OperandStack.pop_back_val();
173 ICToken Op1 = OperandStack.pop_back_val();
174 switch (Op.first) {
175 default:
176 report_fatal_error("Unexpected operator!");
177 break;
178 case IC_PLUS:
179 Val = Op1.second + Op2.second;
180 OperandStack.push_back(std::make_pair(IC_IMM, Val));
181 break;
182 case IC_MINUS:
183 Val = Op1.second - Op2.second;
184 OperandStack.push_back(std::make_pair(IC_IMM, Val));
185 break;
186 case IC_MULTIPLY:
187 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
188 "Multiply operation with an immediate and a register!");
189 Val = Op1.second * Op2.second;
190 OperandStack.push_back(std::make_pair(IC_IMM, Val));
191 break;
192 case IC_DIVIDE:
193 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
194 "Divide operation with an immediate and a register!");
195 assert (Op2.second != 0 && "Division by zero!");
196 Val = Op1.second / Op2.second;
197 OperandStack.push_back(std::make_pair(IC_IMM, Val));
198 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000199 case IC_OR:
200 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
201 "Or operation with an immediate and a register!");
202 Val = Op1.second | Op2.second;
203 OperandStack.push_back(std::make_pair(IC_IMM, Val));
204 break;
205 case IC_AND:
206 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
207 "And operation with an immediate and a register!");
208 Val = Op1.second & Op2.second;
209 OperandStack.push_back(std::make_pair(IC_IMM, Val));
210 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000211 case IC_LSHIFT:
212 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
213 "Left shift operation with an immediate and a register!");
214 Val = Op1.second << Op2.second;
215 OperandStack.push_back(std::make_pair(IC_IMM, Val));
216 break;
217 case IC_RSHIFT:
218 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
219 "Right shift operation with an immediate and a register!");
220 Val = Op1.second >> Op2.second;
221 OperandStack.push_back(std::make_pair(IC_IMM, Val));
222 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000223 }
224 }
225 }
226 assert (OperandStack.size() == 1 && "Expected a single result.");
227 return OperandStack.pop_back_val().second;
228 }
229 };
230
231 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000232 IES_OR,
233 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000234 IES_LSHIFT,
235 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000236 IES_PLUS,
237 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000238 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000239 IES_MULTIPLY,
240 IES_DIVIDE,
241 IES_LBRAC,
242 IES_RBRAC,
243 IES_LPAREN,
244 IES_RPAREN,
245 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000246 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000247 IES_IDENTIFIER,
248 IES_ERROR
249 };
250
251 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000252 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000253 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000254 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000255 const MCExpr *Sym;
256 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000257 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000258 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000259 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000260 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000261 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000262 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
Craig Topper062a2ba2014-04-25 05:30:21 +0000263 Scale(1), Imm(imm), Sym(nullptr), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000264 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000265
266 unsigned getBaseReg() { return BaseReg; }
267 unsigned getIndexReg() { return IndexReg; }
268 unsigned getScale() { return Scale; }
269 const MCExpr *getSym() { return Sym; }
270 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000271 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000272 bool isValidEndState() {
273 return State == IES_RBRAC || State == IES_INTEGER;
274 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000275 bool getStopOnLBrac() { return StopOnLBrac; }
276 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000277 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000278
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000279 InlineAsmIdentifierInfo &getIdentifierInfo() {
280 return Info;
281 }
282
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000283 void onOr() {
284 IntelExprState CurrState = State;
285 switch (State) {
286 default:
287 State = IES_ERROR;
288 break;
289 case IES_INTEGER:
290 case IES_RPAREN:
291 case IES_REGISTER:
292 State = IES_OR;
293 IC.pushOperator(IC_OR);
294 break;
295 }
296 PrevState = CurrState;
297 }
298 void onAnd() {
299 IntelExprState CurrState = State;
300 switch (State) {
301 default:
302 State = IES_ERROR;
303 break;
304 case IES_INTEGER:
305 case IES_RPAREN:
306 case IES_REGISTER:
307 State = IES_AND;
308 IC.pushOperator(IC_AND);
309 break;
310 }
311 PrevState = CurrState;
312 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000313 void onLShift() {
314 IntelExprState CurrState = State;
315 switch (State) {
316 default:
317 State = IES_ERROR;
318 break;
319 case IES_INTEGER:
320 case IES_RPAREN:
321 case IES_REGISTER:
322 State = IES_LSHIFT;
323 IC.pushOperator(IC_LSHIFT);
324 break;
325 }
326 PrevState = CurrState;
327 }
328 void onRShift() {
329 IntelExprState CurrState = State;
330 switch (State) {
331 default:
332 State = IES_ERROR;
333 break;
334 case IES_INTEGER:
335 case IES_RPAREN:
336 case IES_REGISTER:
337 State = IES_RSHIFT;
338 IC.pushOperator(IC_RSHIFT);
339 break;
340 }
341 PrevState = CurrState;
342 }
Chad Rosier5362af92013-04-16 18:15:40 +0000343 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000344 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000345 switch (State) {
346 default:
347 State = IES_ERROR;
348 break;
349 case IES_INTEGER:
350 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000351 case IES_REGISTER:
352 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000353 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000354 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
355 // If we already have a BaseReg, then assume this is the IndexReg with
356 // a scale of 1.
357 if (!BaseReg) {
358 BaseReg = TmpReg;
359 } else {
360 assert (!IndexReg && "BaseReg/IndexReg already set!");
361 IndexReg = TmpReg;
362 Scale = 1;
363 }
364 }
Chad Rosier5362af92013-04-16 18:15:40 +0000365 break;
366 }
Chad Rosier31246272013-04-17 21:01:45 +0000367 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000368 }
369 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000370 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000371 switch (State) {
372 default:
373 State = IES_ERROR;
374 break;
375 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000376 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000377 case IES_MULTIPLY:
378 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000379 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000380 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000381 case IES_LBRAC:
382 case IES_RBRAC:
383 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000384 case IES_REGISTER:
385 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000386 // Only push the minus operator if it is not a unary operator.
387 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
388 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
389 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
390 IC.pushOperator(IC_MINUS);
391 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
392 // If we already have a BaseReg, then assume this is the IndexReg with
393 // a scale of 1.
394 if (!BaseReg) {
395 BaseReg = TmpReg;
396 } else {
397 assert (!IndexReg && "BaseReg/IndexReg already set!");
398 IndexReg = TmpReg;
399 Scale = 1;
400 }
Chad Rosier5362af92013-04-16 18:15:40 +0000401 }
Chad Rosier5362af92013-04-16 18:15:40 +0000402 break;
403 }
Chad Rosier31246272013-04-17 21:01:45 +0000404 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000405 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000406 void onNot() {
407 IntelExprState CurrState = State;
408 switch (State) {
409 default:
410 State = IES_ERROR;
411 break;
412 case IES_PLUS:
413 case IES_NOT:
414 State = IES_NOT;
415 break;
416 }
417 PrevState = CurrState;
418 }
Chad Rosier5362af92013-04-16 18:15:40 +0000419 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000420 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000421 switch (State) {
422 default:
423 State = IES_ERROR;
424 break;
425 case IES_PLUS:
426 case IES_LPAREN:
427 State = IES_REGISTER;
428 TmpReg = Reg;
429 IC.pushOperand(IC_REGISTER);
430 break;
Chad Rosier31246272013-04-17 21:01:45 +0000431 case IES_MULTIPLY:
432 // Index Register - Scale * Register
433 if (PrevState == IES_INTEGER) {
434 assert (!IndexReg && "IndexReg already set!");
435 State = IES_REGISTER;
436 IndexReg = Reg;
437 // Get the scale and replace the 'Scale * Register' with '0'.
438 Scale = IC.popOperand();
439 IC.pushOperand(IC_IMM);
440 IC.popOperator();
441 } else {
442 State = IES_ERROR;
443 }
Chad Rosier5362af92013-04-16 18:15:40 +0000444 break;
445 }
Chad Rosier31246272013-04-17 21:01:45 +0000446 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000447 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000448 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000449 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000450 switch (State) {
451 default:
452 State = IES_ERROR;
453 break;
454 case IES_PLUS:
455 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000456 case IES_NOT:
Chad Rosier5362af92013-04-16 18:15:40 +0000457 State = IES_INTEGER;
458 Sym = SymRef;
459 SymName = SymRefName;
460 IC.pushOperand(IC_IMM);
461 break;
462 }
463 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000464 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000465 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000466 switch (State) {
467 default:
468 State = IES_ERROR;
469 break;
470 case IES_PLUS:
471 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000472 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000473 case IES_OR:
474 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000475 case IES_LSHIFT:
476 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000477 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000478 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000479 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000480 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000481 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
482 // Index Register - Register * Scale
483 assert (!IndexReg && "IndexReg already set!");
484 IndexReg = TmpReg;
485 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000486 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
487 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
488 return true;
489 }
Chad Rosier31246272013-04-17 21:01:45 +0000490 // Get the scale and replace the 'Register * Scale' with '0'.
491 IC.popOperator();
492 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000493 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000494 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosier31246272013-04-17 21:01:45 +0000495 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000496 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
497 PrevState == IES_NOT) &&
Chad Rosier31246272013-04-17 21:01:45 +0000498 CurrState == IES_MINUS) {
499 // Unary minus. No need to pop the minus operand because it was never
500 // pushed.
501 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000502 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
503 PrevState == IES_OR || PrevState == IES_AND ||
504 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
505 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
506 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
507 PrevState == IES_NOT) &&
508 CurrState == IES_NOT) {
509 // Unary not. No need to pop the not operand because it was never
510 // pushed.
511 IC.pushOperand(IC_IMM, ~TmpInt); // Push ~Imm.
Chad Rosier31246272013-04-17 21:01:45 +0000512 } else {
513 IC.pushOperand(IC_IMM, TmpInt);
514 }
Chad Rosier5362af92013-04-16 18:15:40 +0000515 break;
516 }
Chad Rosier31246272013-04-17 21:01:45 +0000517 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000518 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000519 }
520 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000521 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000522 switch (State) {
523 default:
524 State = IES_ERROR;
525 break;
526 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000527 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000528 case IES_RPAREN:
529 State = IES_MULTIPLY;
530 IC.pushOperator(IC_MULTIPLY);
531 break;
532 }
533 }
534 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000535 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000536 switch (State) {
537 default:
538 State = IES_ERROR;
539 break;
540 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000541 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000542 State = IES_DIVIDE;
543 IC.pushOperator(IC_DIVIDE);
544 break;
545 }
546 }
547 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000548 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000549 switch (State) {
550 default:
551 State = IES_ERROR;
552 break;
553 case IES_RBRAC:
554 State = IES_PLUS;
555 IC.pushOperator(IC_PLUS);
556 break;
557 }
558 }
559 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000560 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000561 switch (State) {
562 default:
563 State = IES_ERROR;
564 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000565 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000566 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000567 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000568 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000569 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
570 // If we already have a BaseReg, then assume this is the IndexReg with
571 // a scale of 1.
572 if (!BaseReg) {
573 BaseReg = TmpReg;
574 } else {
575 assert (!IndexReg && "BaseReg/IndexReg already set!");
576 IndexReg = TmpReg;
577 Scale = 1;
578 }
Chad Rosier5362af92013-04-16 18:15:40 +0000579 }
580 break;
581 }
Chad Rosier31246272013-04-17 21:01:45 +0000582 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000583 }
584 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000585 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000586 switch (State) {
587 default:
588 State = IES_ERROR;
589 break;
590 case IES_PLUS:
591 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000592 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000593 case IES_OR:
594 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000595 case IES_LSHIFT:
596 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000597 case IES_MULTIPLY:
598 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000599 case IES_LPAREN:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000600 // FIXME: We don't handle this type of unary minus or not, yet.
Chad Rosierdb003992013-04-18 16:28:19 +0000601 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000602 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000603 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosierdb003992013-04-18 16:28:19 +0000604 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000605 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
606 PrevState == IES_NOT) &&
607 (CurrState == IES_MINUS || CurrState == IES_NOT)) {
Chad Rosierdb003992013-04-18 16:28:19 +0000608 State = IES_ERROR;
609 break;
610 }
Chad Rosier5362af92013-04-16 18:15:40 +0000611 State = IES_LPAREN;
612 IC.pushOperator(IC_LPAREN);
613 break;
614 }
Chad Rosier31246272013-04-17 21:01:45 +0000615 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000616 }
617 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000618 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000619 switch (State) {
620 default:
621 State = IES_ERROR;
622 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000623 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000624 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000625 case IES_RPAREN:
626 State = IES_RPAREN;
627 IC.pushOperator(IC_RPAREN);
628 break;
629 }
630 }
631 };
632
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000633 MCAsmParser &getParser() const { return Parser; }
634
635 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
636
Chris Lattnera3a06812011-10-16 04:47:35 +0000637 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000638 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000639 bool MatchingInlineAsm = false) {
640 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000641 return Parser.Error(L, Msg, Ranges);
642 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000643
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000644 bool ErrorAndEatStatement(SMLoc L, const Twine &Msg,
645 ArrayRef<SMRange> Ranges = None,
646 bool MatchingInlineAsm = false) {
647 Parser.eatToEndOfStatement();
648 return Error(L, Msg, Ranges, MatchingInlineAsm);
649 }
650
David Blaikie960ea3f2014-06-08 16:18:35 +0000651 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000652 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000653 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000654 }
655
David Blaikie960ea3f2014-06-08 16:18:35 +0000656 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
657 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
658 std::unique_ptr<X86Operand> ParseOperand();
659 std::unique_ptr<X86Operand> ParseATTOperand();
660 std::unique_ptr<X86Operand> ParseIntelOperand();
661 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000662 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
David Blaikie960ea3f2014-06-08 16:18:35 +0000663 std::unique_ptr<X86Operand> ParseIntelOperator(unsigned OpKind);
664 std::unique_ptr<X86Operand>
665 ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
666 std::unique_ptr<X86Operand>
667 ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000668 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
David Blaikie960ea3f2014-06-08 16:18:35 +0000669 std::unique_ptr<X86Operand> ParseIntelBracExpression(unsigned SegReg,
670 SMLoc Start,
671 int64_t ImmDisp,
672 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000673 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
674 InlineAsmIdentifierInfo &Info,
675 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000676
David Blaikie960ea3f2014-06-08 16:18:35 +0000677 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000678
David Blaikie960ea3f2014-06-08 16:18:35 +0000679 std::unique_ptr<X86Operand>
680 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
681 unsigned IndexReg, unsigned Scale, SMLoc Start,
682 SMLoc End, unsigned Size, StringRef Identifier,
683 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000684
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000685 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000686 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000687
David Blaikie960ea3f2014-06-08 16:18:35 +0000688 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000689
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000690 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
691 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000692 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000693
Chad Rosier49963552012-10-13 00:26:04 +0000694 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000695 OperandVector &Operands, MCStreamer &Out,
696 unsigned &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000697 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000698
Nico Weber42f79db2014-07-17 20:24:55 +0000699 virtual bool OmitRegisterFromClobberLists(unsigned RegNo) override;
700
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000701 /// doSrcDstMatch - Returns true if operands are matching in their
702 /// word size (%si and %di, %esi and %edi, etc.). Order depends on
703 /// the parsing mode (Intel vs. AT&T).
704 bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
705
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000706 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
707 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
708 /// \return \c true if no parsing errors occurred, \c false otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000709 bool HandleAVX512Operand(OperandVector &Operands,
710 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000711
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000712 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000713 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000714 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000715 }
Craig Topper3c80d622014-01-06 04:55:54 +0000716 bool is32BitMode() const {
717 // FIXME: Can tablegen auto-generate this?
718 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
719 }
720 bool is16BitMode() const {
721 // FIXME: Can tablegen auto-generate this?
722 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
723 }
724 void SwitchMode(uint64_t mode) {
725 uint64_t oldMode = STI.getFeatureBits() &
726 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
727 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000728 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000729 assert(mode == (STI.getFeatureBits() &
730 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000731 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000732
Chad Rosierc2f055d2013-04-18 16:13:18 +0000733 bool isParsingIntelSyntax() {
734 return getParser().getAssemblerDialect();
735 }
736
Daniel Dunbareefe8612010-07-19 05:44:09 +0000737 /// @name Auto-generated Matcher Functions
738 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000739
Chris Lattner3e4582a2010-09-06 19:11:01 +0000740#define GET_ASSEMBLER_HEADER
741#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000742
Daniel Dunbar00331992009-07-29 00:02:19 +0000743 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000744
745public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000746 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +0000747 const MCInstrInfo &mii,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000748 const MCTargetOptions &Options)
Craig Topper062a2ba2014-04-25 05:30:21 +0000749 : MCTargetAsmParser(), STI(sti), Parser(parser), MII(mii),
750 InstInfo(nullptr) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000751
Daniel Dunbareefe8612010-07-19 05:44:09 +0000752 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000753 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000754 Instrumentation.reset(
755 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000756 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000757
Craig Topper39012cc2014-03-09 18:03:14 +0000758 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000759
David Blaikie960ea3f2014-06-08 16:18:35 +0000760 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
761 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000762
Craig Topper39012cc2014-03-09 18:03:14 +0000763 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000764};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000765} // end anonymous namespace
766
Sean Callanan86c11812010-01-23 00:40:33 +0000767/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000768/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000769
Chris Lattner60db0a62010-02-09 00:34:28 +0000770static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000771
772/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000773
Kevin Enderbybc570f22014-01-23 22:34:42 +0000774static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
775 StringRef &ErrMsg) {
776 // If we have both a base register and an index register make sure they are
777 // both 64-bit or 32-bit registers.
778 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
779 if (BaseReg != 0 && IndexReg != 0) {
780 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
781 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
782 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
783 IndexReg != X86::RIZ) {
784 ErrMsg = "base register is 64-bit, but index register is not";
785 return true;
786 }
787 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
788 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
789 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
790 IndexReg != X86::EIZ){
791 ErrMsg = "base register is 32-bit, but index register is not";
792 return true;
793 }
794 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
795 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
796 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
797 ErrMsg = "base register is 16-bit, but index register is not";
798 return true;
799 }
800 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
801 IndexReg != X86::SI && IndexReg != X86::DI) ||
802 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
803 IndexReg != X86::BX && IndexReg != X86::BP)) {
804 ErrMsg = "invalid 16-bit base/index register combination";
805 return true;
806 }
807 }
808 }
809 return false;
810}
811
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000812bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
813{
814 // Return true and let a normal complaint about bogus operands happen.
815 if (!Op1.isMem() || !Op2.isMem())
816 return true;
817
818 // Actually these might be the other way round if Intel syntax is
819 // being used. It doesn't matter.
820 unsigned diReg = Op1.Mem.BaseReg;
821 unsigned siReg = Op2.Mem.BaseReg;
822
823 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
824 return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
825 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
826 return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
827 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
828 return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
829 // Again, return true and let another error happen.
830 return true;
831}
832
Devang Patel4a6e7782012-01-12 18:03:40 +0000833bool X86AsmParser::ParseRegister(unsigned &RegNo,
834 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000835 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000836 const AsmToken &PercentTok = Parser.getTok();
837 StartLoc = PercentTok.getLoc();
838
839 // If we encounter a %, ignore it. This code handles registers with and
840 // without the prefix, unprefixed registers can occur in cfi directives.
841 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000842 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000843
Sean Callanan936b0d32010-01-19 21:44:56 +0000844 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000845 EndLoc = Tok.getEndLoc();
846
Devang Patelce6a2ca2012-01-20 22:32:05 +0000847 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000848 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000849 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000850 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000851 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000852
Kevin Enderby7d912182009-09-03 17:15:07 +0000853 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000854
Chris Lattner1261b812010-09-22 04:11:10 +0000855 // If the match failed, try the register name as lowercase.
856 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000857 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000858
Evan Chengeda1d4f2011-07-27 23:22:03 +0000859 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000860 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000861 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
862 // checked.
863 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
864 // REX prefix.
865 if (RegNo == X86::RIZ ||
866 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
867 X86II::isX86_64NonExtLowByteReg(RegNo) ||
868 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000869 return Error(StartLoc, "register %"
870 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000871 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000872 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000873
Chris Lattner1261b812010-09-22 04:11:10 +0000874 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
875 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000876 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000877 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000878
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000879 // Check to see if we have '(4)' after %st.
880 if (getLexer().isNot(AsmToken::LParen))
881 return false;
882 // Lex the paren.
883 getParser().Lex();
884
885 const AsmToken &IntTok = Parser.getTok();
886 if (IntTok.isNot(AsmToken::Integer))
887 return Error(IntTok.getLoc(), "expected stack index");
888 switch (IntTok.getIntVal()) {
889 case 0: RegNo = X86::ST0; break;
890 case 1: RegNo = X86::ST1; break;
891 case 2: RegNo = X86::ST2; break;
892 case 3: RegNo = X86::ST3; break;
893 case 4: RegNo = X86::ST4; break;
894 case 5: RegNo = X86::ST5; break;
895 case 6: RegNo = X86::ST6; break;
896 case 7: RegNo = X86::ST7; break;
897 default: return Error(IntTok.getLoc(), "invalid stack index");
898 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000899
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000900 if (getParser().Lex().isNot(AsmToken::RParen))
901 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000902
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000904 Parser.Lex(); // Eat ')'
905 return false;
906 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000907
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Parser.getTok().getEndLoc();
909
Chris Lattner80486622010-06-24 07:29:18 +0000910 // If this is "db[0-7]", match it as an alias
911 // for dr[0-7].
912 if (RegNo == 0 && Tok.getString().size() == 3 &&
913 Tok.getString().startswith("db")) {
914 switch (Tok.getString()[2]) {
915 case '0': RegNo = X86::DR0; break;
916 case '1': RegNo = X86::DR1; break;
917 case '2': RegNo = X86::DR2; break;
918 case '3': RegNo = X86::DR3; break;
919 case '4': RegNo = X86::DR4; break;
920 case '5': RegNo = X86::DR5; break;
921 case '6': RegNo = X86::DR6; break;
922 case '7': RegNo = X86::DR7; break;
923 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000924
Chris Lattner80486622010-06-24 07:29:18 +0000925 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000926 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000927 Parser.Lex(); // Eat it.
928 return false;
929 }
930 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000931
Devang Patelce6a2ca2012-01-20 22:32:05 +0000932 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000933 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000934 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000935 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000936 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000937
Sean Callanana83fd7d2010-01-19 20:27:46 +0000938 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000939 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000940}
941
David Blaikie960ea3f2014-06-08 16:18:35 +0000942std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000943 unsigned basereg =
944 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
945 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
946 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
947 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
948}
949
David Blaikie960ea3f2014-06-08 16:18:35 +0000950std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000951 unsigned basereg =
952 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
953 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
954 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
955 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
956}
957
David Blaikie960ea3f2014-06-08 16:18:35 +0000958std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000959 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000960 return ParseIntelOperand();
961 return ParseATTOperand();
962}
963
Devang Patel41b9dde2012-01-17 18:00:18 +0000964/// getIntelMemOperandSize - Return intel memory operand size.
965static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000966 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000967 .Cases("BYTE", "byte", 8)
968 .Cases("WORD", "word", 16)
969 .Cases("DWORD", "dword", 32)
970 .Cases("QWORD", "qword", 64)
971 .Cases("XWORD", "xword", 80)
972 .Cases("XMMWORD", "xmmword", 128)
973 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +0000974 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +0000975 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +0000976 .Default(0);
977 return Size;
Devang Patel46831de2012-01-12 01:36:43 +0000978}
979
David Blaikie960ea3f2014-06-08 16:18:35 +0000980std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
981 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
982 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
983 InlineAsmIdentifierInfo &Info) {
Reid Klecknerd84e70e2014-03-04 00:33:17 +0000984 // If this is not a VarDecl then assume it is a FuncDecl or some other label
985 // reference. We need an 'r' constraint here, so we need to create register
986 // operand to ensure proper matching. Just pick a GPR based on the size of
987 // a pointer.
988 if (isa<MCSymbolRefExpr>(Disp) && !Info.IsVarDecl) {
989 unsigned RegNo =
990 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
991 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
992 SMLoc(), Identifier, Info.OpDecl);
993 }
994
995 // We either have a direct symbol reference, or an offset from a symbol. The
996 // parser always puts the symbol on the LHS, so look there for size
997 // calculation purposes.
998 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
999 bool IsSymRef =
1000 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
1001 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001002 if (!Size) {
1003 Size = Info.Type * 8; // Size is in terms of bits in this context.
1004 if (Size)
1005 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1006 /*Len=*/0, Size));
1007 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001008 }
1009
Chad Rosier7ca135b2013-03-19 21:11:56 +00001010 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001011 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001012 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001013 BaseReg = BaseReg ? BaseReg : 1;
1014 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001015 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001016}
1017
Chad Rosierd383db52013-04-12 20:20:54 +00001018static void
1019RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1020 StringRef SymName, int64_t ImmDisp,
1021 int64_t FinalImmDisp, SMLoc &BracLoc,
1022 SMLoc &StartInBrac, SMLoc &End) {
1023 // Remove the '[' and ']' from the IR string.
1024 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1025 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1026
1027 // If ImmDisp is non-zero, then we parsed a displacement before the
1028 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1029 // If ImmDisp doesn't match the displacement computed by the state machine
1030 // then we have an additional displacement in the bracketed expression.
1031 if (ImmDisp != FinalImmDisp) {
1032 if (ImmDisp) {
1033 // We have an immediate displacement before the bracketed expression.
1034 // Adjust this to match the final immediate displacement.
1035 bool Found = false;
1036 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1037 E = AsmRewrites->end(); I != E; ++I) {
1038 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1039 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001040 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1041 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001042 (*I).Kind = AOK_Imm;
1043 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1044 (*I).Val = FinalImmDisp;
1045 Found = true;
1046 break;
1047 }
1048 }
1049 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001050 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001051 } else {
1052 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001053 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001054 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001055 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001056 }
1057 }
1058 // Remove all the ImmPrefix rewrites within the brackets.
1059 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1060 E = AsmRewrites->end(); I != E; ++I) {
1061 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1062 continue;
1063 if ((*I).Kind == AOK_ImmPrefix)
1064 (*I).Kind = AOK_Delete;
1065 }
1066 const char *SymLocPtr = SymName.data();
1067 // Skip everything before the symbol.
1068 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1069 assert(Len > 0 && "Expected a non-negative length.");
1070 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1071 }
1072 // Skip everything after the symbol.
1073 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1074 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1075 assert(Len > 0 && "Expected a non-negative length.");
1076 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1077 }
1078}
1079
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001080bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001081 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001082
Chad Rosier5c118fd2013-01-14 22:31:35 +00001083 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001084 while (!Done) {
1085 bool UpdateLocLex = true;
1086
1087 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1088 // identifier. Don't try an parse it as a register.
1089 if (Tok.getString().startswith("."))
1090 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001091
1092 // If we're parsing an immediate expression, we don't expect a '['.
1093 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1094 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001095
David Majnemer6a5b8122014-06-19 01:25:43 +00001096 AsmToken::TokenKind TK = getLexer().getKind();
1097 switch (TK) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001098 default: {
1099 if (SM.isValidEndState()) {
1100 Done = true;
1101 break;
1102 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001103 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001104 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001105 case AsmToken::EndOfStatement: {
1106 Done = true;
1107 break;
1108 }
David Majnemer6a5b8122014-06-19 01:25:43 +00001109 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001110 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001111 // This could be a register or a symbolic displacement.
1112 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001113 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001114 SMLoc IdentLoc = Tok.getLoc();
1115 StringRef Identifier = Tok.getString();
David Majnemer6a5b8122014-06-19 01:25:43 +00001116 if (TK != AsmToken::String && !ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001117 SM.onRegister(TmpReg);
1118 UpdateLocLex = false;
1119 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001120 } else {
1121 if (!isParsingInlineAsm()) {
1122 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001123 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001124 } else {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001125 // This is a dot operator, not an adjacent identifier.
1126 if (Identifier.find('.') != StringRef::npos) {
1127 return false;
1128 } else {
1129 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1130 if (ParseIntelIdentifier(Val, Identifier, Info,
1131 /*Unevaluated=*/false, End))
1132 return true;
1133 }
Chad Rosier95ce8892013-04-19 18:39:50 +00001134 }
1135 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001136 UpdateLocLex = false;
1137 break;
1138 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001139 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001140 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001141 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001142 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001143 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001144 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1145 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001146 // Look for 'b' or 'f' following an Integer as a directional label
1147 SMLoc Loc = getTok().getLoc();
1148 int64_t IntVal = getTok().getIntVal();
1149 End = consumeToken();
1150 UpdateLocLex = false;
1151 if (getLexer().getKind() == AsmToken::Identifier) {
1152 StringRef IDVal = getTok().getString();
1153 if (IDVal == "f" || IDVal == "b") {
1154 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +00001155 getContext().GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001156 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1157 const MCExpr *Val =
1158 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1159 if (IDVal == "b" && Sym->isUndefined())
1160 return Error(Loc, "invalid reference to undefined symbol");
1161 StringRef Identifier = Sym->getName();
1162 SM.onIdentifierExpr(Val, Identifier);
1163 End = consumeToken();
1164 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001165 if (SM.onInteger(IntVal, ErrMsg))
1166 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001167 }
1168 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001169 if (SM.onInteger(IntVal, ErrMsg))
1170 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001171 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001172 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001173 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001174 case AsmToken::Plus: SM.onPlus(); break;
1175 case AsmToken::Minus: SM.onMinus(); break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001176 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001177 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001178 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001179 case AsmToken::Pipe: SM.onOr(); break;
1180 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001181 case AsmToken::LessLess:
1182 SM.onLShift(); break;
1183 case AsmToken::GreaterGreater:
1184 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001185 case AsmToken::LBrac: SM.onLBrac(); break;
1186 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001187 case AsmToken::LParen: SM.onLParen(); break;
1188 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001189 }
Chad Rosier31246272013-04-17 21:01:45 +00001190 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001191 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001192
Alp Tokera5b88a52013-12-02 16:06:06 +00001193 if (!Done && UpdateLocLex)
1194 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001195 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001196 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001197}
1198
David Blaikie960ea3f2014-06-08 16:18:35 +00001199std::unique_ptr<X86Operand>
1200X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
1201 int64_t ImmDisp, unsigned Size) {
Chad Rosier5362af92013-04-16 18:15:40 +00001202 const AsmToken &Tok = Parser.getTok();
1203 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1204 if (getLexer().isNot(AsmToken::LBrac))
1205 return ErrorOperand(BracLoc, "Expected '[' token!");
1206 Parser.Lex(); // Eat '['
1207
1208 SMLoc StartInBrac = Tok.getLoc();
1209 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1210 // may have already parsed an immediate displacement before the bracketed
1211 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001212 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001213 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001214 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +00001215
Craig Topper062a2ba2014-04-25 05:30:21 +00001216 const MCExpr *Disp = nullptr;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001217 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001218 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001219 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001220 if (isParsingInlineAsm())
1221 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001222 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001223 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001224 }
1225
1226 if (SM.getImm() || !Disp) {
1227 const MCExpr *Imm = MCConstantExpr::Create(SM.getImm(), getContext());
1228 if (Disp)
1229 Disp = MCBinaryExpr::CreateAdd(Disp, Imm, getContext());
1230 else
1231 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001232 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001233
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001234 // Parse struct field access. Intel requires a dot, but MSVC doesn't. MSVC
1235 // will in fact do global lookup the field name inside all global typedefs,
1236 // but we don't emulate that.
1237 if (Tok.getString().find('.') != StringRef::npos) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001238 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001239 if (ParseIntelDotOperator(Disp, NewDisp))
Craig Topper062a2ba2014-04-25 05:30:21 +00001240 return nullptr;
Chad Rosier911c1f32012-10-25 17:37:43 +00001241
Chad Rosier70f47592013-04-10 20:07:47 +00001242 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001243 Parser.Lex(); // Eat the field.
1244 Disp = NewDisp;
1245 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001246
Chad Rosier5c118fd2013-01-14 22:31:35 +00001247 int BaseReg = SM.getBaseReg();
1248 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001249 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001250 if (!isParsingInlineAsm()) {
1251 // handle [-42]
1252 if (!BaseReg && !IndexReg) {
1253 if (!SegReg)
1254 return X86Operand::CreateMem(Disp, Start, End, Size);
1255 else
1256 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1257 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001258 StringRef ErrMsg;
1259 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1260 Error(StartInBrac, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001261 return nullptr;
Kevin Enderbybc570f22014-01-23 22:34:42 +00001262 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001263 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1264 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001265 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001266
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001267 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001268 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001269 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001270}
1271
Chad Rosier8a244662013-04-02 20:02:33 +00001272// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001273bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1274 StringRef &Identifier,
1275 InlineAsmIdentifierInfo &Info,
1276 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001277 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001278 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001279
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001280 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001281 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001282
Chad Rosier8a244662013-04-02 20:02:33 +00001283 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001284
1285 // Advance the token stream until the end of the current token is
1286 // after the end of what the frontend claimed.
1287 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1288 while (true) {
1289 End = Tok.getEndLoc();
1290 getLexer().Lex();
1291
1292 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1293 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001294 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001295
1296 // Create the symbol reference.
1297 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001298 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1299 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001300 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001301 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001302}
1303
David Majnemeraa34d792013-08-27 21:56:17 +00001304/// \brief Parse intel style segment override.
David Blaikie960ea3f2014-06-08 16:18:35 +00001305std::unique_ptr<X86Operand>
1306X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start,
1307 unsigned Size) {
David Majnemeraa34d792013-08-27 21:56:17 +00001308 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1309 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1310 if (Tok.isNot(AsmToken::Colon))
1311 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1312 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001313
David Majnemeraa34d792013-08-27 21:56:17 +00001314 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001315 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001316 ImmDisp = Tok.getIntVal();
1317 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1318
Chad Rosier1530ba52013-03-27 21:49:56 +00001319 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001320 InstInfo->AsmRewrites->push_back(
1321 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1322
1323 if (getLexer().isNot(AsmToken::LBrac)) {
1324 // An immediate following a 'segment register', 'colon' token sequence can
1325 // be followed by a bracketed expression. If it isn't we know we have our
1326 // final segment override.
1327 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1328 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1329 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1330 Size);
1331 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001332 }
1333
Chad Rosier91c82662012-10-24 17:22:29 +00001334 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001335 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001336
David Majnemeraa34d792013-08-27 21:56:17 +00001337 const MCExpr *Val;
1338 SMLoc End;
1339 if (!isParsingInlineAsm()) {
1340 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001341 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001342
1343 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001344 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001345
David Majnemeraa34d792013-08-27 21:56:17 +00001346 InlineAsmIdentifierInfo Info;
1347 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001348 if (ParseIntelIdentifier(Val, Identifier, Info,
1349 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001350 return nullptr;
David Majnemeraa34d792013-08-27 21:56:17 +00001351 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1352 /*Scale=*/1, Start, End, Size, Identifier, Info);
1353}
1354
1355/// ParseIntelMemOperand - Parse intel style memory operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00001356std::unique_ptr<X86Operand> X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp,
1357 SMLoc Start,
1358 unsigned Size) {
David Majnemeraa34d792013-08-27 21:56:17 +00001359 const AsmToken &Tok = Parser.getTok();
1360 SMLoc End;
1361
1362 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1363 if (getLexer().is(AsmToken::LBrac))
1364 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001365 assert(ImmDisp == 0);
David Majnemeraa34d792013-08-27 21:56:17 +00001366
Chad Rosier95ce8892013-04-19 18:39:50 +00001367 const MCExpr *Val;
1368 if (!isParsingInlineAsm()) {
1369 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001370 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001371
1372 return X86Operand::CreateMem(Val, Start, End, Size);
1373 }
1374
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001375 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001376 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001377 if (ParseIntelIdentifier(Val, Identifier, Info,
1378 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001379 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001380
1381 if (!getLexer().is(AsmToken::LBrac))
1382 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
1383 /*Scale=*/1, Start, End, Size, Identifier, Info);
1384
1385 Parser.Lex(); // Eat '['
1386
1387 // Parse Identifier [ ImmDisp ]
1388 IntelExprStateMachine SM(/*ImmDisp=*/0, /*StopOnLBrac=*/true,
1389 /*AddImmPrefix=*/false);
1390 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001391 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001392
1393 if (SM.getSym()) {
1394 Error(Start, "cannot use more than one symbol in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001395 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001396 }
1397 if (SM.getBaseReg()) {
1398 Error(Start, "cannot use base register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001399 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001400 }
1401 if (SM.getIndexReg()) {
1402 Error(Start, "cannot use index register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001403 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001404 }
1405
1406 const MCExpr *Disp = MCConstantExpr::Create(SM.getImm(), getContext());
1407 // BaseReg is non-zero to avoid assertions. In the context of inline asm,
1408 // we're pointing to a local variable in memory, so the base register is
1409 // really the frame or stack pointer.
1410 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/1, /*IndexReg=*/0,
1411 /*Scale=*/1, Start, End, Size, Identifier,
1412 Info.OpDecl);
Chad Rosier91c82662012-10-24 17:22:29 +00001413}
1414
Chad Rosier5dcb4662012-10-24 22:21:50 +00001415/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001416bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001417 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001418 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001419 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001420
1421 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001422 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001423 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001424 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001425 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001426
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001427 // Drop the optional '.'.
1428 StringRef DotDispStr = Tok.getString();
1429 if (DotDispStr.startswith("."))
1430 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001431
Chad Rosier5dcb4662012-10-24 22:21:50 +00001432 // .Imm gets lexed as a real.
1433 if (Tok.is(AsmToken::Real)) {
1434 APInt DotDisp;
1435 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001436 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001437 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001438 unsigned DotDisp;
1439 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1440 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001441 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001442 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001443 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001444 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001445 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001446
Chad Rosier240b7b92012-10-25 21:51:10 +00001447 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1448 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1449 unsigned Len = DotDispStr.size();
1450 unsigned Val = OrigDispVal + DotDispVal;
1451 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1452 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001453 }
1454
Chad Rosiercc541e82013-04-19 15:57:00 +00001455 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001456 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001457}
1458
Chad Rosier91c82662012-10-24 17:22:29 +00001459/// Parse the 'offset' operator. This operator is used to specify the
1460/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001461std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001462 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001463 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001464 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001465
Chad Rosier91c82662012-10-24 17:22:29 +00001466 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001467 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001468 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001469 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001470 if (ParseIntelIdentifier(Val, Identifier, Info,
1471 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001472 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001473
Chad Rosiere2f03772012-10-26 16:09:20 +00001474 // Don't emit the offset operator.
1475 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1476
Chad Rosier91c82662012-10-24 17:22:29 +00001477 // The offset operator will have an 'r' constraint, thus we need to create
1478 // register operand to ensure proper matching. Just pick a GPR based on
1479 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001480 unsigned RegNo =
1481 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001482 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001483 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001484}
1485
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001486enum IntelOperatorKind {
1487 IOK_LENGTH,
1488 IOK_SIZE,
1489 IOK_TYPE
1490};
1491
1492/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1493/// returns the number of elements in an array. It returns the value 1 for
1494/// non-array variables. The SIZE operator returns the size of a C or C++
1495/// variable. A variable's size is the product of its LENGTH and TYPE. The
1496/// TYPE operator returns the size of a C or C++ type or variable. If the
1497/// variable is an array, TYPE returns the size of a single element.
David Blaikie960ea3f2014-06-08 16:18:35 +00001498std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001499 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001500 SMLoc TypeLoc = Tok.getLoc();
1501 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001502
Craig Topper062a2ba2014-04-25 05:30:21 +00001503 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001504 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001505 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001506 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001507 if (ParseIntelIdentifier(Val, Identifier, Info,
1508 /*Unevaluated=*/true, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001509 return nullptr;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001510
1511 if (!Info.OpDecl)
1512 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001513
Chad Rosierf6675c32013-04-22 17:01:46 +00001514 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001515 switch(OpKind) {
1516 default: llvm_unreachable("Unexpected operand kind!");
1517 case IOK_LENGTH: CVal = Info.Length; break;
1518 case IOK_SIZE: CVal = Info.Size; break;
1519 case IOK_TYPE: CVal = Info.Type; break;
1520 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001521
1522 // Rewrite the type operator and the C or C++ type or variable in terms of an
1523 // immediate. E.g. TYPE foo -> $$4
1524 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001525 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001526
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001527 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001528 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001529}
1530
David Blaikie960ea3f2014-06-08 16:18:35 +00001531std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001532 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001533 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001534
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001535 // Offset, length, type and size operators.
1536 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001537 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001538 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001539 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001540 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001541 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001542 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001543 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001544 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001545 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001546 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001547
David Majnemeraa34d792013-08-27 21:56:17 +00001548 unsigned Size = getIntelMemOperandSize(Tok.getString());
1549 if (Size) {
1550 Parser.Lex(); // Eat operand size (e.g., byte, word).
1551 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1552 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1553 Parser.Lex(); // Eat ptr.
1554 }
1555 Start = Tok.getLoc();
1556
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001557 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001558 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001559 getLexer().is(AsmToken::Tilde) || getLexer().is(AsmToken::LParen)) {
Chad Rosierbfb70992013-04-17 00:11:46 +00001560 AsmToken StartTok = Tok;
1561 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1562 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001563 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001564 return nullptr;
Chad Rosierbfb70992013-04-17 00:11:46 +00001565
1566 int64_t Imm = SM.getImm();
1567 if (isParsingInlineAsm()) {
1568 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1569 if (StartTok.getString().size() == Len)
1570 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001571 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001572 else
1573 // Otherwise, rewrite the complex expression as a single immediate.
1574 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001575 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001576
1577 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001578 // If a directional label (ie. 1f or 2b) was parsed above from
1579 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1580 // to the MCExpr with the directional local symbol and this is a
1581 // memory operand not an immediate operand.
1582 if (SM.getSym())
1583 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1584
Chad Rosierbfb70992013-04-17 00:11:46 +00001585 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1586 return X86Operand::CreateImm(ImmExpr, Start, End);
1587 }
1588
1589 // Only positive immediates are valid.
1590 if (Imm < 0)
1591 return ErrorOperand(Start, "expected a positive immediate displacement "
1592 "before bracketed expr.");
1593
1594 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001595 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001596 }
1597
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001598 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001599 unsigned RegNo = 0;
1600 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001601 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001602 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001603 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001604 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001605
David Majnemeraa34d792013-08-27 21:56:17 +00001606 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001607 }
1608
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001609 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001610 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001611}
1612
David Blaikie960ea3f2014-06-08 16:18:35 +00001613std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001614 switch (getLexer().getKind()) {
1615 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001616 // Parse a memory operand with no segment register.
1617 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001618 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001619 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001620 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001621 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001622 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001623 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001624 Error(Start, "%eiz and %riz can only be used as index registers",
1625 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001626 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001627 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001628
Chris Lattnerb9270732010-04-17 18:56:34 +00001629 // If this is a segment register followed by a ':', then this is the start
1630 // of a memory reference, otherwise this is a normal register reference.
1631 if (getLexer().isNot(AsmToken::Colon))
1632 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001633
Chris Lattnerb9270732010-04-17 18:56:34 +00001634 getParser().Lex(); // Eat the colon.
1635 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001636 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001637 case AsmToken::Dollar: {
1638 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001639 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001640 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001641 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001642 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001643 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001644 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001645 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001646 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001647}
1648
David Blaikie960ea3f2014-06-08 16:18:35 +00001649bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1650 const MCParsedAsmOperand &Op) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001651 if(STI.getFeatureBits() & X86::FeatureAVX512) {
1652 if (getLexer().is(AsmToken::LCurly)) {
1653 // Eat "{" and mark the current place.
1654 const SMLoc consumedToken = consumeToken();
1655 // Distinguish {1to<NUM>} from {%k<NUM>}.
1656 if(getLexer().is(AsmToken::Integer)) {
1657 // Parse memory broadcasting ({1to<NUM>}).
1658 if (getLexer().getTok().getIntVal() != 1)
1659 return !ErrorAndEatStatement(getLexer().getLoc(),
1660 "Expected 1to<NUM> at this point");
1661 Parser.Lex(); // Eat "1" of 1to8
1662 if (!getLexer().is(AsmToken::Identifier) ||
1663 !getLexer().getTok().getIdentifier().startswith("to"))
1664 return !ErrorAndEatStatement(getLexer().getLoc(),
1665 "Expected 1to<NUM> at this point");
1666 // Recognize only reasonable suffixes.
1667 const char *BroadcastPrimitive =
1668 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
1669 .Case("to8", "{1to8}")
1670 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001671 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001672 if (!BroadcastPrimitive)
1673 return !ErrorAndEatStatement(getLexer().getLoc(),
1674 "Invalid memory broadcast primitive.");
1675 Parser.Lex(); // Eat "toN" of 1toN
1676 if (!getLexer().is(AsmToken::RCurly))
1677 return !ErrorAndEatStatement(getLexer().getLoc(),
1678 "Expected } at this point");
1679 Parser.Lex(); // Eat "}"
1680 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1681 consumedToken));
1682 // No AVX512 specific primitives can pass
1683 // after memory broadcasting, so return.
1684 return true;
1685 } else {
1686 // Parse mask register {%k1}
1687 Operands.push_back(X86Operand::CreateToken("{", consumedToken));
David Blaikie960ea3f2014-06-08 16:18:35 +00001688 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1689 Operands.push_back(std::move(Op));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001690 if (!getLexer().is(AsmToken::RCurly))
1691 return !ErrorAndEatStatement(getLexer().getLoc(),
1692 "Expected } at this point");
1693 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1694
1695 // Parse "zeroing non-masked" semantic {z}
1696 if (getLexer().is(AsmToken::LCurly)) {
1697 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1698 if (!getLexer().is(AsmToken::Identifier) ||
1699 getLexer().getTok().getIdentifier() != "z")
1700 return !ErrorAndEatStatement(getLexer().getLoc(),
1701 "Expected z at this point");
1702 Parser.Lex(); // Eat the z
1703 if (!getLexer().is(AsmToken::RCurly))
1704 return !ErrorAndEatStatement(getLexer().getLoc(),
1705 "Expected } at this point");
1706 Parser.Lex(); // Eat the }
1707 }
1708 }
1709 }
1710 }
1711 }
1712 return true;
1713}
1714
Chris Lattnerb9270732010-04-17 18:56:34 +00001715/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1716/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00001717std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
1718 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001719
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001720 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1721 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001722 // only way to do this without lookahead is to eat the '(' and see what is
1723 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001724 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001725 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001726 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00001727 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001728
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001729 // After parsing the base expression we could either have a parenthesized
1730 // memory address or not. If not, return now. If so, eat the (.
1731 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001732 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001733 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001734 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001735 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001736 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001737
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001738 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001739 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001740 } else {
1741 // Okay, we have a '('. We don't know if this is an expression or not, but
1742 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001743 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001744 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001745
Kevin Enderby7d912182009-09-03 17:15:07 +00001746 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001747 // Nothing to do here, fall into the code below with the '(' part of the
1748 // memory operand consumed.
1749 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001750 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001751
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001752 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001753 if (getParser().parseParenExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00001754 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001755
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001756 // After parsing the base expression we could either have a parenthesized
1757 // memory address or not. If not, return now. If so, eat the (.
1758 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001759 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001760 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001761 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001762 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001763 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001764
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001765 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001766 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001767 }
1768 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001769
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001770 // If we reached here, then we just ate the ( of the memory operand. Process
1771 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001772 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001773 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001774
Chris Lattner0c2538f2010-01-15 18:51:29 +00001775 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001776 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001777 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00001778 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001779 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001780 Error(StartLoc, "eiz and riz can only be used as index registers",
1781 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00001782 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001783 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001784 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001785
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001786 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001787 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001788 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001789
1790 // Following the comma we should have either an index register, or a scale
1791 // value. We don't support the later form, but we want to parse it
1792 // correctly.
1793 //
1794 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001795 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001796 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001797 SMLoc L;
Craig Topper062a2ba2014-04-25 05:30:21 +00001798 if (ParseRegister(IndexReg, L, L)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001799
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001800 if (getLexer().isNot(AsmToken::RParen)) {
1801 // Parse the scale amount:
1802 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001803 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001804 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001805 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001806 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001807 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001808 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001809
1810 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001811 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001812
1813 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001814 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001815 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001816 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001817 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001818
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001819 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001820 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1821 ScaleVal != 1) {
1822 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00001823 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001824 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001825 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1826 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00001827 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001828 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001829 Scale = (unsigned)ScaleVal;
1830 }
1831 }
1832 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001833 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001834 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001835 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001836
1837 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001838 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00001839 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001840
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001841 if (Value != 1)
1842 Warning(Loc, "scale factor without index register is ignored");
1843 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001844 }
1845 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001846
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001847 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001848 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001849 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001850 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001851 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001852 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001853 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001854
David Woodhouse6dbda442014-01-08 12:58:28 +00001855 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1856 // and then only in non-64-bit modes. Except for DX, which is a special case
1857 // because an unofficial form of in/out instructions uses it.
1858 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1859 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1860 BaseReg != X86::SI && BaseReg != X86::DI)) &&
1861 BaseReg != X86::DX) {
1862 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001863 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001864 }
1865 if (BaseReg == 0 &&
1866 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
1867 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001868 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001869 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001870
1871 StringRef ErrMsg;
1872 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1873 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001874 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001875 }
1876
Chris Lattner015cfb12010-01-15 19:33:43 +00001877 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1878 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001879}
1880
David Blaikie960ea3f2014-06-08 16:18:35 +00001881bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
1882 SMLoc NameLoc, OperandVector &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001883 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001884 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001885
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001886 // FIXME: Hack to recognize setneb as setne.
1887 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1888 PatchedName != "setb" && PatchedName != "setnb")
1889 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001890
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001891 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Craig Topper062a2ba2014-04-25 05:30:21 +00001892 const MCExpr *ExtraImmOp = nullptr;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001893 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001894 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1895 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001896 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001897 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001898 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001899 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001900 .Case("eq", 0x00)
1901 .Case("lt", 0x01)
1902 .Case("le", 0x02)
1903 .Case("unord", 0x03)
1904 .Case("neq", 0x04)
1905 .Case("nlt", 0x05)
1906 .Case("nle", 0x06)
1907 .Case("ord", 0x07)
1908 /* AVX only from here */
1909 .Case("eq_uq", 0x08)
1910 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001911 .Case("ngt", 0x0A)
1912 .Case("false", 0x0B)
1913 .Case("neq_oq", 0x0C)
1914 .Case("ge", 0x0D)
1915 .Case("gt", 0x0E)
1916 .Case("true", 0x0F)
1917 .Case("eq_os", 0x10)
1918 .Case("lt_oq", 0x11)
1919 .Case("le_oq", 0x12)
1920 .Case("unord_s", 0x13)
1921 .Case("neq_us", 0x14)
1922 .Case("nlt_uq", 0x15)
1923 .Case("nle_uq", 0x16)
1924 .Case("ord_s", 0x17)
1925 .Case("eq_us", 0x18)
1926 .Case("nge_uq", 0x19)
1927 .Case("ngt_uq", 0x1A)
1928 .Case("false_os", 0x1B)
1929 .Case("neq_os", 0x1C)
1930 .Case("ge_oq", 0x1D)
1931 .Case("gt_oq", 0x1E)
1932 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001933 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001934 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001935 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1936 getParser().getContext());
1937 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001938 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001939 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001940 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001941 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001942 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001943 } else {
1944 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001945 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001946 }
1947 }
1948 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001949
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001950 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001951
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001952 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001953 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001954
Chris Lattner086a83a2010-09-08 05:17:37 +00001955 // Determine whether this is an instruction prefix.
1956 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001957 Name == "lock" || Name == "rep" ||
1958 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001959 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001960 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001961
1962
Chris Lattner086a83a2010-09-08 05:17:37 +00001963 // This does the actual operand parsing. Don't parse any more if we have a
1964 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1965 // just want to parse the "lock" as the first instruction and the "incl" as
1966 // the next one.
1967 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001968
1969 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00001970 if (getLexer().is(AsmToken::Star))
1971 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00001972
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001973 // Read the operands.
1974 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00001975 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1976 Operands.push_back(std::move(Op));
1977 if (!HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00001978 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001979 } else {
1980 Parser.eatToEndOfStatement();
1981 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00001982 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001983 // check for comma and eat it
1984 if (getLexer().is(AsmToken::Comma))
1985 Parser.Lex();
1986 else
1987 break;
1988 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00001989
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001990 if (getLexer().isNot(AsmToken::EndOfStatement))
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001991 return ErrorAndEatStatement(getLexer().getLoc(),
1992 "unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001993 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001994
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001995 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001996 if (getLexer().is(AsmToken::EndOfStatement) ||
1997 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001998 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001999
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002000 if (ExtraImmOp && isParsingIntelSyntax())
2001 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2002
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002003 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2004 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2005 // documented form in various unofficial manuals, so a lot of code uses it.
2006 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2007 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002008 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002009 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2010 isa<MCConstantExpr>(Op.Mem.Disp) &&
2011 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2012 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2013 SMLoc Loc = Op.getEndLoc();
2014 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002015 }
2016 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002017 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2018 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2019 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002020 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002021 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2022 isa<MCConstantExpr>(Op.Mem.Disp) &&
2023 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2024 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2025 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002026 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002027 }
2028 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002029
2030 // Append default arguments to "ins[bwld]"
2031 if (Name.startswith("ins") && Operands.size() == 1 &&
2032 (Name == "insb" || Name == "insw" || Name == "insl" ||
2033 Name == "insd" )) {
2034 if (isParsingIntelSyntax()) {
2035 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2036 Operands.push_back(DefaultMemDIOperand(NameLoc));
2037 } else {
2038 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2039 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002040 }
2041 }
2042
David Woodhousec472b812014-01-22 15:08:49 +00002043 // Append default arguments to "outs[bwld]"
2044 if (Name.startswith("outs") && Operands.size() == 1 &&
2045 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
2046 Name == "outsd" )) {
2047 if (isParsingIntelSyntax()) {
2048 Operands.push_back(DefaultMemSIOperand(NameLoc));
2049 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2050 } else {
2051 Operands.push_back(DefaultMemSIOperand(NameLoc));
2052 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002053 }
2054 }
2055
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002056 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2057 // values of $SIREG according to the mode. It would be nice if this
2058 // could be achieved with InstAlias in the tables.
2059 if (Name.startswith("lods") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002060 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002061 Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
2062 Operands.push_back(DefaultMemSIOperand(NameLoc));
2063
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002064 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2065 // values of $DIREG according to the mode. It would be nice if this
2066 // could be achieved with InstAlias in the tables.
2067 if (Name.startswith("stos") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002068 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002069 Name == "stosl" || Name == "stosd" || Name == "stosq"))
2070 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002071
David Woodhouse20fe4802014-01-22 15:08:27 +00002072 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2073 // values of $DIREG according to the mode. It would be nice if this
2074 // could be achieved with InstAlias in the tables.
2075 if (Name.startswith("scas") && Operands.size() == 1 &&
2076 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
2077 Name == "scasl" || Name == "scasd" || Name == "scasq"))
2078 Operands.push_back(DefaultMemDIOperand(NameLoc));
2079
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002080 // Add default SI and DI operands to "cmps[bwlq]".
2081 if (Name.startswith("cmps") &&
2082 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2083 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
2084 if (Operands.size() == 1) {
2085 if (isParsingIntelSyntax()) {
2086 Operands.push_back(DefaultMemSIOperand(NameLoc));
2087 Operands.push_back(DefaultMemDIOperand(NameLoc));
2088 } else {
2089 Operands.push_back(DefaultMemDIOperand(NameLoc));
2090 Operands.push_back(DefaultMemSIOperand(NameLoc));
2091 }
2092 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002093 X86Operand &Op = (X86Operand &)*Operands[1];
2094 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002095 if (!doSrcDstMatch(Op, Op2))
2096 return Error(Op.getStartLoc(),
2097 "mismatching source and destination index registers");
2098 }
2099 }
2100
David Woodhouse6f417de2014-01-22 15:08:42 +00002101 // Add default SI and DI operands to "movs[bwlq]".
2102 if ((Name.startswith("movs") &&
2103 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2104 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2105 (Name.startswith("smov") &&
2106 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2107 Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2108 if (Operands.size() == 1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002109 if (Name == "movsd")
David Woodhouse6f417de2014-01-22 15:08:42 +00002110 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2111 if (isParsingIntelSyntax()) {
2112 Operands.push_back(DefaultMemDIOperand(NameLoc));
2113 Operands.push_back(DefaultMemSIOperand(NameLoc));
2114 } else {
2115 Operands.push_back(DefaultMemSIOperand(NameLoc));
2116 Operands.push_back(DefaultMemDIOperand(NameLoc));
2117 }
2118 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002119 X86Operand &Op = (X86Operand &)*Operands[1];
2120 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse6f417de2014-01-22 15:08:42 +00002121 if (!doSrcDstMatch(Op, Op2))
2122 return Error(Op.getStartLoc(),
2123 "mismatching source and destination index registers");
2124 }
2125 }
2126
Chris Lattner4bd21712010-09-15 04:33:27 +00002127 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002128 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002129 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002130 Name.startswith("shl") || Name.startswith("sal") ||
2131 Name.startswith("rcl") || Name.startswith("rcr") ||
2132 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002133 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002134 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002135 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002136 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2137 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2138 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002139 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002140 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002141 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2142 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2143 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002144 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002145 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002146 }
Chad Rosier51afe632012-06-27 22:34:28 +00002147
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002148 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2149 // instalias with an immediate operand yet.
2150 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002151 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2152 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2153 cast<MCConstantExpr>(Op1.getImm())->getValue() == 3) {
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002154 Operands.erase(Operands.begin() + 1);
David Blaikie960ea3f2014-06-08 16:18:35 +00002155 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002156 }
2157 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002158
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002159 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002160}
2161
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002162static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2163 bool isCmp) {
2164 MCInst TmpInst;
2165 TmpInst.setOpcode(Opcode);
2166 if (!isCmp)
2167 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2168 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2169 TmpInst.addOperand(Inst.getOperand(0));
2170 Inst = TmpInst;
2171 return true;
2172}
2173
2174static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2175 bool isCmp = false) {
2176 if (!Inst.getOperand(0).isImm() ||
2177 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2178 return false;
2179
2180 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2181}
2182
2183static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2184 bool isCmp = false) {
2185 if (!Inst.getOperand(0).isImm() ||
2186 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2187 return false;
2188
2189 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2190}
2191
2192static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2193 bool isCmp = false) {
2194 if (!Inst.getOperand(0).isImm() ||
2195 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2196 return false;
2197
2198 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2199}
2200
David Blaikie960ea3f2014-06-08 16:18:35 +00002201bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Devang Patelde47cce2012-01-18 22:42:29 +00002202 switch (Inst.getOpcode()) {
2203 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002204 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2205 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2206 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2207 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2208 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2209 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2210 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2211 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2212 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2213 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2214 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2215 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2216 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2217 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2218 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2219 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2220 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2221 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002222 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2223 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2224 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2225 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2226 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2227 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002228 case X86::VMOVAPDrr:
2229 case X86::VMOVAPDYrr:
2230 case X86::VMOVAPSrr:
2231 case X86::VMOVAPSYrr:
2232 case X86::VMOVDQArr:
2233 case X86::VMOVDQAYrr:
2234 case X86::VMOVDQUrr:
2235 case X86::VMOVDQUYrr:
2236 case X86::VMOVUPDrr:
2237 case X86::VMOVUPDYrr:
2238 case X86::VMOVUPSrr:
2239 case X86::VMOVUPSYrr: {
2240 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2241 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2242 return false;
2243
2244 unsigned NewOpc;
2245 switch (Inst.getOpcode()) {
2246 default: llvm_unreachable("Invalid opcode");
2247 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2248 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2249 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2250 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2251 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2252 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2253 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2254 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2255 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2256 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2257 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2258 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2259 }
2260 Inst.setOpcode(NewOpc);
2261 return true;
2262 }
2263 case X86::VMOVSDrr:
2264 case X86::VMOVSSrr: {
2265 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2266 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2267 return false;
2268 unsigned NewOpc;
2269 switch (Inst.getOpcode()) {
2270 default: llvm_unreachable("Invalid opcode");
2271 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2272 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2273 }
2274 Inst.setOpcode(NewOpc);
2275 return true;
2276 }
Devang Patelde47cce2012-01-18 22:42:29 +00002277 }
Devang Patelde47cce2012-01-18 22:42:29 +00002278}
2279
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002280static const char *getSubtargetFeatureName(unsigned Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002281
David Blaikie960ea3f2014-06-08 16:18:35 +00002282void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2283 MCStreamer &Out) {
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +00002284 Instrumentation->InstrumentInstruction(Inst, Operands, getContext(), MII,
2285 Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002286 Out.EmitInstruction(Inst, STI);
2287}
2288
David Blaikie960ea3f2014-06-08 16:18:35 +00002289bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2290 OperandVector &Operands,
2291 MCStreamer &Out, unsigned &ErrorInfo,
2292 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002293 assert(!Operands.empty() && "Unexpect empty operand list!");
David Blaikie960ea3f2014-06-08 16:18:35 +00002294 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2295 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002296 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002297
Chris Lattnera63292a2010-09-29 01:50:45 +00002298 // First, handle aliases that expand to multiple instructions.
2299 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002300 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002301 // call.
David Blaikie960ea3f2014-06-08 16:18:35 +00002302 if (Op.getToken() == "fstsw" || Op.getToken() == "fstcw" ||
2303 Op.getToken() == "fstsww" || Op.getToken() == "fstcww" ||
2304 Op.getToken() == "finit" || Op.getToken() == "fsave" ||
2305 Op.getToken() == "fstenv" || Op.getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002306 MCInst Inst;
2307 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002308 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002309 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002310 EmitInstruction(Inst, Operands, Out);
Chris Lattnera63292a2010-09-29 01:50:45 +00002311
David Blaikie960ea3f2014-06-08 16:18:35 +00002312 const char *Repl = StringSwitch<const char *>(Op.getToken())
2313 .Case("finit", "fninit")
2314 .Case("fsave", "fnsave")
2315 .Case("fstcw", "fnstcw")
2316 .Case("fstcww", "fnstcw")
2317 .Case("fstenv", "fnstenv")
2318 .Case("fstsw", "fnstsw")
2319 .Case("fstsww", "fnstsw")
2320 .Case("fclex", "fnclex")
2321 .Default(nullptr);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002322 assert(Repl && "Unknown wait-prefixed instruction");
2323 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002324 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002325
Chris Lattner628fbec2010-09-06 21:54:15 +00002326 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002327 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002328
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002329 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002330 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002331 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002332 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002333 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002334 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002335 // Some instructions need post-processing to, for example, tweak which
2336 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002337 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002338 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002339 while (processInstruction(Inst, Operands))
2340 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002341
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002342 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002343 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002344 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002345 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002346 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002347 case Match_MissingFeature: {
2348 assert(ErrorInfo && "Unknown missing feature!");
2349 // Special case the error message for the very common case where only
2350 // a single subtarget feature is missing.
2351 std::string Msg = "instruction requires:";
2352 unsigned Mask = 1;
2353 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2354 if (ErrorInfo & Mask) {
2355 Msg += " ";
2356 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2357 }
2358 Mask <<= 1;
2359 }
2360 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2361 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002362 case Match_InvalidOperand:
2363 WasOriginallyInvalidOperand = true;
2364 break;
2365 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002366 break;
2367 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002368
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002369 // FIXME: Ideally, we would only attempt suffix matches for things which are
2370 // valid prefixes, and we could just infer the right unambiguous
2371 // type. However, that requires substantially more matcher support than the
2372 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002373
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002374 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002375 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002376 SmallString<16> Tmp;
2377 Tmp += Base;
2378 Tmp += ' ';
David Blaikie960ea3f2014-06-08 16:18:35 +00002379 Op.setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002380
Chris Lattnerfab94132010-11-06 18:28:02 +00002381 // If this instruction starts with an 'f', then it is a floating point stack
2382 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2383 // 80-bit floating point, which use the suffixes s,l,t respectively.
2384 //
2385 // Otherwise, we assume that this may be an integer instruction, which comes
2386 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2387 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002388
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002389 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002390 Tmp[Base.size()] = Suffixes[0];
2391 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002392 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002393 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002394
Chad Rosier2f480a82012-10-12 22:53:36 +00002395 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002396 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002397 // If this returned as a missing feature failure, remember that.
2398 if (Match1 == Match_MissingFeature)
2399 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002400 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002401 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002402 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002403 // If this returned as a missing feature failure, remember that.
2404 if (Match2 == Match_MissingFeature)
2405 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002406 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002407 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002408 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002409 // If this returned as a missing feature failure, remember that.
2410 if (Match3 == Match_MissingFeature)
2411 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002412 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002413 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002414 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002415 // If this returned as a missing feature failure, remember that.
2416 if (Match4 == Match_MissingFeature)
2417 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002418
2419 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002420 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002421
2422 // If exactly one matched, then we treat that as a successful match (and the
2423 // instruction will already have been filled in correctly, since the failing
2424 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002425 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002426 (Match1 == Match_Success) + (Match2 == Match_Success) +
2427 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002428 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002429 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002430 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002431 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002432 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002433 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002434 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002435
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002436 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002437
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002438 // If we had multiple suffix matches, then identify this as an ambiguous
2439 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002440 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002441 char MatchChars[4];
2442 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002443 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2444 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2445 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2446 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002447
Alp Tokere69170a2014-06-26 22:52:05 +00002448 SmallString<126> Msg;
2449 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002450 OS << "ambiguous instructions require an explicit suffix (could be ";
2451 for (unsigned i = 0; i != NumMatches; ++i) {
2452 if (i != 0)
2453 OS << ", ";
2454 if (i + 1 == NumMatches)
2455 OS << "or ";
2456 OS << "'" << Base << MatchChars[i] << "'";
2457 }
2458 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002459 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002460 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002461 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002462
Chris Lattner628fbec2010-09-06 21:54:15 +00002463 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002464
Chris Lattner628fbec2010-09-06 21:54:15 +00002465 // If all of the instructions reported an invalid mnemonic, then the original
2466 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002467 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2468 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002469 if (!WasOriginallyInvalidOperand) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002470 ArrayRef<SMRange> Ranges =
2471 MatchingInlineAsm ? EmptyRanges : Op.getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002472 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002473 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002474 }
2475
2476 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002477 if (ErrorInfo != ~0U) {
2478 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002479 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002480 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002481
David Blaikie960ea3f2014-06-08 16:18:35 +00002482 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2483 if (Operand.getStartLoc().isValid()) {
2484 SMRange OperandRange = Operand.getLocRange();
2485 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002486 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002487 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002488 }
2489
Chad Rosier3d4bc622012-08-21 19:36:59 +00002490 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002491 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002492 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002493
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002494 // If one instruction matched with a missing feature, report this as a
2495 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002496 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2497 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002498 std::string Msg = "instruction requires:";
2499 unsigned Mask = 1;
2500 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2501 if (ErrorInfoMissingFeature & Mask) {
2502 Msg += " ";
2503 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2504 }
2505 Mask <<= 1;
2506 }
2507 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002508 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002509
Chris Lattner628fbec2010-09-06 21:54:15 +00002510 // If one instruction matched with an invalid operand, report this as an
2511 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002512 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2513 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002514 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002515 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002516 return true;
2517 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002518
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002519 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002520 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002521 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002522 return true;
2523}
2524
Nico Weber42f79db2014-07-17 20:24:55 +00002525bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
2526 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
2527}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002528
Devang Patel4a6e7782012-01-12 18:03:40 +00002529bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002530 StringRef IDVal = DirectiveID.getIdentifier();
2531 if (IDVal == ".word")
2532 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002533 else if (IDVal.startswith(".code"))
2534 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002535 else if (IDVal.startswith(".att_syntax")) {
2536 getParser().setAssemblerDialect(0);
2537 return false;
2538 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002539 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002540 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002541 // FIXME: Handle noprefix
2542 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002543 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002544 }
2545 return false;
2546 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002547 return true;
2548}
2549
2550/// ParseDirectiveWord
2551/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002552bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002553 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2554 for (;;) {
2555 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002556 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002557 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002558
Eric Christopherbf7bc492013-01-09 03:52:05 +00002559 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002560
Chris Lattner72c0b592010-10-30 17:38:55 +00002561 if (getLexer().is(AsmToken::EndOfStatement))
2562 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002563
Chris Lattner72c0b592010-10-30 17:38:55 +00002564 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002565 if (getLexer().isNot(AsmToken::Comma)) {
2566 Error(L, "unexpected token in directive");
2567 return false;
2568 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002569 Parser.Lex();
2570 }
2571 }
Chad Rosier51afe632012-06-27 22:34:28 +00002572
Chris Lattner72c0b592010-10-30 17:38:55 +00002573 Parser.Lex();
2574 return false;
2575}
2576
Evan Cheng481ebb02011-07-27 00:38:12 +00002577/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002578/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002579bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Craig Topper3c80d622014-01-06 04:55:54 +00002580 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002581 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002582 if (!is16BitMode()) {
2583 SwitchMode(X86::Mode16Bit);
2584 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2585 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002586 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002587 Parser.Lex();
2588 if (!is32BitMode()) {
2589 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002590 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2591 }
2592 } else if (IDVal == ".code64") {
2593 Parser.Lex();
2594 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002595 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002596 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2597 }
2598 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002599 Error(L, "unknown directive " + IDVal);
2600 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002601 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002602
Evan Cheng481ebb02011-07-27 00:38:12 +00002603 return false;
2604}
Chris Lattner72c0b592010-10-30 17:38:55 +00002605
Daniel Dunbar71475772009-07-17 20:42:00 +00002606// Force static initialization.
2607extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002608 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2609 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002610}
Daniel Dunbar00331992009-07-29 00:02:19 +00002611
Chris Lattner3e4582a2010-09-06 19:11:01 +00002612#define GET_REGISTER_MATCHER
2613#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002614#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002615#include "X86GenAsmMatcher.inc"