blob: e504f2705529497605d814cae08d7ebb489998a5 [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"
Reid Kleckner7b1e1a02014-07-30 22:23:11 +000035#include <algorithm>
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000036#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000037
Daniel Dunbar71475772009-07-17 20:42:00 +000038using namespace llvm;
39
40namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000041
Chad Rosier5362af92013-04-16 18:15:40 +000042static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000043 0, // IC_OR
44 1, // IC_AND
Kevin Enderbyd6b10712014-02-06 01:21:15 +000045 2, // IC_LSHIFT
46 2, // IC_RSHIFT
47 3, // IC_PLUS
48 3, // IC_MINUS
49 4, // IC_MULTIPLY
50 4, // IC_DIVIDE
51 5, // IC_RPAREN
52 6, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000053 0, // IC_IMM
54 0 // IC_REGISTER
55};
56
Devang Patel4a6e7782012-01-12 18:03:40 +000057class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000058 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000059 MCAsmParser &Parser;
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000060 const MCInstrInfo &MII;
Chad Rosierf0e87202012-10-25 20:41:34 +000061 ParseInstructionInfo *InstInfo;
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000062 std::unique_ptr<X86AsmInstrumentation> Instrumentation;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000063private:
Alp Tokera5b88a52013-12-02 16:06:06 +000064 SMLoc consumeToken() {
65 SMLoc Result = Parser.getTok().getLoc();
66 Parser.Lex();
67 return Result;
68 }
69
Chad Rosier5362af92013-04-16 18:15:40 +000070 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000071 IC_OR = 0,
72 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +000073 IC_LSHIFT,
74 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000075 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000076 IC_MINUS,
77 IC_MULTIPLY,
78 IC_DIVIDE,
79 IC_RPAREN,
80 IC_LPAREN,
81 IC_IMM,
82 IC_REGISTER
83 };
84
85 class InfixCalculator {
86 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
87 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
88 SmallVector<ICToken, 4> PostfixStack;
89
90 public:
91 int64_t popOperand() {
92 assert (!PostfixStack.empty() && "Poped an empty stack!");
93 ICToken Op = PostfixStack.pop_back_val();
94 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
95 && "Expected and immediate or register!");
96 return Op.second;
97 }
98 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
99 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
100 "Unexpected operand!");
101 PostfixStack.push_back(std::make_pair(Op, Val));
102 }
103
Jakub Staszak9c349222013-08-08 15:48:46 +0000104 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000105 void pushOperator(InfixCalculatorTok Op) {
106 // Push the new operator if the stack is empty.
107 if (InfixOperatorStack.empty()) {
108 InfixOperatorStack.push_back(Op);
109 return;
110 }
111
112 // Push the new operator if it has a higher precedence than the operator
113 // on the top of the stack or the operator on the top of the stack is a
114 // left parentheses.
115 unsigned Idx = InfixOperatorStack.size() - 1;
116 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
117 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
118 InfixOperatorStack.push_back(Op);
119 return;
120 }
121
122 // The operator on the top of the stack has higher precedence than the
123 // new operator.
124 unsigned ParenCount = 0;
125 while (1) {
126 // Nothing to process.
127 if (InfixOperatorStack.empty())
128 break;
129
130 Idx = InfixOperatorStack.size() - 1;
131 StackOp = InfixOperatorStack[Idx];
132 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
133 break;
134
135 // If we have an even parentheses count and we see a left parentheses,
136 // then stop processing.
137 if (!ParenCount && StackOp == IC_LPAREN)
138 break;
139
140 if (StackOp == IC_RPAREN) {
141 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000142 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000143 } else if (StackOp == IC_LPAREN) {
144 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000145 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000146 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000147 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000148 PostfixStack.push_back(std::make_pair(StackOp, 0));
149 }
150 }
151 // Push the new operator.
152 InfixOperatorStack.push_back(Op);
153 }
154 int64_t execute() {
155 // Push any remaining operators onto the postfix stack.
156 while (!InfixOperatorStack.empty()) {
157 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
158 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
159 PostfixStack.push_back(std::make_pair(StackOp, 0));
160 }
161
162 if (PostfixStack.empty())
163 return 0;
164
165 SmallVector<ICToken, 16> OperandStack;
166 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
167 ICToken Op = PostfixStack[i];
168 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
169 OperandStack.push_back(Op);
170 } else {
171 assert (OperandStack.size() > 1 && "Too few operands.");
172 int64_t Val;
173 ICToken Op2 = OperandStack.pop_back_val();
174 ICToken Op1 = OperandStack.pop_back_val();
175 switch (Op.first) {
176 default:
177 report_fatal_error("Unexpected operator!");
178 break;
179 case IC_PLUS:
180 Val = Op1.second + Op2.second;
181 OperandStack.push_back(std::make_pair(IC_IMM, Val));
182 break;
183 case IC_MINUS:
184 Val = Op1.second - Op2.second;
185 OperandStack.push_back(std::make_pair(IC_IMM, Val));
186 break;
187 case IC_MULTIPLY:
188 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
189 "Multiply operation with an immediate and a register!");
190 Val = Op1.second * Op2.second;
191 OperandStack.push_back(std::make_pair(IC_IMM, Val));
192 break;
193 case IC_DIVIDE:
194 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
195 "Divide operation with an immediate and a register!");
196 assert (Op2.second != 0 && "Division by zero!");
197 Val = Op1.second / Op2.second;
198 OperandStack.push_back(std::make_pair(IC_IMM, Val));
199 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000200 case IC_OR:
201 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
202 "Or operation with an immediate and a register!");
203 Val = Op1.second | Op2.second;
204 OperandStack.push_back(std::make_pair(IC_IMM, Val));
205 break;
206 case IC_AND:
207 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
208 "And operation with an immediate and a register!");
209 Val = Op1.second & Op2.second;
210 OperandStack.push_back(std::make_pair(IC_IMM, Val));
211 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000212 case IC_LSHIFT:
213 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
214 "Left shift operation with an immediate and a register!");
215 Val = Op1.second << Op2.second;
216 OperandStack.push_back(std::make_pair(IC_IMM, Val));
217 break;
218 case IC_RSHIFT:
219 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
220 "Right shift operation with an immediate and a register!");
221 Val = Op1.second >> Op2.second;
222 OperandStack.push_back(std::make_pair(IC_IMM, Val));
223 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000224 }
225 }
226 }
227 assert (OperandStack.size() == 1 && "Expected a single result.");
228 return OperandStack.pop_back_val().second;
229 }
230 };
231
232 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000233 IES_OR,
234 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000235 IES_LSHIFT,
236 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000237 IES_PLUS,
238 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000239 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000240 IES_MULTIPLY,
241 IES_DIVIDE,
242 IES_LBRAC,
243 IES_RBRAC,
244 IES_LPAREN,
245 IES_RPAREN,
246 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000247 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000248 IES_IDENTIFIER,
249 IES_ERROR
250 };
251
252 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000253 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000254 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000255 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000256 const MCExpr *Sym;
257 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000258 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000259 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000260 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000261 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000262 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000263 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
Craig Topper062a2ba2014-04-25 05:30:21 +0000264 Scale(1), Imm(imm), Sym(nullptr), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000265 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000266
267 unsigned getBaseReg() { return BaseReg; }
268 unsigned getIndexReg() { return IndexReg; }
269 unsigned getScale() { return Scale; }
270 const MCExpr *getSym() { return Sym; }
271 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000272 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000273 bool isValidEndState() {
274 return State == IES_RBRAC || State == IES_INTEGER;
275 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000276 bool getStopOnLBrac() { return StopOnLBrac; }
277 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000278 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000279
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000280 InlineAsmIdentifierInfo &getIdentifierInfo() {
281 return Info;
282 }
283
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000284 void onOr() {
285 IntelExprState CurrState = State;
286 switch (State) {
287 default:
288 State = IES_ERROR;
289 break;
290 case IES_INTEGER:
291 case IES_RPAREN:
292 case IES_REGISTER:
293 State = IES_OR;
294 IC.pushOperator(IC_OR);
295 break;
296 }
297 PrevState = CurrState;
298 }
299 void onAnd() {
300 IntelExprState CurrState = State;
301 switch (State) {
302 default:
303 State = IES_ERROR;
304 break;
305 case IES_INTEGER:
306 case IES_RPAREN:
307 case IES_REGISTER:
308 State = IES_AND;
309 IC.pushOperator(IC_AND);
310 break;
311 }
312 PrevState = CurrState;
313 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000314 void onLShift() {
315 IntelExprState CurrState = State;
316 switch (State) {
317 default:
318 State = IES_ERROR;
319 break;
320 case IES_INTEGER:
321 case IES_RPAREN:
322 case IES_REGISTER:
323 State = IES_LSHIFT;
324 IC.pushOperator(IC_LSHIFT);
325 break;
326 }
327 PrevState = CurrState;
328 }
329 void onRShift() {
330 IntelExprState CurrState = State;
331 switch (State) {
332 default:
333 State = IES_ERROR;
334 break;
335 case IES_INTEGER:
336 case IES_RPAREN:
337 case IES_REGISTER:
338 State = IES_RSHIFT;
339 IC.pushOperator(IC_RSHIFT);
340 break;
341 }
342 PrevState = CurrState;
343 }
Chad Rosier5362af92013-04-16 18:15:40 +0000344 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000345 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000346 switch (State) {
347 default:
348 State = IES_ERROR;
349 break;
350 case IES_INTEGER:
351 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000352 case IES_REGISTER:
353 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000354 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000355 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
356 // If we already have a BaseReg, then assume this is the IndexReg with
357 // a scale of 1.
358 if (!BaseReg) {
359 BaseReg = TmpReg;
360 } else {
361 assert (!IndexReg && "BaseReg/IndexReg already set!");
362 IndexReg = TmpReg;
363 Scale = 1;
364 }
365 }
Chad Rosier5362af92013-04-16 18:15:40 +0000366 break;
367 }
Chad Rosier31246272013-04-17 21:01:45 +0000368 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000369 }
370 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000371 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000372 switch (State) {
373 default:
374 State = IES_ERROR;
375 break;
376 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000377 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000378 case IES_MULTIPLY:
379 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000380 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000381 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000382 case IES_LBRAC:
383 case IES_RBRAC:
384 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000385 case IES_REGISTER:
386 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000387 // Only push the minus operator if it is not a unary operator.
388 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
389 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
390 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
391 IC.pushOperator(IC_MINUS);
392 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
393 // If we already have a BaseReg, then assume this is the IndexReg with
394 // a scale of 1.
395 if (!BaseReg) {
396 BaseReg = TmpReg;
397 } else {
398 assert (!IndexReg && "BaseReg/IndexReg already set!");
399 IndexReg = TmpReg;
400 Scale = 1;
401 }
Chad Rosier5362af92013-04-16 18:15:40 +0000402 }
Chad Rosier5362af92013-04-16 18:15:40 +0000403 break;
404 }
Chad Rosier31246272013-04-17 21:01:45 +0000405 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000406 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000407 void onNot() {
408 IntelExprState CurrState = State;
409 switch (State) {
410 default:
411 State = IES_ERROR;
412 break;
413 case IES_PLUS:
414 case IES_NOT:
415 State = IES_NOT;
416 break;
417 }
418 PrevState = CurrState;
419 }
Chad Rosier5362af92013-04-16 18:15:40 +0000420 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000421 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000422 switch (State) {
423 default:
424 State = IES_ERROR;
425 break;
426 case IES_PLUS:
427 case IES_LPAREN:
428 State = IES_REGISTER;
429 TmpReg = Reg;
430 IC.pushOperand(IC_REGISTER);
431 break;
Chad Rosier31246272013-04-17 21:01:45 +0000432 case IES_MULTIPLY:
433 // Index Register - Scale * Register
434 if (PrevState == IES_INTEGER) {
435 assert (!IndexReg && "IndexReg already set!");
436 State = IES_REGISTER;
437 IndexReg = Reg;
438 // Get the scale and replace the 'Scale * Register' with '0'.
439 Scale = IC.popOperand();
440 IC.pushOperand(IC_IMM);
441 IC.popOperator();
442 } else {
443 State = IES_ERROR;
444 }
Chad Rosier5362af92013-04-16 18:15:40 +0000445 break;
446 }
Chad Rosier31246272013-04-17 21:01:45 +0000447 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000448 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000449 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000450 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000451 switch (State) {
452 default:
453 State = IES_ERROR;
454 break;
455 case IES_PLUS:
456 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000457 case IES_NOT:
Chad Rosier5362af92013-04-16 18:15:40 +0000458 State = IES_INTEGER;
459 Sym = SymRef;
460 SymName = SymRefName;
461 IC.pushOperand(IC_IMM);
462 break;
463 }
464 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000465 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000466 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000467 switch (State) {
468 default:
469 State = IES_ERROR;
470 break;
471 case IES_PLUS:
472 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000473 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000474 case IES_OR:
475 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000476 case IES_LSHIFT:
477 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000478 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000479 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000480 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000481 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000482 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
483 // Index Register - Register * Scale
484 assert (!IndexReg && "IndexReg already set!");
485 IndexReg = TmpReg;
486 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000487 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
488 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
489 return true;
490 }
Chad Rosier31246272013-04-17 21:01:45 +0000491 // Get the scale and replace the 'Register * Scale' with '0'.
492 IC.popOperator();
493 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000494 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000495 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosier31246272013-04-17 21:01:45 +0000496 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000497 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
498 PrevState == IES_NOT) &&
Chad Rosier31246272013-04-17 21:01:45 +0000499 CurrState == IES_MINUS) {
500 // Unary minus. No need to pop the minus operand because it was never
501 // pushed.
502 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000503 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
504 PrevState == IES_OR || PrevState == IES_AND ||
505 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
506 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
507 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
508 PrevState == IES_NOT) &&
509 CurrState == IES_NOT) {
510 // Unary not. No need to pop the not operand because it was never
511 // pushed.
512 IC.pushOperand(IC_IMM, ~TmpInt); // Push ~Imm.
Chad Rosier31246272013-04-17 21:01:45 +0000513 } else {
514 IC.pushOperand(IC_IMM, TmpInt);
515 }
Chad Rosier5362af92013-04-16 18:15:40 +0000516 break;
517 }
Chad Rosier31246272013-04-17 21:01:45 +0000518 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000519 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000520 }
521 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000522 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000523 switch (State) {
524 default:
525 State = IES_ERROR;
526 break;
527 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000528 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000529 case IES_RPAREN:
530 State = IES_MULTIPLY;
531 IC.pushOperator(IC_MULTIPLY);
532 break;
533 }
534 }
535 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000536 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000537 switch (State) {
538 default:
539 State = IES_ERROR;
540 break;
541 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000542 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000543 State = IES_DIVIDE;
544 IC.pushOperator(IC_DIVIDE);
545 break;
546 }
547 }
548 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000549 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000550 switch (State) {
551 default:
552 State = IES_ERROR;
553 break;
554 case IES_RBRAC:
555 State = IES_PLUS;
556 IC.pushOperator(IC_PLUS);
557 break;
558 }
559 }
560 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000561 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000562 switch (State) {
563 default:
564 State = IES_ERROR;
565 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000566 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000567 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000568 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000569 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000570 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
571 // If we already have a BaseReg, then assume this is the IndexReg with
572 // a scale of 1.
573 if (!BaseReg) {
574 BaseReg = TmpReg;
575 } else {
576 assert (!IndexReg && "BaseReg/IndexReg already set!");
577 IndexReg = TmpReg;
578 Scale = 1;
579 }
Chad Rosier5362af92013-04-16 18:15:40 +0000580 }
581 break;
582 }
Chad Rosier31246272013-04-17 21:01:45 +0000583 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000584 }
585 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000586 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000587 switch (State) {
588 default:
589 State = IES_ERROR;
590 break;
591 case IES_PLUS:
592 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000593 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000594 case IES_OR:
595 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000596 case IES_LSHIFT:
597 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000598 case IES_MULTIPLY:
599 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000600 case IES_LPAREN:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000601 // FIXME: We don't handle this type of unary minus or not, yet.
Chad Rosierdb003992013-04-18 16:28:19 +0000602 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000603 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000604 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosierdb003992013-04-18 16:28:19 +0000605 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000606 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
607 PrevState == IES_NOT) &&
608 (CurrState == IES_MINUS || CurrState == IES_NOT)) {
Chad Rosierdb003992013-04-18 16:28:19 +0000609 State = IES_ERROR;
610 break;
611 }
Chad Rosier5362af92013-04-16 18:15:40 +0000612 State = IES_LPAREN;
613 IC.pushOperator(IC_LPAREN);
614 break;
615 }
Chad Rosier31246272013-04-17 21:01:45 +0000616 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000617 }
618 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000619 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000620 switch (State) {
621 default:
622 State = IES_ERROR;
623 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000624 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000625 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000626 case IES_RPAREN:
627 State = IES_RPAREN;
628 IC.pushOperator(IC_RPAREN);
629 break;
630 }
631 }
632 };
633
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000634 MCAsmParser &getParser() const { return Parser; }
635
636 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
637
Chris Lattnera3a06812011-10-16 04:47:35 +0000638 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000639 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000640 bool MatchingInlineAsm = false) {
641 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000642 return Parser.Error(L, Msg, Ranges);
643 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000644
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000645 bool ErrorAndEatStatement(SMLoc L, const Twine &Msg,
646 ArrayRef<SMRange> Ranges = None,
647 bool MatchingInlineAsm = false) {
648 Parser.eatToEndOfStatement();
649 return Error(L, Msg, Ranges, MatchingInlineAsm);
650 }
651
David Blaikie960ea3f2014-06-08 16:18:35 +0000652 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000653 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000654 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000655 }
656
David Blaikie960ea3f2014-06-08 16:18:35 +0000657 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
658 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
659 std::unique_ptr<X86Operand> ParseOperand();
660 std::unique_ptr<X86Operand> ParseATTOperand();
661 std::unique_ptr<X86Operand> ParseIntelOperand();
662 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000663 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
David Blaikie960ea3f2014-06-08 16:18:35 +0000664 std::unique_ptr<X86Operand> ParseIntelOperator(unsigned OpKind);
665 std::unique_ptr<X86Operand>
666 ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
667 std::unique_ptr<X86Operand>
668 ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000669 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
David Blaikie960ea3f2014-06-08 16:18:35 +0000670 std::unique_ptr<X86Operand> ParseIntelBracExpression(unsigned SegReg,
671 SMLoc Start,
672 int64_t ImmDisp,
673 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000674 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
675 InlineAsmIdentifierInfo &Info,
676 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000677
David Blaikie960ea3f2014-06-08 16:18:35 +0000678 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000679
David Blaikie960ea3f2014-06-08 16:18:35 +0000680 std::unique_ptr<X86Operand>
681 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
682 unsigned IndexReg, unsigned Scale, SMLoc Start,
683 SMLoc End, unsigned Size, StringRef Identifier,
684 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000685
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000686 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000687 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000688
David Blaikie960ea3f2014-06-08 16:18:35 +0000689 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000690
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000691 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
692 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000693 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000694
Chad Rosier49963552012-10-13 00:26:04 +0000695 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000696 OperandVector &Operands, MCStreamer &Out,
697 unsigned &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000698 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000699
Nico Weber42f79db2014-07-17 20:24:55 +0000700 virtual bool OmitRegisterFromClobberLists(unsigned RegNo) override;
701
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000702 /// doSrcDstMatch - Returns true if operands are matching in their
703 /// word size (%si and %di, %esi and %edi, etc.). Order depends on
704 /// the parsing mode (Intel vs. AT&T).
705 bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
706
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000707 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
708 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
709 /// \return \c true if no parsing errors occurred, \c false otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000710 bool HandleAVX512Operand(OperandVector &Operands,
711 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000712
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000713 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000714 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000715 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000716 }
Craig Topper3c80d622014-01-06 04:55:54 +0000717 bool is32BitMode() const {
718 // FIXME: Can tablegen auto-generate this?
719 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
720 }
721 bool is16BitMode() const {
722 // FIXME: Can tablegen auto-generate this?
723 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
724 }
725 void SwitchMode(uint64_t mode) {
726 uint64_t oldMode = STI.getFeatureBits() &
727 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
728 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000729 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000730 assert(mode == (STI.getFeatureBits() &
731 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000732 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000733
Reid Kleckner5b37c182014-08-01 20:21:24 +0000734 unsigned getPointerWidth() {
735 if (is16BitMode()) return 16;
736 if (is32BitMode()) return 32;
737 if (is64BitMode()) return 64;
738 llvm_unreachable("invalid mode");
739 }
740
Chad Rosierc2f055d2013-04-18 16:13:18 +0000741 bool isParsingIntelSyntax() {
742 return getParser().getAssemblerDialect();
743 }
744
Daniel Dunbareefe8612010-07-19 05:44:09 +0000745 /// @name Auto-generated Matcher Functions
746 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000747
Chris Lattner3e4582a2010-09-06 19:11:01 +0000748#define GET_ASSEMBLER_HEADER
749#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000750
Daniel Dunbar00331992009-07-29 00:02:19 +0000751 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000752
753public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000754 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +0000755 const MCInstrInfo &mii,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000756 const MCTargetOptions &Options)
Craig Topper062a2ba2014-04-25 05:30:21 +0000757 : MCTargetAsmParser(), STI(sti), Parser(parser), MII(mii),
758 InstInfo(nullptr) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000759
Daniel Dunbareefe8612010-07-19 05:44:09 +0000760 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000761 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000762 Instrumentation.reset(
763 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000764 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000765
Craig Topper39012cc2014-03-09 18:03:14 +0000766 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000767
David Blaikie960ea3f2014-06-08 16:18:35 +0000768 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
769 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000770
Craig Topper39012cc2014-03-09 18:03:14 +0000771 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000772};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000773} // end anonymous namespace
774
Sean Callanan86c11812010-01-23 00:40:33 +0000775/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000776/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000777
Chris Lattner60db0a62010-02-09 00:34:28 +0000778static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000779
780/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000781
Kevin Enderbybc570f22014-01-23 22:34:42 +0000782static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
783 StringRef &ErrMsg) {
784 // If we have both a base register and an index register make sure they are
785 // both 64-bit or 32-bit registers.
786 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
787 if (BaseReg != 0 && IndexReg != 0) {
788 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
789 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
790 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
791 IndexReg != X86::RIZ) {
792 ErrMsg = "base register is 64-bit, but index register is not";
793 return true;
794 }
795 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
796 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
797 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
798 IndexReg != X86::EIZ){
799 ErrMsg = "base register is 32-bit, but index register is not";
800 return true;
801 }
802 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
803 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
804 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
805 ErrMsg = "base register is 16-bit, but index register is not";
806 return true;
807 }
808 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
809 IndexReg != X86::SI && IndexReg != X86::DI) ||
810 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
811 IndexReg != X86::BX && IndexReg != X86::BP)) {
812 ErrMsg = "invalid 16-bit base/index register combination";
813 return true;
814 }
815 }
816 }
817 return false;
818}
819
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000820bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
821{
822 // Return true and let a normal complaint about bogus operands happen.
823 if (!Op1.isMem() || !Op2.isMem())
824 return true;
825
826 // Actually these might be the other way round if Intel syntax is
827 // being used. It doesn't matter.
828 unsigned diReg = Op1.Mem.BaseReg;
829 unsigned siReg = Op2.Mem.BaseReg;
830
831 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
832 return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
833 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
834 return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
835 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
836 return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
837 // Again, return true and let another error happen.
838 return true;
839}
840
Devang Patel4a6e7782012-01-12 18:03:40 +0000841bool X86AsmParser::ParseRegister(unsigned &RegNo,
842 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000843 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000844 const AsmToken &PercentTok = Parser.getTok();
845 StartLoc = PercentTok.getLoc();
846
847 // If we encounter a %, ignore it. This code handles registers with and
848 // without the prefix, unprefixed registers can occur in cfi directives.
849 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000850 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000851
Sean Callanan936b0d32010-01-19 21:44:56 +0000852 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000853 EndLoc = Tok.getEndLoc();
854
Devang Patelce6a2ca2012-01-20 22:32:05 +0000855 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000856 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000857 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000858 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000859 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000860
Kevin Enderby7d912182009-09-03 17:15:07 +0000861 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000862
Chris Lattner1261b812010-09-22 04:11:10 +0000863 // If the match failed, try the register name as lowercase.
864 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000865 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000866
Evan Chengeda1d4f2011-07-27 23:22:03 +0000867 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000868 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000869 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
870 // checked.
871 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
872 // REX prefix.
873 if (RegNo == X86::RIZ ||
874 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
875 X86II::isX86_64NonExtLowByteReg(RegNo) ||
876 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000877 return Error(StartLoc, "register %"
878 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000879 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000880 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000881
Chris Lattner1261b812010-09-22 04:11:10 +0000882 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
883 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000884 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000885 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000886
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000887 // Check to see if we have '(4)' after %st.
888 if (getLexer().isNot(AsmToken::LParen))
889 return false;
890 // Lex the paren.
891 getParser().Lex();
892
893 const AsmToken &IntTok = Parser.getTok();
894 if (IntTok.isNot(AsmToken::Integer))
895 return Error(IntTok.getLoc(), "expected stack index");
896 switch (IntTok.getIntVal()) {
897 case 0: RegNo = X86::ST0; break;
898 case 1: RegNo = X86::ST1; break;
899 case 2: RegNo = X86::ST2; break;
900 case 3: RegNo = X86::ST3; break;
901 case 4: RegNo = X86::ST4; break;
902 case 5: RegNo = X86::ST5; break;
903 case 6: RegNo = X86::ST6; break;
904 case 7: RegNo = X86::ST7; break;
905 default: return Error(IntTok.getLoc(), "invalid stack index");
906 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000907
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000908 if (getParser().Lex().isNot(AsmToken::RParen))
909 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000910
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000911 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000912 Parser.Lex(); // Eat ')'
913 return false;
914 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000915
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000916 EndLoc = Parser.getTok().getEndLoc();
917
Chris Lattner80486622010-06-24 07:29:18 +0000918 // If this is "db[0-7]", match it as an alias
919 // for dr[0-7].
920 if (RegNo == 0 && Tok.getString().size() == 3 &&
921 Tok.getString().startswith("db")) {
922 switch (Tok.getString()[2]) {
923 case '0': RegNo = X86::DR0; break;
924 case '1': RegNo = X86::DR1; break;
925 case '2': RegNo = X86::DR2; break;
926 case '3': RegNo = X86::DR3; break;
927 case '4': RegNo = X86::DR4; break;
928 case '5': RegNo = X86::DR5; break;
929 case '6': RegNo = X86::DR6; break;
930 case '7': RegNo = X86::DR7; break;
931 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000932
Chris Lattner80486622010-06-24 07:29:18 +0000933 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000934 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000935 Parser.Lex(); // Eat it.
936 return false;
937 }
938 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000939
Devang Patelce6a2ca2012-01-20 22:32:05 +0000940 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000941 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000942 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000943 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000944 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000945
Sean Callanana83fd7d2010-01-19 20:27:46 +0000946 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000947 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000948}
949
David Blaikie960ea3f2014-06-08 16:18:35 +0000950std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000951 unsigned basereg =
952 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
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::DefaultMemDIOperand(SMLoc Loc) {
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000959 unsigned basereg =
960 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
961 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
962 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
963 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
964}
965
David Blaikie960ea3f2014-06-08 16:18:35 +0000966std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000967 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000968 return ParseIntelOperand();
969 return ParseATTOperand();
970}
971
Devang Patel41b9dde2012-01-17 18:00:18 +0000972/// getIntelMemOperandSize - Return intel memory operand size.
973static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000974 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000975 .Cases("BYTE", "byte", 8)
976 .Cases("WORD", "word", 16)
977 .Cases("DWORD", "dword", 32)
978 .Cases("QWORD", "qword", 64)
979 .Cases("XWORD", "xword", 80)
980 .Cases("XMMWORD", "xmmword", 128)
981 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +0000982 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +0000983 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +0000984 .Default(0);
985 return Size;
Devang Patel46831de2012-01-12 01:36:43 +0000986}
987
David Blaikie960ea3f2014-06-08 16:18:35 +0000988std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
989 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
990 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
991 InlineAsmIdentifierInfo &Info) {
Reid Kleckner5b37c182014-08-01 20:21:24 +0000992 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
993 // some other label reference.
994 if (isa<MCSymbolRefExpr>(Disp) && Info.OpDecl && !Info.IsVarDecl) {
995 // Insert an explicit size if the user didn't have one.
996 if (!Size) {
997 Size = getPointerWidth();
998 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
999 /*Len=*/0, Size));
1000 }
1001
1002 // Create an absolute memory reference in order to match against
1003 // instructions taking a PC relative operand.
1004 return X86Operand::CreateMem(Disp, Start, End, Size, Identifier,
1005 Info.OpDecl);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001006 }
1007
1008 // We either have a direct symbol reference, or an offset from a symbol. The
1009 // parser always puts the symbol on the LHS, so look there for size
1010 // calculation purposes.
1011 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
1012 bool IsSymRef =
1013 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
1014 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001015 if (!Size) {
1016 Size = Info.Type * 8; // Size is in terms of bits in this context.
1017 if (Size)
1018 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1019 /*Len=*/0, Size));
1020 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001021 }
1022
Chad Rosier7ca135b2013-03-19 21:11:56 +00001023 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001024 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001025 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001026 BaseReg = BaseReg ? BaseReg : 1;
1027 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001028 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001029}
1030
Chad Rosierd383db52013-04-12 20:20:54 +00001031static void
1032RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1033 StringRef SymName, int64_t ImmDisp,
1034 int64_t FinalImmDisp, SMLoc &BracLoc,
1035 SMLoc &StartInBrac, SMLoc &End) {
1036 // Remove the '[' and ']' from the IR string.
1037 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1038 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1039
1040 // If ImmDisp is non-zero, then we parsed a displacement before the
1041 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1042 // If ImmDisp doesn't match the displacement computed by the state machine
1043 // then we have an additional displacement in the bracketed expression.
1044 if (ImmDisp != FinalImmDisp) {
1045 if (ImmDisp) {
1046 // We have an immediate displacement before the bracketed expression.
1047 // Adjust this to match the final immediate displacement.
1048 bool Found = false;
1049 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1050 E = AsmRewrites->end(); I != E; ++I) {
1051 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1052 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001053 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1054 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001055 (*I).Kind = AOK_Imm;
1056 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1057 (*I).Val = FinalImmDisp;
1058 Found = true;
1059 break;
1060 }
1061 }
1062 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001063 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001064 } else {
1065 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001066 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001067 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001068 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001069 }
1070 }
1071 // Remove all the ImmPrefix rewrites within the brackets.
1072 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1073 E = AsmRewrites->end(); I != E; ++I) {
1074 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1075 continue;
1076 if ((*I).Kind == AOK_ImmPrefix)
1077 (*I).Kind = AOK_Delete;
1078 }
1079 const char *SymLocPtr = SymName.data();
1080 // Skip everything before the symbol.
1081 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1082 assert(Len > 0 && "Expected a non-negative length.");
1083 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1084 }
1085 // Skip everything after the symbol.
1086 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1087 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1088 assert(Len > 0 && "Expected a non-negative length.");
1089 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1090 }
1091}
1092
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001093bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001094 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001095
Chad Rosier5c118fd2013-01-14 22:31:35 +00001096 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001097 while (!Done) {
1098 bool UpdateLocLex = true;
1099
1100 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1101 // identifier. Don't try an parse it as a register.
1102 if (Tok.getString().startswith("."))
1103 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001104
1105 // If we're parsing an immediate expression, we don't expect a '['.
1106 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1107 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001108
David Majnemer6a5b8122014-06-19 01:25:43 +00001109 AsmToken::TokenKind TK = getLexer().getKind();
1110 switch (TK) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001111 default: {
1112 if (SM.isValidEndState()) {
1113 Done = true;
1114 break;
1115 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001116 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001117 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001118 case AsmToken::EndOfStatement: {
1119 Done = true;
1120 break;
1121 }
David Majnemer6a5b8122014-06-19 01:25:43 +00001122 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001123 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001124 // This could be a register or a symbolic displacement.
1125 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001126 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001127 SMLoc IdentLoc = Tok.getLoc();
1128 StringRef Identifier = Tok.getString();
David Majnemer6a5b8122014-06-19 01:25:43 +00001129 if (TK != AsmToken::String && !ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001130 SM.onRegister(TmpReg);
1131 UpdateLocLex = false;
1132 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001133 } else {
1134 if (!isParsingInlineAsm()) {
1135 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001136 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001137 } else {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001138 // This is a dot operator, not an adjacent identifier.
1139 if (Identifier.find('.') != StringRef::npos) {
1140 return false;
1141 } else {
1142 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1143 if (ParseIntelIdentifier(Val, Identifier, Info,
1144 /*Unevaluated=*/false, End))
1145 return true;
1146 }
Chad Rosier95ce8892013-04-19 18:39:50 +00001147 }
1148 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001149 UpdateLocLex = false;
1150 break;
1151 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001152 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001153 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001154 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001155 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001156 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001157 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1158 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001159 // Look for 'b' or 'f' following an Integer as a directional label
1160 SMLoc Loc = getTok().getLoc();
1161 int64_t IntVal = getTok().getIntVal();
1162 End = consumeToken();
1163 UpdateLocLex = false;
1164 if (getLexer().getKind() == AsmToken::Identifier) {
1165 StringRef IDVal = getTok().getString();
1166 if (IDVal == "f" || IDVal == "b") {
1167 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +00001168 getContext().GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001169 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1170 const MCExpr *Val =
1171 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1172 if (IDVal == "b" && Sym->isUndefined())
1173 return Error(Loc, "invalid reference to undefined symbol");
1174 StringRef Identifier = Sym->getName();
1175 SM.onIdentifierExpr(Val, Identifier);
1176 End = consumeToken();
1177 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001178 if (SM.onInteger(IntVal, ErrMsg))
1179 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001180 }
1181 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001182 if (SM.onInteger(IntVal, ErrMsg))
1183 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001184 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001185 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001186 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001187 case AsmToken::Plus: SM.onPlus(); break;
1188 case AsmToken::Minus: SM.onMinus(); break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001189 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001190 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001191 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001192 case AsmToken::Pipe: SM.onOr(); break;
1193 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001194 case AsmToken::LessLess:
1195 SM.onLShift(); break;
1196 case AsmToken::GreaterGreater:
1197 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001198 case AsmToken::LBrac: SM.onLBrac(); break;
1199 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001200 case AsmToken::LParen: SM.onLParen(); break;
1201 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001202 }
Chad Rosier31246272013-04-17 21:01:45 +00001203 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001204 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001205
Alp Tokera5b88a52013-12-02 16:06:06 +00001206 if (!Done && UpdateLocLex)
1207 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001208 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001209 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001210}
1211
David Blaikie960ea3f2014-06-08 16:18:35 +00001212std::unique_ptr<X86Operand>
1213X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
1214 int64_t ImmDisp, unsigned Size) {
Chad Rosier5362af92013-04-16 18:15:40 +00001215 const AsmToken &Tok = Parser.getTok();
1216 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1217 if (getLexer().isNot(AsmToken::LBrac))
1218 return ErrorOperand(BracLoc, "Expected '[' token!");
1219 Parser.Lex(); // Eat '['
1220
1221 SMLoc StartInBrac = Tok.getLoc();
1222 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1223 // may have already parsed an immediate displacement before the bracketed
1224 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001225 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001226 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001227 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +00001228
Craig Topper062a2ba2014-04-25 05:30:21 +00001229 const MCExpr *Disp = nullptr;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001230 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001231 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001232 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001233 if (isParsingInlineAsm())
1234 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001235 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001236 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001237 }
1238
1239 if (SM.getImm() || !Disp) {
1240 const MCExpr *Imm = MCConstantExpr::Create(SM.getImm(), getContext());
1241 if (Disp)
1242 Disp = MCBinaryExpr::CreateAdd(Disp, Imm, getContext());
1243 else
1244 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001245 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001246
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001247 // Parse struct field access. Intel requires a dot, but MSVC doesn't. MSVC
1248 // will in fact do global lookup the field name inside all global typedefs,
1249 // but we don't emulate that.
1250 if (Tok.getString().find('.') != StringRef::npos) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001251 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001252 if (ParseIntelDotOperator(Disp, NewDisp))
Craig Topper062a2ba2014-04-25 05:30:21 +00001253 return nullptr;
Chad Rosier911c1f32012-10-25 17:37:43 +00001254
Chad Rosier70f47592013-04-10 20:07:47 +00001255 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001256 Parser.Lex(); // Eat the field.
1257 Disp = NewDisp;
1258 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001259
Chad Rosier5c118fd2013-01-14 22:31:35 +00001260 int BaseReg = SM.getBaseReg();
1261 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001262 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001263 if (!isParsingInlineAsm()) {
1264 // handle [-42]
1265 if (!BaseReg && !IndexReg) {
1266 if (!SegReg)
1267 return X86Operand::CreateMem(Disp, Start, End, Size);
1268 else
1269 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1270 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001271 StringRef ErrMsg;
1272 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1273 Error(StartInBrac, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001274 return nullptr;
Kevin Enderbybc570f22014-01-23 22:34:42 +00001275 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001276 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1277 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001278 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001279
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001280 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001281 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001282 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001283}
1284
Chad Rosier8a244662013-04-02 20:02:33 +00001285// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001286bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1287 StringRef &Identifier,
1288 InlineAsmIdentifierInfo &Info,
1289 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001290 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001291 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001292
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001293 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001294 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001295
Chad Rosier8a244662013-04-02 20:02:33 +00001296 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001297
1298 // Advance the token stream until the end of the current token is
1299 // after the end of what the frontend claimed.
1300 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1301 while (true) {
1302 End = Tok.getEndLoc();
1303 getLexer().Lex();
1304
1305 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1306 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001307 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001308
1309 // Create the symbol reference.
1310 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001311 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1312 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001313 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001314 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001315}
1316
David Majnemeraa34d792013-08-27 21:56:17 +00001317/// \brief Parse intel style segment override.
David Blaikie960ea3f2014-06-08 16:18:35 +00001318std::unique_ptr<X86Operand>
1319X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start,
1320 unsigned Size) {
David Majnemeraa34d792013-08-27 21:56:17 +00001321 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1322 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1323 if (Tok.isNot(AsmToken::Colon))
1324 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1325 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001326
David Majnemeraa34d792013-08-27 21:56:17 +00001327 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001328 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001329 ImmDisp = Tok.getIntVal();
1330 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1331
Chad Rosier1530ba52013-03-27 21:49:56 +00001332 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001333 InstInfo->AsmRewrites->push_back(
1334 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1335
1336 if (getLexer().isNot(AsmToken::LBrac)) {
1337 // An immediate following a 'segment register', 'colon' token sequence can
1338 // be followed by a bracketed expression. If it isn't we know we have our
1339 // final segment override.
1340 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1341 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1342 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1343 Size);
1344 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001345 }
1346
Chad Rosier91c82662012-10-24 17:22:29 +00001347 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001348 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001349
David Majnemeraa34d792013-08-27 21:56:17 +00001350 const MCExpr *Val;
1351 SMLoc End;
1352 if (!isParsingInlineAsm()) {
1353 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001354 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001355
1356 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001357 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001358
David Majnemeraa34d792013-08-27 21:56:17 +00001359 InlineAsmIdentifierInfo Info;
1360 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001361 if (ParseIntelIdentifier(Val, Identifier, Info,
1362 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001363 return nullptr;
David Majnemeraa34d792013-08-27 21:56:17 +00001364 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1365 /*Scale=*/1, Start, End, Size, Identifier, Info);
1366}
1367
1368/// ParseIntelMemOperand - Parse intel style memory operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00001369std::unique_ptr<X86Operand> X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp,
1370 SMLoc Start,
1371 unsigned Size) {
David Majnemeraa34d792013-08-27 21:56:17 +00001372 const AsmToken &Tok = Parser.getTok();
1373 SMLoc End;
1374
1375 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1376 if (getLexer().is(AsmToken::LBrac))
1377 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001378 assert(ImmDisp == 0);
David Majnemeraa34d792013-08-27 21:56:17 +00001379
Chad Rosier95ce8892013-04-19 18:39:50 +00001380 const MCExpr *Val;
1381 if (!isParsingInlineAsm()) {
1382 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001383 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001384
1385 return X86Operand::CreateMem(Val, Start, End, Size);
1386 }
1387
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001388 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001389 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001390 if (ParseIntelIdentifier(Val, Identifier, Info,
1391 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001392 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001393
1394 if (!getLexer().is(AsmToken::LBrac))
1395 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
1396 /*Scale=*/1, Start, End, Size, Identifier, Info);
1397
1398 Parser.Lex(); // Eat '['
1399
1400 // Parse Identifier [ ImmDisp ]
1401 IntelExprStateMachine SM(/*ImmDisp=*/0, /*StopOnLBrac=*/true,
1402 /*AddImmPrefix=*/false);
1403 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001404 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001405
1406 if (SM.getSym()) {
1407 Error(Start, "cannot use more than one symbol in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001408 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001409 }
1410 if (SM.getBaseReg()) {
1411 Error(Start, "cannot use base register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001412 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001413 }
1414 if (SM.getIndexReg()) {
1415 Error(Start, "cannot use index register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001416 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001417 }
1418
1419 const MCExpr *Disp = MCConstantExpr::Create(SM.getImm(), getContext());
1420 // BaseReg is non-zero to avoid assertions. In the context of inline asm,
1421 // we're pointing to a local variable in memory, so the base register is
1422 // really the frame or stack pointer.
1423 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/1, /*IndexReg=*/0,
1424 /*Scale=*/1, Start, End, Size, Identifier,
1425 Info.OpDecl);
Chad Rosier91c82662012-10-24 17:22:29 +00001426}
1427
Chad Rosier5dcb4662012-10-24 22:21:50 +00001428/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001429bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001430 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001431 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001432 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001433
1434 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001435 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001436 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001437 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001438 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001439
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001440 // Drop the optional '.'.
1441 StringRef DotDispStr = Tok.getString();
1442 if (DotDispStr.startswith("."))
1443 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001444
Chad Rosier5dcb4662012-10-24 22:21:50 +00001445 // .Imm gets lexed as a real.
1446 if (Tok.is(AsmToken::Real)) {
1447 APInt DotDisp;
1448 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001449 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001450 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001451 unsigned DotDisp;
1452 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1453 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001454 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001455 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001456 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001457 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001458 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001459
Chad Rosier240b7b92012-10-25 21:51:10 +00001460 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1461 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1462 unsigned Len = DotDispStr.size();
1463 unsigned Val = OrigDispVal + DotDispVal;
1464 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1465 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001466 }
1467
Chad Rosiercc541e82013-04-19 15:57:00 +00001468 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001469 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001470}
1471
Chad Rosier91c82662012-10-24 17:22:29 +00001472/// Parse the 'offset' operator. This operator is used to specify the
1473/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001474std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001475 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001476 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001477 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001478
Chad Rosier91c82662012-10-24 17:22:29 +00001479 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001480 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001481 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001482 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001483 if (ParseIntelIdentifier(Val, Identifier, Info,
1484 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001485 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001486
Chad Rosiere2f03772012-10-26 16:09:20 +00001487 // Don't emit the offset operator.
1488 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1489
Chad Rosier91c82662012-10-24 17:22:29 +00001490 // The offset operator will have an 'r' constraint, thus we need to create
1491 // register operand to ensure proper matching. Just pick a GPR based on
1492 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001493 unsigned RegNo =
1494 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001495 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001496 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001497}
1498
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001499enum IntelOperatorKind {
1500 IOK_LENGTH,
1501 IOK_SIZE,
1502 IOK_TYPE
1503};
1504
1505/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1506/// returns the number of elements in an array. It returns the value 1 for
1507/// non-array variables. The SIZE operator returns the size of a C or C++
1508/// variable. A variable's size is the product of its LENGTH and TYPE. The
1509/// TYPE operator returns the size of a C or C++ type or variable. If the
1510/// variable is an array, TYPE returns the size of a single element.
David Blaikie960ea3f2014-06-08 16:18:35 +00001511std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001512 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001513 SMLoc TypeLoc = Tok.getLoc();
1514 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001515
Craig Topper062a2ba2014-04-25 05:30:21 +00001516 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001517 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001518 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001519 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001520 if (ParseIntelIdentifier(Val, Identifier, Info,
1521 /*Unevaluated=*/true, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001522 return nullptr;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001523
1524 if (!Info.OpDecl)
1525 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001526
Chad Rosierf6675c32013-04-22 17:01:46 +00001527 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001528 switch(OpKind) {
1529 default: llvm_unreachable("Unexpected operand kind!");
1530 case IOK_LENGTH: CVal = Info.Length; break;
1531 case IOK_SIZE: CVal = Info.Size; break;
1532 case IOK_TYPE: CVal = Info.Type; break;
1533 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001534
1535 // Rewrite the type operator and the C or C++ type or variable in terms of an
1536 // immediate. E.g. TYPE foo -> $$4
1537 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001538 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001539
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001540 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001541 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001542}
1543
David Blaikie960ea3f2014-06-08 16:18:35 +00001544std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001545 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001546 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001547
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001548 // Offset, length, type and size operators.
1549 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001550 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001551 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001552 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001553 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001554 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001555 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001556 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001557 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001558 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001559 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001560
David Majnemeraa34d792013-08-27 21:56:17 +00001561 unsigned Size = getIntelMemOperandSize(Tok.getString());
1562 if (Size) {
1563 Parser.Lex(); // Eat operand size (e.g., byte, word).
1564 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
Reid Kleckner71ff3f22014-08-01 00:59:22 +00001565 return ErrorOperand(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
David Majnemeraa34d792013-08-27 21:56:17 +00001566 Parser.Lex(); // Eat ptr.
1567 }
1568 Start = Tok.getLoc();
1569
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001570 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001571 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001572 getLexer().is(AsmToken::Tilde) || getLexer().is(AsmToken::LParen)) {
Chad Rosierbfb70992013-04-17 00:11:46 +00001573 AsmToken StartTok = Tok;
1574 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1575 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001576 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001577 return nullptr;
Chad Rosierbfb70992013-04-17 00:11:46 +00001578
1579 int64_t Imm = SM.getImm();
1580 if (isParsingInlineAsm()) {
1581 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1582 if (StartTok.getString().size() == Len)
1583 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001584 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001585 else
1586 // Otherwise, rewrite the complex expression as a single immediate.
1587 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001588 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001589
1590 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001591 // If a directional label (ie. 1f or 2b) was parsed above from
1592 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1593 // to the MCExpr with the directional local symbol and this is a
1594 // memory operand not an immediate operand.
1595 if (SM.getSym())
1596 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1597
Chad Rosierbfb70992013-04-17 00:11:46 +00001598 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1599 return X86Operand::CreateImm(ImmExpr, Start, End);
1600 }
1601
1602 // Only positive immediates are valid.
1603 if (Imm < 0)
1604 return ErrorOperand(Start, "expected a positive immediate displacement "
1605 "before bracketed expr.");
1606
1607 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001608 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001609 }
1610
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001611 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001612 unsigned RegNo = 0;
1613 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001614 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001615 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001616 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001617 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001618
David Majnemeraa34d792013-08-27 21:56:17 +00001619 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001620 }
1621
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001622 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001623 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001624}
1625
David Blaikie960ea3f2014-06-08 16:18:35 +00001626std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001627 switch (getLexer().getKind()) {
1628 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001629 // Parse a memory operand with no segment register.
1630 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001631 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001632 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001633 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001634 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001635 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001636 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001637 Error(Start, "%eiz and %riz can only be used as index registers",
1638 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001639 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001640 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001641
Chris Lattnerb9270732010-04-17 18:56:34 +00001642 // If this is a segment register followed by a ':', then this is the start
1643 // of a memory reference, otherwise this is a normal register reference.
1644 if (getLexer().isNot(AsmToken::Colon))
1645 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001646
Reid Kleckner0c5da972014-07-31 23:03:22 +00001647 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1648 return ErrorOperand(Start, "invalid segment register");
1649
Chris Lattnerb9270732010-04-17 18:56:34 +00001650 getParser().Lex(); // Eat the colon.
1651 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001652 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001653 case AsmToken::Dollar: {
1654 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001655 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001656 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001657 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001658 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001659 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001660 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001661 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001662 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001663}
1664
David Blaikie960ea3f2014-06-08 16:18:35 +00001665bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1666 const MCParsedAsmOperand &Op) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001667 if(STI.getFeatureBits() & X86::FeatureAVX512) {
1668 if (getLexer().is(AsmToken::LCurly)) {
1669 // Eat "{" and mark the current place.
1670 const SMLoc consumedToken = consumeToken();
1671 // Distinguish {1to<NUM>} from {%k<NUM>}.
1672 if(getLexer().is(AsmToken::Integer)) {
1673 // Parse memory broadcasting ({1to<NUM>}).
1674 if (getLexer().getTok().getIntVal() != 1)
1675 return !ErrorAndEatStatement(getLexer().getLoc(),
1676 "Expected 1to<NUM> at this point");
1677 Parser.Lex(); // Eat "1" of 1to8
1678 if (!getLexer().is(AsmToken::Identifier) ||
1679 !getLexer().getTok().getIdentifier().startswith("to"))
1680 return !ErrorAndEatStatement(getLexer().getLoc(),
1681 "Expected 1to<NUM> at this point");
1682 // Recognize only reasonable suffixes.
1683 const char *BroadcastPrimitive =
1684 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
Robert Khasanovbfa01312014-07-21 14:54:21 +00001685 .Case("to2", "{1to2}")
1686 .Case("to4", "{1to4}")
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001687 .Case("to8", "{1to8}")
1688 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001689 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001690 if (!BroadcastPrimitive)
1691 return !ErrorAndEatStatement(getLexer().getLoc(),
1692 "Invalid memory broadcast primitive.");
1693 Parser.Lex(); // Eat "toN" of 1toN
1694 if (!getLexer().is(AsmToken::RCurly))
1695 return !ErrorAndEatStatement(getLexer().getLoc(),
1696 "Expected } at this point");
1697 Parser.Lex(); // Eat "}"
1698 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1699 consumedToken));
1700 // No AVX512 specific primitives can pass
1701 // after memory broadcasting, so return.
1702 return true;
1703 } else {
1704 // Parse mask register {%k1}
1705 Operands.push_back(X86Operand::CreateToken("{", consumedToken));
David Blaikie960ea3f2014-06-08 16:18:35 +00001706 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1707 Operands.push_back(std::move(Op));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001708 if (!getLexer().is(AsmToken::RCurly))
1709 return !ErrorAndEatStatement(getLexer().getLoc(),
1710 "Expected } at this point");
1711 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1712
1713 // Parse "zeroing non-masked" semantic {z}
1714 if (getLexer().is(AsmToken::LCurly)) {
1715 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1716 if (!getLexer().is(AsmToken::Identifier) ||
1717 getLexer().getTok().getIdentifier() != "z")
1718 return !ErrorAndEatStatement(getLexer().getLoc(),
1719 "Expected z at this point");
1720 Parser.Lex(); // Eat the z
1721 if (!getLexer().is(AsmToken::RCurly))
1722 return !ErrorAndEatStatement(getLexer().getLoc(),
1723 "Expected } at this point");
1724 Parser.Lex(); // Eat the }
1725 }
1726 }
1727 }
1728 }
1729 }
1730 return true;
1731}
1732
Chris Lattnerb9270732010-04-17 18:56:34 +00001733/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1734/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00001735std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
1736 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001737
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001738 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1739 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001740 // only way to do this without lookahead is to eat the '(' and see what is
1741 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001742 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001743 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001744 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00001745 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001746
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001747 // After parsing the base expression we could either have a parenthesized
1748 // memory address or not. If not, return now. If so, eat the (.
1749 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001750 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001751 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001752 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001753 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001754 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001755
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001756 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001757 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001758 } else {
1759 // Okay, we have a '('. We don't know if this is an expression or not, but
1760 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001761 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001762 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001763
Kevin Enderby7d912182009-09-03 17:15:07 +00001764 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001765 // Nothing to do here, fall into the code below with the '(' part of the
1766 // memory operand consumed.
1767 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001768 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001769
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001770 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001771 if (getParser().parseParenExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00001772 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001773
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001774 // After parsing the base expression we could either have a parenthesized
1775 // memory address or not. If not, return now. If so, eat the (.
1776 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001777 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001778 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001779 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001780 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001781 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001782
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001783 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001784 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001785 }
1786 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001787
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001788 // If we reached here, then we just ate the ( of the memory operand. Process
1789 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001790 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001791 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001792
Chris Lattner0c2538f2010-01-15 18:51:29 +00001793 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001794 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001795 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00001796 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001797 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001798 Error(StartLoc, "eiz and riz can only be used as index registers",
1799 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00001800 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001801 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001802 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001803
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001804 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001805 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001806 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001807
1808 // Following the comma we should have either an index register, or a scale
1809 // value. We don't support the later form, but we want to parse it
1810 // correctly.
1811 //
1812 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001813 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001814 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001815 SMLoc L;
Craig Topper062a2ba2014-04-25 05:30:21 +00001816 if (ParseRegister(IndexReg, L, L)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001817
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001818 if (getLexer().isNot(AsmToken::RParen)) {
1819 // Parse the scale amount:
1820 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001821 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001822 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001823 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001824 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001825 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001826 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001827
1828 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001829 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001830
1831 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001832 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001833 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001834 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001835 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001836
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001837 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001838 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1839 ScaleVal != 1) {
1840 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00001841 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001842 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001843 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1844 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00001845 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001846 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001847 Scale = (unsigned)ScaleVal;
1848 }
1849 }
1850 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001851 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001852 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001853 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001854
1855 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001856 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00001857 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001858
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001859 if (Value != 1)
1860 Warning(Loc, "scale factor without index register is ignored");
1861 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001862 }
1863 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001864
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001865 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001866 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001867 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001868 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001869 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001870 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001871 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001872
David Woodhouse6dbda442014-01-08 12:58:28 +00001873 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1874 // and then only in non-64-bit modes. Except for DX, which is a special case
1875 // because an unofficial form of in/out instructions uses it.
1876 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1877 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1878 BaseReg != X86::SI && BaseReg != X86::DI)) &&
1879 BaseReg != X86::DX) {
1880 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001881 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001882 }
1883 if (BaseReg == 0 &&
1884 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
1885 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001886 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001887 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001888
1889 StringRef ErrMsg;
1890 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1891 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001892 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001893 }
1894
Reid Klecknerb7e2f602014-07-31 23:26:35 +00001895 if (SegReg || BaseReg || IndexReg)
1896 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1897 MemStart, MemEnd);
1898 return X86Operand::CreateMem(Disp, MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001899}
1900
David Blaikie960ea3f2014-06-08 16:18:35 +00001901bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
1902 SMLoc NameLoc, OperandVector &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001903 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001904 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001905
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001906 // FIXME: Hack to recognize setneb as setne.
1907 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1908 PatchedName != "setb" && PatchedName != "setnb")
1909 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001910
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001911 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Craig Topper062a2ba2014-04-25 05:30:21 +00001912 const MCExpr *ExtraImmOp = nullptr;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001913 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001914 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1915 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001916 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001917 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001918 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001919 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001920 .Case("eq", 0x00)
1921 .Case("lt", 0x01)
1922 .Case("le", 0x02)
1923 .Case("unord", 0x03)
1924 .Case("neq", 0x04)
1925 .Case("nlt", 0x05)
1926 .Case("nle", 0x06)
1927 .Case("ord", 0x07)
1928 /* AVX only from here */
1929 .Case("eq_uq", 0x08)
1930 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001931 .Case("ngt", 0x0A)
1932 .Case("false", 0x0B)
1933 .Case("neq_oq", 0x0C)
1934 .Case("ge", 0x0D)
1935 .Case("gt", 0x0E)
1936 .Case("true", 0x0F)
1937 .Case("eq_os", 0x10)
1938 .Case("lt_oq", 0x11)
1939 .Case("le_oq", 0x12)
1940 .Case("unord_s", 0x13)
1941 .Case("neq_us", 0x14)
1942 .Case("nlt_uq", 0x15)
1943 .Case("nle_uq", 0x16)
1944 .Case("ord_s", 0x17)
1945 .Case("eq_us", 0x18)
1946 .Case("nge_uq", 0x19)
1947 .Case("ngt_uq", 0x1A)
1948 .Case("false_os", 0x1B)
1949 .Case("neq_os", 0x1C)
1950 .Case("ge_oq", 0x1D)
1951 .Case("gt_oq", 0x1E)
1952 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001953 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001954 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001955 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1956 getParser().getContext());
1957 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001958 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001959 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001960 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001961 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001962 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001963 } else {
1964 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001965 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001966 }
1967 }
1968 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001969
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001970 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001971
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001972 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001973 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001974
Chris Lattner086a83a2010-09-08 05:17:37 +00001975 // Determine whether this is an instruction prefix.
1976 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001977 Name == "lock" || Name == "rep" ||
1978 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001979 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001980 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001981
1982
Chris Lattner086a83a2010-09-08 05:17:37 +00001983 // This does the actual operand parsing. Don't parse any more if we have a
1984 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1985 // just want to parse the "lock" as the first instruction and the "incl" as
1986 // the next one.
1987 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001988
1989 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00001990 if (getLexer().is(AsmToken::Star))
1991 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00001992
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001993 // Read the operands.
1994 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00001995 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1996 Operands.push_back(std::move(Op));
1997 if (!HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00001998 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001999 } else {
2000 Parser.eatToEndOfStatement();
2001 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00002002 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002003 // check for comma and eat it
2004 if (getLexer().is(AsmToken::Comma))
2005 Parser.Lex();
2006 else
2007 break;
2008 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00002009
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002010 if (getLexer().isNot(AsmToken::EndOfStatement))
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002011 return ErrorAndEatStatement(getLexer().getLoc(),
2012 "unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002013 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002014
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002015 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002016 if (getLexer().is(AsmToken::EndOfStatement) ||
2017 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002018 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002019
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002020 if (ExtraImmOp && isParsingIntelSyntax())
2021 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2022
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002023 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2024 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2025 // documented form in various unofficial manuals, so a lot of code uses it.
2026 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2027 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002028 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002029 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2030 isa<MCConstantExpr>(Op.Mem.Disp) &&
2031 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2032 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2033 SMLoc Loc = Op.getEndLoc();
2034 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002035 }
2036 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002037 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2038 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2039 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002040 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002041 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2042 isa<MCConstantExpr>(Op.Mem.Disp) &&
2043 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2044 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2045 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002046 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002047 }
2048 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002049
2050 // Append default arguments to "ins[bwld]"
2051 if (Name.startswith("ins") && Operands.size() == 1 &&
2052 (Name == "insb" || Name == "insw" || Name == "insl" ||
2053 Name == "insd" )) {
2054 if (isParsingIntelSyntax()) {
2055 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2056 Operands.push_back(DefaultMemDIOperand(NameLoc));
2057 } else {
2058 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2059 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002060 }
2061 }
2062
David Woodhousec472b812014-01-22 15:08:49 +00002063 // Append default arguments to "outs[bwld]"
2064 if (Name.startswith("outs") && Operands.size() == 1 &&
2065 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
2066 Name == "outsd" )) {
2067 if (isParsingIntelSyntax()) {
2068 Operands.push_back(DefaultMemSIOperand(NameLoc));
2069 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2070 } else {
2071 Operands.push_back(DefaultMemSIOperand(NameLoc));
2072 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002073 }
2074 }
2075
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002076 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2077 // values of $SIREG according to the mode. It would be nice if this
2078 // could be achieved with InstAlias in the tables.
2079 if (Name.startswith("lods") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002080 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002081 Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
2082 Operands.push_back(DefaultMemSIOperand(NameLoc));
2083
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002084 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2085 // values of $DIREG according to the mode. It would be nice if this
2086 // could be achieved with InstAlias in the tables.
2087 if (Name.startswith("stos") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002088 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002089 Name == "stosl" || Name == "stosd" || Name == "stosq"))
2090 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002091
David Woodhouse20fe4802014-01-22 15:08:27 +00002092 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2093 // values of $DIREG according to the mode. It would be nice if this
2094 // could be achieved with InstAlias in the tables.
2095 if (Name.startswith("scas") && Operands.size() == 1 &&
2096 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
2097 Name == "scasl" || Name == "scasd" || Name == "scasq"))
2098 Operands.push_back(DefaultMemDIOperand(NameLoc));
2099
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002100 // Add default SI and DI operands to "cmps[bwlq]".
2101 if (Name.startswith("cmps") &&
2102 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2103 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
2104 if (Operands.size() == 1) {
2105 if (isParsingIntelSyntax()) {
2106 Operands.push_back(DefaultMemSIOperand(NameLoc));
2107 Operands.push_back(DefaultMemDIOperand(NameLoc));
2108 } else {
2109 Operands.push_back(DefaultMemDIOperand(NameLoc));
2110 Operands.push_back(DefaultMemSIOperand(NameLoc));
2111 }
2112 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002113 X86Operand &Op = (X86Operand &)*Operands[1];
2114 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002115 if (!doSrcDstMatch(Op, Op2))
2116 return Error(Op.getStartLoc(),
2117 "mismatching source and destination index registers");
2118 }
2119 }
2120
David Woodhouse6f417de2014-01-22 15:08:42 +00002121 // Add default SI and DI operands to "movs[bwlq]".
2122 if ((Name.startswith("movs") &&
2123 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2124 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2125 (Name.startswith("smov") &&
2126 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2127 Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2128 if (Operands.size() == 1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002129 if (Name == "movsd")
David Woodhouse6f417de2014-01-22 15:08:42 +00002130 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2131 if (isParsingIntelSyntax()) {
2132 Operands.push_back(DefaultMemDIOperand(NameLoc));
2133 Operands.push_back(DefaultMemSIOperand(NameLoc));
2134 } else {
2135 Operands.push_back(DefaultMemSIOperand(NameLoc));
2136 Operands.push_back(DefaultMemDIOperand(NameLoc));
2137 }
2138 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002139 X86Operand &Op = (X86Operand &)*Operands[1];
2140 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse6f417de2014-01-22 15:08:42 +00002141 if (!doSrcDstMatch(Op, Op2))
2142 return Error(Op.getStartLoc(),
2143 "mismatching source and destination index registers");
2144 }
2145 }
2146
Chris Lattner4bd21712010-09-15 04:33:27 +00002147 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002148 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002149 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002150 Name.startswith("shl") || Name.startswith("sal") ||
2151 Name.startswith("rcl") || Name.startswith("rcr") ||
2152 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002153 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002154 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002155 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002156 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2157 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2158 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002159 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002160 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002161 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2162 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2163 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002164 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002165 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002166 }
Chad Rosier51afe632012-06-27 22:34:28 +00002167
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002168 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2169 // instalias with an immediate operand yet.
2170 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002171 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2172 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2173 cast<MCConstantExpr>(Op1.getImm())->getValue() == 3) {
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002174 Operands.erase(Operands.begin() + 1);
David Blaikie960ea3f2014-06-08 16:18:35 +00002175 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002176 }
2177 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002178
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002179 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002180}
2181
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002182static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2183 bool isCmp) {
2184 MCInst TmpInst;
2185 TmpInst.setOpcode(Opcode);
2186 if (!isCmp)
2187 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2188 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2189 TmpInst.addOperand(Inst.getOperand(0));
2190 Inst = TmpInst;
2191 return true;
2192}
2193
2194static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2195 bool isCmp = false) {
2196 if (!Inst.getOperand(0).isImm() ||
2197 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2198 return false;
2199
2200 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2201}
2202
2203static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2204 bool isCmp = false) {
2205 if (!Inst.getOperand(0).isImm() ||
2206 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2207 return false;
2208
2209 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2210}
2211
2212static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2213 bool isCmp = false) {
2214 if (!Inst.getOperand(0).isImm() ||
2215 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2216 return false;
2217
2218 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2219}
2220
David Blaikie960ea3f2014-06-08 16:18:35 +00002221bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Devang Patelde47cce2012-01-18 22:42:29 +00002222 switch (Inst.getOpcode()) {
2223 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002224 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2225 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2226 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2227 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2228 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2229 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2230 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2231 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2232 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2233 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2234 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2235 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2236 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2237 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2238 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2239 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2240 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2241 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002242 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2243 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2244 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2245 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2246 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2247 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002248 case X86::VMOVAPDrr:
2249 case X86::VMOVAPDYrr:
2250 case X86::VMOVAPSrr:
2251 case X86::VMOVAPSYrr:
2252 case X86::VMOVDQArr:
2253 case X86::VMOVDQAYrr:
2254 case X86::VMOVDQUrr:
2255 case X86::VMOVDQUYrr:
2256 case X86::VMOVUPDrr:
2257 case X86::VMOVUPDYrr:
2258 case X86::VMOVUPSrr:
2259 case X86::VMOVUPSYrr: {
2260 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2261 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2262 return false;
2263
2264 unsigned NewOpc;
2265 switch (Inst.getOpcode()) {
2266 default: llvm_unreachable("Invalid opcode");
2267 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2268 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2269 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2270 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2271 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2272 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2273 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2274 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2275 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2276 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2277 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2278 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2279 }
2280 Inst.setOpcode(NewOpc);
2281 return true;
2282 }
2283 case X86::VMOVSDrr:
2284 case X86::VMOVSSrr: {
2285 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2286 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2287 return false;
2288 unsigned NewOpc;
2289 switch (Inst.getOpcode()) {
2290 default: llvm_unreachable("Invalid opcode");
2291 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2292 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2293 }
2294 Inst.setOpcode(NewOpc);
2295 return true;
2296 }
Devang Patelde47cce2012-01-18 22:42:29 +00002297 }
Devang Patelde47cce2012-01-18 22:42:29 +00002298}
2299
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002300static const char *getSubtargetFeatureName(unsigned Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002301
David Blaikie960ea3f2014-06-08 16:18:35 +00002302void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2303 MCStreamer &Out) {
Evgeniy Stepanov77ad8662014-07-31 09:11:04 +00002304 Instrumentation->InstrumentAndEmitInstruction(Inst, Operands, getContext(),
2305 MII, Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002306}
2307
David Blaikie960ea3f2014-06-08 16:18:35 +00002308bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2309 OperandVector &Operands,
2310 MCStreamer &Out, unsigned &ErrorInfo,
2311 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002312 assert(!Operands.empty() && "Unexpect empty operand list!");
David Blaikie960ea3f2014-06-08 16:18:35 +00002313 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2314 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002315 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002316
Chris Lattnera63292a2010-09-29 01:50:45 +00002317 // First, handle aliases that expand to multiple instructions.
2318 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002319 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002320 // call.
Reid Klecknerb1f2d2f2014-07-31 00:07:33 +00002321 const char *Repl = StringSwitch<const char *>(Op.getToken())
2322 .Case("finit", "fninit")
2323 .Case("fsave", "fnsave")
2324 .Case("fstcw", "fnstcw")
2325 .Case("fstcww", "fnstcw")
2326 .Case("fstenv", "fnstenv")
2327 .Case("fstsw", "fnstsw")
2328 .Case("fstsww", "fnstsw")
2329 .Case("fclex", "fnclex")
2330 .Default(nullptr);
2331 if (Repl) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002332 MCInst Inst;
2333 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002334 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002335 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002336 EmitInstruction(Inst, Operands, Out);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002337 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002338 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002339
Chris Lattner628fbec2010-09-06 21:54:15 +00002340 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002341 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002342
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002343 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002344 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002345 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002346 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002347 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002348 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002349 // Some instructions need post-processing to, for example, tweak which
2350 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002351 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002352 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002353 while (processInstruction(Inst, Operands))
2354 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002355
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002356 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002357 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002358 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002359 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002360 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002361 case Match_MissingFeature: {
2362 assert(ErrorInfo && "Unknown missing feature!");
2363 // Special case the error message for the very common case where only
2364 // a single subtarget feature is missing.
2365 std::string Msg = "instruction requires:";
2366 unsigned Mask = 1;
2367 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2368 if (ErrorInfo & Mask) {
2369 Msg += " ";
2370 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2371 }
2372 Mask <<= 1;
2373 }
2374 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2375 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002376 case Match_InvalidOperand:
2377 WasOriginallyInvalidOperand = true;
2378 break;
2379 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002380 break;
2381 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002382
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002383 // FIXME: Ideally, we would only attempt suffix matches for things which are
2384 // valid prefixes, and we could just infer the right unambiguous
2385 // type. However, that requires substantially more matcher support than the
2386 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002387
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002388 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002389 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002390 SmallString<16> Tmp;
2391 Tmp += Base;
2392 Tmp += ' ';
David Blaikie960ea3f2014-06-08 16:18:35 +00002393 Op.setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002394
Chris Lattnerfab94132010-11-06 18:28:02 +00002395 // If this instruction starts with an 'f', then it is a floating point stack
2396 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2397 // 80-bit floating point, which use the suffixes s,l,t respectively.
2398 //
2399 // Otherwise, we assume that this may be an integer instruction, which comes
2400 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2401 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002402
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002403 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002404 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002405 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002406 unsigned Match[4];
Chad Rosier51afe632012-06-27 22:34:28 +00002407
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002408 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
2409 Tmp.back() = Suffixes[I];
2410 Match[I] = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2411 MatchingInlineAsm, isParsingIntelSyntax());
2412 // If this returned as a missing feature failure, remember that.
2413 if (Match[I] == Match_MissingFeature)
2414 ErrorInfoMissingFeature = ErrorInfoIgnore;
2415 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002416
2417 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002418 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002419
2420 // If exactly one matched, then we treat that as a successful match (and the
2421 // instruction will already have been filled in correctly, since the failing
2422 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002423 unsigned NumSuccessfulMatches =
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002424 std::count(std::begin(Match), std::end(Match), Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002425 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002426 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002427 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002428 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002429 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002430 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002431 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002432
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002433 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002434
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002435 // If we had multiple suffix matches, then identify this as an ambiguous
2436 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002437 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002438 char MatchChars[4];
2439 unsigned NumMatches = 0;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002440 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
2441 if (Match[I] == Match_Success)
2442 MatchChars[NumMatches++] = Suffixes[I];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002443
Alp Tokere69170a2014-06-26 22:52:05 +00002444 SmallString<126> Msg;
2445 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002446 OS << "ambiguous instructions require an explicit suffix (could be ";
2447 for (unsigned i = 0; i != NumMatches; ++i) {
2448 if (i != 0)
2449 OS << ", ";
2450 if (i + 1 == NumMatches)
2451 OS << "or ";
2452 OS << "'" << Base << MatchChars[i] << "'";
2453 }
2454 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002455 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002456 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002457 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002458
Chris Lattner628fbec2010-09-06 21:54:15 +00002459 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002460
Chris Lattner628fbec2010-09-06 21:54:15 +00002461 // If all of the instructions reported an invalid mnemonic, then the original
2462 // mnemonic was invalid.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002463 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002464 if (!WasOriginallyInvalidOperand) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002465 ArrayRef<SMRange> Ranges =
2466 MatchingInlineAsm ? EmptyRanges : Op.getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002467 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002468 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002469 }
2470
2471 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002472 if (ErrorInfo != ~0U) {
2473 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002474 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002475 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002476
David Blaikie960ea3f2014-06-08 16:18:35 +00002477 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2478 if (Operand.getStartLoc().isValid()) {
2479 SMRange OperandRange = Operand.getLocRange();
2480 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002481 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002482 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002483 }
2484
Chad Rosier3d4bc622012-08-21 19:36:59 +00002485 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002486 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002487 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002488
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002489 // If one instruction matched with a missing feature, report this as a
2490 // missing feature.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002491 if (std::count(std::begin(Match), std::end(Match),
2492 Match_MissingFeature) == 1) {
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002493 std::string Msg = "instruction requires:";
2494 unsigned Mask = 1;
2495 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2496 if (ErrorInfoMissingFeature & Mask) {
2497 Msg += " ";
2498 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2499 }
2500 Mask <<= 1;
2501 }
2502 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002503 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002504
Chris Lattner628fbec2010-09-06 21:54:15 +00002505 // If one instruction matched with an invalid operand, report this as an
2506 // operand failure.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002507 if (std::count(std::begin(Match), std::end(Match),
2508 Match_InvalidOperand) == 1) {
Chad Rosier3d4bc622012-08-21 19:36:59 +00002509 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002510 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002511 return true;
2512 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002513
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002514 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002515 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002516 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002517 return true;
2518}
2519
Nico Weber42f79db2014-07-17 20:24:55 +00002520bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
2521 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
2522}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002523
Devang Patel4a6e7782012-01-12 18:03:40 +00002524bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002525 StringRef IDVal = DirectiveID.getIdentifier();
2526 if (IDVal == ".word")
2527 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002528 else if (IDVal.startswith(".code"))
2529 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002530 else if (IDVal.startswith(".att_syntax")) {
2531 getParser().setAssemblerDialect(0);
2532 return false;
2533 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002534 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002535 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002536 // FIXME: Handle noprefix
2537 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002538 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002539 }
2540 return false;
2541 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002542 return true;
2543}
2544
2545/// ParseDirectiveWord
2546/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002547bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002548 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2549 for (;;) {
2550 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002551 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002552 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002553
Eric Christopherbf7bc492013-01-09 03:52:05 +00002554 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002555
Chris Lattner72c0b592010-10-30 17:38:55 +00002556 if (getLexer().is(AsmToken::EndOfStatement))
2557 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002558
Chris Lattner72c0b592010-10-30 17:38:55 +00002559 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002560 if (getLexer().isNot(AsmToken::Comma)) {
2561 Error(L, "unexpected token in directive");
2562 return false;
2563 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002564 Parser.Lex();
2565 }
2566 }
Chad Rosier51afe632012-06-27 22:34:28 +00002567
Chris Lattner72c0b592010-10-30 17:38:55 +00002568 Parser.Lex();
2569 return false;
2570}
2571
Evan Cheng481ebb02011-07-27 00:38:12 +00002572/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002573/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002574bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Craig Topper3c80d622014-01-06 04:55:54 +00002575 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002576 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002577 if (!is16BitMode()) {
2578 SwitchMode(X86::Mode16Bit);
2579 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2580 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002581 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002582 Parser.Lex();
2583 if (!is32BitMode()) {
2584 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002585 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2586 }
2587 } else if (IDVal == ".code64") {
2588 Parser.Lex();
2589 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002590 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002591 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2592 }
2593 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002594 Error(L, "unknown directive " + IDVal);
2595 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002596 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002597
Evan Cheng481ebb02011-07-27 00:38:12 +00002598 return false;
2599}
Chris Lattner72c0b592010-10-30 17:38:55 +00002600
Daniel Dunbar71475772009-07-17 20:42:00 +00002601// Force static initialization.
2602extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002603 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2604 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002605}
Daniel Dunbar00331992009-07-29 00:02:19 +00002606
Chris Lattner3e4582a2010-09-06 19:11:01 +00002607#define GET_REGISTER_MATCHER
2608#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002609#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002610#include "X86GenAsmMatcher.inc"