blob: f0765ed50d71befce6005eb6f481b2efd5c427ac [file] [log] [blame]
Daniel Dunbar71475772009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng11424442011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000011#include "X86AsmInstrumentation.h"
Evgeniy Stepanove3804d42014-02-28 12:28:07 +000012#include "X86AsmParserCommon.h"
13#include "X86Operand.h"
Chad Rosier6844ea02012-10-24 22:13:37 +000014#include "llvm/ADT/APFloat.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000015#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000018#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000020#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000023#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCParser/MCAsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSubtargetInfo.h"
30#include "llvm/MC/MCSymbol.h"
31#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000032#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000034#include "llvm/Support/raw_ostream.h"
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000035#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000036
Daniel Dunbar71475772009-07-17 20:42:00 +000037using namespace llvm;
38
39namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000040
Chad Rosier5362af92013-04-16 18:15:40 +000041static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000042 0, // IC_OR
43 1, // IC_AND
Kevin Enderbyd6b10712014-02-06 01:21:15 +000044 2, // IC_LSHIFT
45 2, // IC_RSHIFT
46 3, // IC_PLUS
47 3, // IC_MINUS
48 4, // IC_MULTIPLY
49 4, // IC_DIVIDE
50 5, // IC_RPAREN
51 6, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000052 0, // IC_IMM
53 0 // IC_REGISTER
54};
55
Devang Patel4a6e7782012-01-12 18:03:40 +000056class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000057 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000058 MCAsmParser &Parser;
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000059 const MCInstrInfo &MII;
Chad Rosierf0e87202012-10-25 20:41:34 +000060 ParseInstructionInfo *InstInfo;
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000061 std::unique_ptr<X86AsmInstrumentation> Instrumentation;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000062private:
Alp Tokera5b88a52013-12-02 16:06:06 +000063 SMLoc consumeToken() {
64 SMLoc Result = Parser.getTok().getLoc();
65 Parser.Lex();
66 return Result;
67 }
68
Chad Rosier5362af92013-04-16 18:15:40 +000069 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000070 IC_OR = 0,
71 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +000072 IC_LSHIFT,
73 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000074 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000075 IC_MINUS,
76 IC_MULTIPLY,
77 IC_DIVIDE,
78 IC_RPAREN,
79 IC_LPAREN,
80 IC_IMM,
81 IC_REGISTER
82 };
83
84 class InfixCalculator {
85 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
86 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
87 SmallVector<ICToken, 4> PostfixStack;
88
89 public:
90 int64_t popOperand() {
91 assert (!PostfixStack.empty() && "Poped an empty stack!");
92 ICToken Op = PostfixStack.pop_back_val();
93 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
94 && "Expected and immediate or register!");
95 return Op.second;
96 }
97 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
98 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
99 "Unexpected operand!");
100 PostfixStack.push_back(std::make_pair(Op, Val));
101 }
102
Jakub Staszak9c349222013-08-08 15:48:46 +0000103 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000104 void pushOperator(InfixCalculatorTok Op) {
105 // Push the new operator if the stack is empty.
106 if (InfixOperatorStack.empty()) {
107 InfixOperatorStack.push_back(Op);
108 return;
109 }
110
111 // Push the new operator if it has a higher precedence than the operator
112 // on the top of the stack or the operator on the top of the stack is a
113 // left parentheses.
114 unsigned Idx = InfixOperatorStack.size() - 1;
115 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
116 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
117 InfixOperatorStack.push_back(Op);
118 return;
119 }
120
121 // The operator on the top of the stack has higher precedence than the
122 // new operator.
123 unsigned ParenCount = 0;
124 while (1) {
125 // Nothing to process.
126 if (InfixOperatorStack.empty())
127 break;
128
129 Idx = InfixOperatorStack.size() - 1;
130 StackOp = InfixOperatorStack[Idx];
131 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
132 break;
133
134 // If we have an even parentheses count and we see a left parentheses,
135 // then stop processing.
136 if (!ParenCount && StackOp == IC_LPAREN)
137 break;
138
139 if (StackOp == IC_RPAREN) {
140 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000141 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000142 } else if (StackOp == IC_LPAREN) {
143 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000144 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000145 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000146 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000147 PostfixStack.push_back(std::make_pair(StackOp, 0));
148 }
149 }
150 // Push the new operator.
151 InfixOperatorStack.push_back(Op);
152 }
153 int64_t execute() {
154 // Push any remaining operators onto the postfix stack.
155 while (!InfixOperatorStack.empty()) {
156 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
157 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
158 PostfixStack.push_back(std::make_pair(StackOp, 0));
159 }
160
161 if (PostfixStack.empty())
162 return 0;
163
164 SmallVector<ICToken, 16> OperandStack;
165 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
166 ICToken Op = PostfixStack[i];
167 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
168 OperandStack.push_back(Op);
169 } else {
170 assert (OperandStack.size() > 1 && "Too few operands.");
171 int64_t Val;
172 ICToken Op2 = OperandStack.pop_back_val();
173 ICToken Op1 = OperandStack.pop_back_val();
174 switch (Op.first) {
175 default:
176 report_fatal_error("Unexpected operator!");
177 break;
178 case IC_PLUS:
179 Val = Op1.second + Op2.second;
180 OperandStack.push_back(std::make_pair(IC_IMM, Val));
181 break;
182 case IC_MINUS:
183 Val = Op1.second - Op2.second;
184 OperandStack.push_back(std::make_pair(IC_IMM, Val));
185 break;
186 case IC_MULTIPLY:
187 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
188 "Multiply operation with an immediate and a register!");
189 Val = Op1.second * Op2.second;
190 OperandStack.push_back(std::make_pair(IC_IMM, Val));
191 break;
192 case IC_DIVIDE:
193 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
194 "Divide operation with an immediate and a register!");
195 assert (Op2.second != 0 && "Division by zero!");
196 Val = Op1.second / Op2.second;
197 OperandStack.push_back(std::make_pair(IC_IMM, Val));
198 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000199 case IC_OR:
200 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
201 "Or operation with an immediate and a register!");
202 Val = Op1.second | Op2.second;
203 OperandStack.push_back(std::make_pair(IC_IMM, Val));
204 break;
205 case IC_AND:
206 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
207 "And operation with an immediate and a register!");
208 Val = Op1.second & Op2.second;
209 OperandStack.push_back(std::make_pair(IC_IMM, Val));
210 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000211 case IC_LSHIFT:
212 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
213 "Left shift operation with an immediate and a register!");
214 Val = Op1.second << Op2.second;
215 OperandStack.push_back(std::make_pair(IC_IMM, Val));
216 break;
217 case IC_RSHIFT:
218 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
219 "Right shift operation with an immediate and a register!");
220 Val = Op1.second >> Op2.second;
221 OperandStack.push_back(std::make_pair(IC_IMM, Val));
222 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000223 }
224 }
225 }
226 assert (OperandStack.size() == 1 && "Expected a single result.");
227 return OperandStack.pop_back_val().second;
228 }
229 };
230
231 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000232 IES_OR,
233 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000234 IES_LSHIFT,
235 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000236 IES_PLUS,
237 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000238 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000239 IES_MULTIPLY,
240 IES_DIVIDE,
241 IES_LBRAC,
242 IES_RBRAC,
243 IES_LPAREN,
244 IES_RPAREN,
245 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000246 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000247 IES_IDENTIFIER,
248 IES_ERROR
249 };
250
251 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000252 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000253 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000254 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000255 const MCExpr *Sym;
256 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000257 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000258 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000259 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000260 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000261 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000262 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
Craig Topper062a2ba2014-04-25 05:30:21 +0000263 Scale(1), Imm(imm), Sym(nullptr), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000264 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000265
266 unsigned getBaseReg() { return BaseReg; }
267 unsigned getIndexReg() { return IndexReg; }
268 unsigned getScale() { return Scale; }
269 const MCExpr *getSym() { return Sym; }
270 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000271 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000272 bool isValidEndState() {
273 return State == IES_RBRAC || State == IES_INTEGER;
274 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000275 bool getStopOnLBrac() { return StopOnLBrac; }
276 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000277 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000278
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000279 InlineAsmIdentifierInfo &getIdentifierInfo() {
280 return Info;
281 }
282
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000283 void onOr() {
284 IntelExprState CurrState = State;
285 switch (State) {
286 default:
287 State = IES_ERROR;
288 break;
289 case IES_INTEGER:
290 case IES_RPAREN:
291 case IES_REGISTER:
292 State = IES_OR;
293 IC.pushOperator(IC_OR);
294 break;
295 }
296 PrevState = CurrState;
297 }
298 void onAnd() {
299 IntelExprState CurrState = State;
300 switch (State) {
301 default:
302 State = IES_ERROR;
303 break;
304 case IES_INTEGER:
305 case IES_RPAREN:
306 case IES_REGISTER:
307 State = IES_AND;
308 IC.pushOperator(IC_AND);
309 break;
310 }
311 PrevState = CurrState;
312 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000313 void onLShift() {
314 IntelExprState CurrState = State;
315 switch (State) {
316 default:
317 State = IES_ERROR;
318 break;
319 case IES_INTEGER:
320 case IES_RPAREN:
321 case IES_REGISTER:
322 State = IES_LSHIFT;
323 IC.pushOperator(IC_LSHIFT);
324 break;
325 }
326 PrevState = CurrState;
327 }
328 void onRShift() {
329 IntelExprState CurrState = State;
330 switch (State) {
331 default:
332 State = IES_ERROR;
333 break;
334 case IES_INTEGER:
335 case IES_RPAREN:
336 case IES_REGISTER:
337 State = IES_RSHIFT;
338 IC.pushOperator(IC_RSHIFT);
339 break;
340 }
341 PrevState = CurrState;
342 }
Chad Rosier5362af92013-04-16 18:15:40 +0000343 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000344 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000345 switch (State) {
346 default:
347 State = IES_ERROR;
348 break;
349 case IES_INTEGER:
350 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000351 case IES_REGISTER:
352 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000353 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000354 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
355 // If we already have a BaseReg, then assume this is the IndexReg with
356 // a scale of 1.
357 if (!BaseReg) {
358 BaseReg = TmpReg;
359 } else {
360 assert (!IndexReg && "BaseReg/IndexReg already set!");
361 IndexReg = TmpReg;
362 Scale = 1;
363 }
364 }
Chad Rosier5362af92013-04-16 18:15:40 +0000365 break;
366 }
Chad Rosier31246272013-04-17 21:01:45 +0000367 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000368 }
369 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000370 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000371 switch (State) {
372 default:
373 State = IES_ERROR;
374 break;
375 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000376 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000377 case IES_MULTIPLY:
378 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000379 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000380 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000381 case IES_LBRAC:
382 case IES_RBRAC:
383 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000384 case IES_REGISTER:
385 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000386 // Only push the minus operator if it is not a unary operator.
387 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
388 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
389 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
390 IC.pushOperator(IC_MINUS);
391 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
392 // If we already have a BaseReg, then assume this is the IndexReg with
393 // a scale of 1.
394 if (!BaseReg) {
395 BaseReg = TmpReg;
396 } else {
397 assert (!IndexReg && "BaseReg/IndexReg already set!");
398 IndexReg = TmpReg;
399 Scale = 1;
400 }
Chad Rosier5362af92013-04-16 18:15:40 +0000401 }
Chad Rosier5362af92013-04-16 18:15:40 +0000402 break;
403 }
Chad Rosier31246272013-04-17 21:01:45 +0000404 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000405 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000406 void onNot() {
407 IntelExprState CurrState = State;
408 switch (State) {
409 default:
410 State = IES_ERROR;
411 break;
412 case IES_PLUS:
413 case IES_NOT:
414 State = IES_NOT;
415 break;
416 }
417 PrevState = CurrState;
418 }
Chad Rosier5362af92013-04-16 18:15:40 +0000419 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000420 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000421 switch (State) {
422 default:
423 State = IES_ERROR;
424 break;
425 case IES_PLUS:
426 case IES_LPAREN:
427 State = IES_REGISTER;
428 TmpReg = Reg;
429 IC.pushOperand(IC_REGISTER);
430 break;
Chad Rosier31246272013-04-17 21:01:45 +0000431 case IES_MULTIPLY:
432 // Index Register - Scale * Register
433 if (PrevState == IES_INTEGER) {
434 assert (!IndexReg && "IndexReg already set!");
435 State = IES_REGISTER;
436 IndexReg = Reg;
437 // Get the scale and replace the 'Scale * Register' with '0'.
438 Scale = IC.popOperand();
439 IC.pushOperand(IC_IMM);
440 IC.popOperator();
441 } else {
442 State = IES_ERROR;
443 }
Chad Rosier5362af92013-04-16 18:15:40 +0000444 break;
445 }
Chad Rosier31246272013-04-17 21:01:45 +0000446 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000447 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000448 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000449 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000450 switch (State) {
451 default:
452 State = IES_ERROR;
453 break;
454 case IES_PLUS:
455 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000456 case IES_NOT:
Chad Rosier5362af92013-04-16 18:15:40 +0000457 State = IES_INTEGER;
458 Sym = SymRef;
459 SymName = SymRefName;
460 IC.pushOperand(IC_IMM);
461 break;
462 }
463 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000464 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000465 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000466 switch (State) {
467 default:
468 State = IES_ERROR;
469 break;
470 case IES_PLUS:
471 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000472 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000473 case IES_OR:
474 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000475 case IES_LSHIFT:
476 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000477 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000478 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000479 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000480 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000481 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
482 // Index Register - Register * Scale
483 assert (!IndexReg && "IndexReg already set!");
484 IndexReg = TmpReg;
485 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000486 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
487 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
488 return true;
489 }
Chad Rosier31246272013-04-17 21:01:45 +0000490 // Get the scale and replace the 'Register * Scale' with '0'.
491 IC.popOperator();
492 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000493 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000494 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosier31246272013-04-17 21:01:45 +0000495 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000496 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
497 PrevState == IES_NOT) &&
Chad Rosier31246272013-04-17 21:01:45 +0000498 CurrState == IES_MINUS) {
499 // Unary minus. No need to pop the minus operand because it was never
500 // pushed.
501 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000502 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
503 PrevState == IES_OR || PrevState == IES_AND ||
504 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
505 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
506 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
507 PrevState == IES_NOT) &&
508 CurrState == IES_NOT) {
509 // Unary not. No need to pop the not operand because it was never
510 // pushed.
511 IC.pushOperand(IC_IMM, ~TmpInt); // Push ~Imm.
Chad Rosier31246272013-04-17 21:01:45 +0000512 } else {
513 IC.pushOperand(IC_IMM, TmpInt);
514 }
Chad Rosier5362af92013-04-16 18:15:40 +0000515 break;
516 }
Chad Rosier31246272013-04-17 21:01:45 +0000517 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000518 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000519 }
520 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000521 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000522 switch (State) {
523 default:
524 State = IES_ERROR;
525 break;
526 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000527 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000528 case IES_RPAREN:
529 State = IES_MULTIPLY;
530 IC.pushOperator(IC_MULTIPLY);
531 break;
532 }
533 }
534 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000535 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000536 switch (State) {
537 default:
538 State = IES_ERROR;
539 break;
540 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000541 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000542 State = IES_DIVIDE;
543 IC.pushOperator(IC_DIVIDE);
544 break;
545 }
546 }
547 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000548 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000549 switch (State) {
550 default:
551 State = IES_ERROR;
552 break;
553 case IES_RBRAC:
554 State = IES_PLUS;
555 IC.pushOperator(IC_PLUS);
556 break;
557 }
558 }
559 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000560 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000561 switch (State) {
562 default:
563 State = IES_ERROR;
564 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000565 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000566 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000567 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000568 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000569 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
570 // If we already have a BaseReg, then assume this is the IndexReg with
571 // a scale of 1.
572 if (!BaseReg) {
573 BaseReg = TmpReg;
574 } else {
575 assert (!IndexReg && "BaseReg/IndexReg already set!");
576 IndexReg = TmpReg;
577 Scale = 1;
578 }
Chad Rosier5362af92013-04-16 18:15:40 +0000579 }
580 break;
581 }
Chad Rosier31246272013-04-17 21:01:45 +0000582 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000583 }
584 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000585 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000586 switch (State) {
587 default:
588 State = IES_ERROR;
589 break;
590 case IES_PLUS:
591 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000592 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000593 case IES_OR:
594 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000595 case IES_LSHIFT:
596 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000597 case IES_MULTIPLY:
598 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000599 case IES_LPAREN:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000600 // FIXME: We don't handle this type of unary minus or not, yet.
Chad Rosierdb003992013-04-18 16:28:19 +0000601 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000602 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000603 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosierdb003992013-04-18 16:28:19 +0000604 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000605 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
606 PrevState == IES_NOT) &&
607 (CurrState == IES_MINUS || CurrState == IES_NOT)) {
Chad Rosierdb003992013-04-18 16:28:19 +0000608 State = IES_ERROR;
609 break;
610 }
Chad Rosier5362af92013-04-16 18:15:40 +0000611 State = IES_LPAREN;
612 IC.pushOperator(IC_LPAREN);
613 break;
614 }
Chad Rosier31246272013-04-17 21:01:45 +0000615 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000616 }
617 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000618 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000619 switch (State) {
620 default:
621 State = IES_ERROR;
622 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000623 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000624 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000625 case IES_RPAREN:
626 State = IES_RPAREN;
627 IC.pushOperator(IC_RPAREN);
628 break;
629 }
630 }
631 };
632
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000633 MCAsmParser &getParser() const { return Parser; }
634
635 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
636
Chris Lattnera3a06812011-10-16 04:47:35 +0000637 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000638 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000639 bool MatchingInlineAsm = false) {
640 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000641 return Parser.Error(L, Msg, Ranges);
642 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000643
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000644 bool ErrorAndEatStatement(SMLoc L, const Twine &Msg,
645 ArrayRef<SMRange> Ranges = None,
646 bool MatchingInlineAsm = false) {
647 Parser.eatToEndOfStatement();
648 return Error(L, Msg, Ranges, MatchingInlineAsm);
649 }
650
David Blaikie960ea3f2014-06-08 16:18:35 +0000651 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000652 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000653 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000654 }
655
David Blaikie960ea3f2014-06-08 16:18:35 +0000656 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
657 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
658 std::unique_ptr<X86Operand> ParseOperand();
659 std::unique_ptr<X86Operand> ParseATTOperand();
660 std::unique_ptr<X86Operand> ParseIntelOperand();
661 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000662 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
David Blaikie960ea3f2014-06-08 16:18:35 +0000663 std::unique_ptr<X86Operand> ParseIntelOperator(unsigned OpKind);
664 std::unique_ptr<X86Operand>
665 ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
666 std::unique_ptr<X86Operand>
667 ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000668 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
David Blaikie960ea3f2014-06-08 16:18:35 +0000669 std::unique_ptr<X86Operand> ParseIntelBracExpression(unsigned SegReg,
670 SMLoc Start,
671 int64_t ImmDisp,
672 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000673 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
674 InlineAsmIdentifierInfo &Info,
675 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000676
David Blaikie960ea3f2014-06-08 16:18:35 +0000677 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000678
David Blaikie960ea3f2014-06-08 16:18:35 +0000679 std::unique_ptr<X86Operand>
680 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
681 unsigned IndexReg, unsigned Scale, SMLoc Start,
682 SMLoc End, unsigned Size, StringRef Identifier,
683 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000684
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000685 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000686 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000687
David Blaikie960ea3f2014-06-08 16:18:35 +0000688 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000689
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000690 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
691 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000692 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000693
Chad Rosier49963552012-10-13 00:26:04 +0000694 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000695 OperandVector &Operands, MCStreamer &Out,
696 unsigned &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000697 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000698
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000699 /// doSrcDstMatch - Returns true if operands are matching in their
700 /// word size (%si and %di, %esi and %edi, etc.). Order depends on
701 /// the parsing mode (Intel vs. AT&T).
702 bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
703
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000704 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
705 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
706 /// \return \c true if no parsing errors occurred, \c false otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000707 bool HandleAVX512Operand(OperandVector &Operands,
708 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000709
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000710 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000711 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000712 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000713 }
Craig Topper3c80d622014-01-06 04:55:54 +0000714 bool is32BitMode() const {
715 // FIXME: Can tablegen auto-generate this?
716 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
717 }
718 bool is16BitMode() const {
719 // FIXME: Can tablegen auto-generate this?
720 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
721 }
722 void SwitchMode(uint64_t mode) {
723 uint64_t oldMode = STI.getFeatureBits() &
724 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
725 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000726 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000727 assert(mode == (STI.getFeatureBits() &
728 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000729 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000730
Chad Rosierc2f055d2013-04-18 16:13:18 +0000731 bool isParsingIntelSyntax() {
732 return getParser().getAssemblerDialect();
733 }
734
Daniel Dunbareefe8612010-07-19 05:44:09 +0000735 /// @name Auto-generated Matcher Functions
736 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000737
Chris Lattner3e4582a2010-09-06 19:11:01 +0000738#define GET_ASSEMBLER_HEADER
739#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000740
Daniel Dunbar00331992009-07-29 00:02:19 +0000741 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000742
743public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000744 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +0000745 const MCInstrInfo &mii,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000746 const MCTargetOptions &Options)
Craig Topper062a2ba2014-04-25 05:30:21 +0000747 : MCTargetAsmParser(), STI(sti), Parser(parser), MII(mii),
748 InstInfo(nullptr) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000749
Daniel Dunbareefe8612010-07-19 05:44:09 +0000750 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000751 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000752 Instrumentation.reset(
753 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000754 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000755
Craig Topper39012cc2014-03-09 18:03:14 +0000756 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000757
David Blaikie960ea3f2014-06-08 16:18:35 +0000758 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
759 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000760
Craig Topper39012cc2014-03-09 18:03:14 +0000761 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000762};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000763} // end anonymous namespace
764
Sean Callanan86c11812010-01-23 00:40:33 +0000765/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000766/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000767
Chris Lattner60db0a62010-02-09 00:34:28 +0000768static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000769
770/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000771
Kevin Enderbybc570f22014-01-23 22:34:42 +0000772static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
773 StringRef &ErrMsg) {
774 // If we have both a base register and an index register make sure they are
775 // both 64-bit or 32-bit registers.
776 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
777 if (BaseReg != 0 && IndexReg != 0) {
778 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
779 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
780 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
781 IndexReg != X86::RIZ) {
782 ErrMsg = "base register is 64-bit, but index register is not";
783 return true;
784 }
785 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
786 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
787 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
788 IndexReg != X86::EIZ){
789 ErrMsg = "base register is 32-bit, but index register is not";
790 return true;
791 }
792 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
793 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
794 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
795 ErrMsg = "base register is 16-bit, but index register is not";
796 return true;
797 }
798 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
799 IndexReg != X86::SI && IndexReg != X86::DI) ||
800 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
801 IndexReg != X86::BX && IndexReg != X86::BP)) {
802 ErrMsg = "invalid 16-bit base/index register combination";
803 return true;
804 }
805 }
806 }
807 return false;
808}
809
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000810bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
811{
812 // Return true and let a normal complaint about bogus operands happen.
813 if (!Op1.isMem() || !Op2.isMem())
814 return true;
815
816 // Actually these might be the other way round if Intel syntax is
817 // being used. It doesn't matter.
818 unsigned diReg = Op1.Mem.BaseReg;
819 unsigned siReg = Op2.Mem.BaseReg;
820
821 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
822 return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
823 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
824 return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
825 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
826 return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
827 // Again, return true and let another error happen.
828 return true;
829}
830
Devang Patel4a6e7782012-01-12 18:03:40 +0000831bool X86AsmParser::ParseRegister(unsigned &RegNo,
832 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000833 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000834 const AsmToken &PercentTok = Parser.getTok();
835 StartLoc = PercentTok.getLoc();
836
837 // If we encounter a %, ignore it. This code handles registers with and
838 // without the prefix, unprefixed registers can occur in cfi directives.
839 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000840 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000841
Sean Callanan936b0d32010-01-19 21:44:56 +0000842 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000843 EndLoc = Tok.getEndLoc();
844
Devang Patelce6a2ca2012-01-20 22:32:05 +0000845 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000846 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000847 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000848 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000849 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000850
Kevin Enderby7d912182009-09-03 17:15:07 +0000851 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000852
Chris Lattner1261b812010-09-22 04:11:10 +0000853 // If the match failed, try the register name as lowercase.
854 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000855 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000856
Evan Chengeda1d4f2011-07-27 23:22:03 +0000857 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000858 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000859 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
860 // checked.
861 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
862 // REX prefix.
863 if (RegNo == X86::RIZ ||
864 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
865 X86II::isX86_64NonExtLowByteReg(RegNo) ||
866 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000867 return Error(StartLoc, "register %"
868 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000869 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000870 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000871
Chris Lattner1261b812010-09-22 04:11:10 +0000872 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
873 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000874 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000875 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000876
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000877 // Check to see if we have '(4)' after %st.
878 if (getLexer().isNot(AsmToken::LParen))
879 return false;
880 // Lex the paren.
881 getParser().Lex();
882
883 const AsmToken &IntTok = Parser.getTok();
884 if (IntTok.isNot(AsmToken::Integer))
885 return Error(IntTok.getLoc(), "expected stack index");
886 switch (IntTok.getIntVal()) {
887 case 0: RegNo = X86::ST0; break;
888 case 1: RegNo = X86::ST1; break;
889 case 2: RegNo = X86::ST2; break;
890 case 3: RegNo = X86::ST3; break;
891 case 4: RegNo = X86::ST4; break;
892 case 5: RegNo = X86::ST5; break;
893 case 6: RegNo = X86::ST6; break;
894 case 7: RegNo = X86::ST7; break;
895 default: return Error(IntTok.getLoc(), "invalid stack index");
896 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000897
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000898 if (getParser().Lex().isNot(AsmToken::RParen))
899 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000900
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000901 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000902 Parser.Lex(); // Eat ')'
903 return false;
904 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000905
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000906 EndLoc = Parser.getTok().getEndLoc();
907
Chris Lattner80486622010-06-24 07:29:18 +0000908 // If this is "db[0-7]", match it as an alias
909 // for dr[0-7].
910 if (RegNo == 0 && Tok.getString().size() == 3 &&
911 Tok.getString().startswith("db")) {
912 switch (Tok.getString()[2]) {
913 case '0': RegNo = X86::DR0; break;
914 case '1': RegNo = X86::DR1; break;
915 case '2': RegNo = X86::DR2; break;
916 case '3': RegNo = X86::DR3; break;
917 case '4': RegNo = X86::DR4; break;
918 case '5': RegNo = X86::DR5; break;
919 case '6': RegNo = X86::DR6; break;
920 case '7': RegNo = X86::DR7; break;
921 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000922
Chris Lattner80486622010-06-24 07:29:18 +0000923 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000924 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000925 Parser.Lex(); // Eat it.
926 return false;
927 }
928 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000929
Devang Patelce6a2ca2012-01-20 22:32:05 +0000930 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000931 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000932 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000933 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000934 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000935
Sean Callanana83fd7d2010-01-19 20:27:46 +0000936 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000937 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000938}
939
David Blaikie960ea3f2014-06-08 16:18:35 +0000940std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000941 unsigned basereg =
942 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
943 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
944 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
945 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
946}
947
David Blaikie960ea3f2014-06-08 16:18:35 +0000948std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000949 unsigned basereg =
950 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
951 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
952 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
953 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
954}
955
David Blaikie960ea3f2014-06-08 16:18:35 +0000956std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000957 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000958 return ParseIntelOperand();
959 return ParseATTOperand();
960}
961
Devang Patel41b9dde2012-01-17 18:00:18 +0000962/// getIntelMemOperandSize - Return intel memory operand size.
963static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000964 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000965 .Cases("BYTE", "byte", 8)
966 .Cases("WORD", "word", 16)
967 .Cases("DWORD", "dword", 32)
968 .Cases("QWORD", "qword", 64)
969 .Cases("XWORD", "xword", 80)
970 .Cases("XMMWORD", "xmmword", 128)
971 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +0000972 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +0000973 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +0000974 .Default(0);
975 return Size;
Devang Patel46831de2012-01-12 01:36:43 +0000976}
977
David Blaikie960ea3f2014-06-08 16:18:35 +0000978std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
979 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
980 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
981 InlineAsmIdentifierInfo &Info) {
Reid Klecknerd84e70e2014-03-04 00:33:17 +0000982 // If this is not a VarDecl then assume it is a FuncDecl or some other label
983 // reference. We need an 'r' constraint here, so we need to create register
984 // operand to ensure proper matching. Just pick a GPR based on the size of
985 // a pointer.
986 if (isa<MCSymbolRefExpr>(Disp) && !Info.IsVarDecl) {
987 unsigned RegNo =
988 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
989 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
990 SMLoc(), Identifier, Info.OpDecl);
991 }
992
993 // We either have a direct symbol reference, or an offset from a symbol. The
994 // parser always puts the symbol on the LHS, so look there for size
995 // calculation purposes.
996 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
997 bool IsSymRef =
998 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
999 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001000 if (!Size) {
1001 Size = Info.Type * 8; // Size is in terms of bits in this context.
1002 if (Size)
1003 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1004 /*Len=*/0, Size));
1005 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001006 }
1007
Chad Rosier7ca135b2013-03-19 21:11:56 +00001008 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001009 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001010 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001011 BaseReg = BaseReg ? BaseReg : 1;
1012 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001013 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001014}
1015
Chad Rosierd383db52013-04-12 20:20:54 +00001016static void
1017RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1018 StringRef SymName, int64_t ImmDisp,
1019 int64_t FinalImmDisp, SMLoc &BracLoc,
1020 SMLoc &StartInBrac, SMLoc &End) {
1021 // Remove the '[' and ']' from the IR string.
1022 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1023 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1024
1025 // If ImmDisp is non-zero, then we parsed a displacement before the
1026 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1027 // If ImmDisp doesn't match the displacement computed by the state machine
1028 // then we have an additional displacement in the bracketed expression.
1029 if (ImmDisp != FinalImmDisp) {
1030 if (ImmDisp) {
1031 // We have an immediate displacement before the bracketed expression.
1032 // Adjust this to match the final immediate displacement.
1033 bool Found = false;
1034 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1035 E = AsmRewrites->end(); I != E; ++I) {
1036 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1037 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001038 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1039 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001040 (*I).Kind = AOK_Imm;
1041 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1042 (*I).Val = FinalImmDisp;
1043 Found = true;
1044 break;
1045 }
1046 }
1047 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001048 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001049 } else {
1050 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001051 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001052 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001053 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001054 }
1055 }
1056 // Remove all the ImmPrefix rewrites within the brackets.
1057 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1058 E = AsmRewrites->end(); I != E; ++I) {
1059 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1060 continue;
1061 if ((*I).Kind == AOK_ImmPrefix)
1062 (*I).Kind = AOK_Delete;
1063 }
1064 const char *SymLocPtr = SymName.data();
1065 // Skip everything before the symbol.
1066 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1067 assert(Len > 0 && "Expected a non-negative length.");
1068 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1069 }
1070 // Skip everything after the symbol.
1071 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1072 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1073 assert(Len > 0 && "Expected a non-negative length.");
1074 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1075 }
1076}
1077
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001078bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001079 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001080
Chad Rosier5c118fd2013-01-14 22:31:35 +00001081 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001082 while (!Done) {
1083 bool UpdateLocLex = true;
1084
1085 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1086 // identifier. Don't try an parse it as a register.
1087 if (Tok.getString().startswith("."))
1088 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001089
1090 // If we're parsing an immediate expression, we don't expect a '['.
1091 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1092 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001093
David Majnemer6a5b8122014-06-19 01:25:43 +00001094 AsmToken::TokenKind TK = getLexer().getKind();
1095 switch (TK) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001096 default: {
1097 if (SM.isValidEndState()) {
1098 Done = true;
1099 break;
1100 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001101 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001102 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001103 case AsmToken::EndOfStatement: {
1104 Done = true;
1105 break;
1106 }
David Majnemer6a5b8122014-06-19 01:25:43 +00001107 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001108 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001109 // This could be a register or a symbolic displacement.
1110 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001111 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001112 SMLoc IdentLoc = Tok.getLoc();
1113 StringRef Identifier = Tok.getString();
David Majnemer6a5b8122014-06-19 01:25:43 +00001114 if (TK != AsmToken::String && !ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001115 SM.onRegister(TmpReg);
1116 UpdateLocLex = false;
1117 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001118 } else {
1119 if (!isParsingInlineAsm()) {
1120 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001121 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001122 } else {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001123 // This is a dot operator, not an adjacent identifier.
1124 if (Identifier.find('.') != StringRef::npos) {
1125 return false;
1126 } else {
1127 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1128 if (ParseIntelIdentifier(Val, Identifier, Info,
1129 /*Unevaluated=*/false, End))
1130 return true;
1131 }
Chad Rosier95ce8892013-04-19 18:39:50 +00001132 }
1133 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001134 UpdateLocLex = false;
1135 break;
1136 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001137 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001138 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001139 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001140 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001141 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001142 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1143 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001144 // Look for 'b' or 'f' following an Integer as a directional label
1145 SMLoc Loc = getTok().getLoc();
1146 int64_t IntVal = getTok().getIntVal();
1147 End = consumeToken();
1148 UpdateLocLex = false;
1149 if (getLexer().getKind() == AsmToken::Identifier) {
1150 StringRef IDVal = getTok().getString();
1151 if (IDVal == "f" || IDVal == "b") {
1152 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +00001153 getContext().GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001154 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1155 const MCExpr *Val =
1156 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1157 if (IDVal == "b" && Sym->isUndefined())
1158 return Error(Loc, "invalid reference to undefined symbol");
1159 StringRef Identifier = Sym->getName();
1160 SM.onIdentifierExpr(Val, Identifier);
1161 End = consumeToken();
1162 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001163 if (SM.onInteger(IntVal, ErrMsg))
1164 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001165 }
1166 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001167 if (SM.onInteger(IntVal, ErrMsg))
1168 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001169 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001170 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001171 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001172 case AsmToken::Plus: SM.onPlus(); break;
1173 case AsmToken::Minus: SM.onMinus(); break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001174 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001175 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001176 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001177 case AsmToken::Pipe: SM.onOr(); break;
1178 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001179 case AsmToken::LessLess:
1180 SM.onLShift(); break;
1181 case AsmToken::GreaterGreater:
1182 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001183 case AsmToken::LBrac: SM.onLBrac(); break;
1184 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001185 case AsmToken::LParen: SM.onLParen(); break;
1186 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001187 }
Chad Rosier31246272013-04-17 21:01:45 +00001188 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001189 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001190
Alp Tokera5b88a52013-12-02 16:06:06 +00001191 if (!Done && UpdateLocLex)
1192 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001193 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001194 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001195}
1196
David Blaikie960ea3f2014-06-08 16:18:35 +00001197std::unique_ptr<X86Operand>
1198X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
1199 int64_t ImmDisp, unsigned Size) {
Chad Rosier5362af92013-04-16 18:15:40 +00001200 const AsmToken &Tok = Parser.getTok();
1201 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1202 if (getLexer().isNot(AsmToken::LBrac))
1203 return ErrorOperand(BracLoc, "Expected '[' token!");
1204 Parser.Lex(); // Eat '['
1205
1206 SMLoc StartInBrac = Tok.getLoc();
1207 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1208 // may have already parsed an immediate displacement before the bracketed
1209 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001210 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001211 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001212 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +00001213
Craig Topper062a2ba2014-04-25 05:30:21 +00001214 const MCExpr *Disp = nullptr;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001215 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001216 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001217 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001218 if (isParsingInlineAsm())
1219 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001220 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001221 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001222 }
1223
1224 if (SM.getImm() || !Disp) {
1225 const MCExpr *Imm = MCConstantExpr::Create(SM.getImm(), getContext());
1226 if (Disp)
1227 Disp = MCBinaryExpr::CreateAdd(Disp, Imm, getContext());
1228 else
1229 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001230 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001231
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001232 // Parse struct field access. Intel requires a dot, but MSVC doesn't. MSVC
1233 // will in fact do global lookup the field name inside all global typedefs,
1234 // but we don't emulate that.
1235 if (Tok.getString().find('.') != StringRef::npos) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001236 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001237 if (ParseIntelDotOperator(Disp, NewDisp))
Craig Topper062a2ba2014-04-25 05:30:21 +00001238 return nullptr;
Chad Rosier911c1f32012-10-25 17:37:43 +00001239
Chad Rosier70f47592013-04-10 20:07:47 +00001240 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001241 Parser.Lex(); // Eat the field.
1242 Disp = NewDisp;
1243 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001244
Chad Rosier5c118fd2013-01-14 22:31:35 +00001245 int BaseReg = SM.getBaseReg();
1246 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001247 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001248 if (!isParsingInlineAsm()) {
1249 // handle [-42]
1250 if (!BaseReg && !IndexReg) {
1251 if (!SegReg)
1252 return X86Operand::CreateMem(Disp, Start, End, Size);
1253 else
1254 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1255 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001256 StringRef ErrMsg;
1257 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1258 Error(StartInBrac, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001259 return nullptr;
Kevin Enderbybc570f22014-01-23 22:34:42 +00001260 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001261 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1262 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001263 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001264
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001265 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001266 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001267 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001268}
1269
Chad Rosier8a244662013-04-02 20:02:33 +00001270// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001271bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1272 StringRef &Identifier,
1273 InlineAsmIdentifierInfo &Info,
1274 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001275 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001276 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001277
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001278 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001279 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001280
Chad Rosier8a244662013-04-02 20:02:33 +00001281 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001282
1283 // Advance the token stream until the end of the current token is
1284 // after the end of what the frontend claimed.
1285 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1286 while (true) {
1287 End = Tok.getEndLoc();
1288 getLexer().Lex();
1289
1290 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1291 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001292 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001293
1294 // Create the symbol reference.
1295 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001296 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1297 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001298 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001299 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001300}
1301
David Majnemeraa34d792013-08-27 21:56:17 +00001302/// \brief Parse intel style segment override.
David Blaikie960ea3f2014-06-08 16:18:35 +00001303std::unique_ptr<X86Operand>
1304X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start,
1305 unsigned Size) {
David Majnemeraa34d792013-08-27 21:56:17 +00001306 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1307 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1308 if (Tok.isNot(AsmToken::Colon))
1309 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1310 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001311
David Majnemeraa34d792013-08-27 21:56:17 +00001312 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001313 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001314 ImmDisp = Tok.getIntVal();
1315 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1316
Chad Rosier1530ba52013-03-27 21:49:56 +00001317 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001318 InstInfo->AsmRewrites->push_back(
1319 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1320
1321 if (getLexer().isNot(AsmToken::LBrac)) {
1322 // An immediate following a 'segment register', 'colon' token sequence can
1323 // be followed by a bracketed expression. If it isn't we know we have our
1324 // final segment override.
1325 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1326 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1327 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1328 Size);
1329 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001330 }
1331
Chad Rosier91c82662012-10-24 17:22:29 +00001332 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001333 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001334
David Majnemeraa34d792013-08-27 21:56:17 +00001335 const MCExpr *Val;
1336 SMLoc End;
1337 if (!isParsingInlineAsm()) {
1338 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001339 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001340
1341 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001342 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001343
David Majnemeraa34d792013-08-27 21:56:17 +00001344 InlineAsmIdentifierInfo Info;
1345 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001346 if (ParseIntelIdentifier(Val, Identifier, Info,
1347 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001348 return nullptr;
David Majnemeraa34d792013-08-27 21:56:17 +00001349 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1350 /*Scale=*/1, Start, End, Size, Identifier, Info);
1351}
1352
1353/// ParseIntelMemOperand - Parse intel style memory operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00001354std::unique_ptr<X86Operand> X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp,
1355 SMLoc Start,
1356 unsigned Size) {
David Majnemeraa34d792013-08-27 21:56:17 +00001357 const AsmToken &Tok = Parser.getTok();
1358 SMLoc End;
1359
1360 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1361 if (getLexer().is(AsmToken::LBrac))
1362 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001363 assert(ImmDisp == 0);
David Majnemeraa34d792013-08-27 21:56:17 +00001364
Chad Rosier95ce8892013-04-19 18:39:50 +00001365 const MCExpr *Val;
1366 if (!isParsingInlineAsm()) {
1367 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001368 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001369
1370 return X86Operand::CreateMem(Val, Start, End, Size);
1371 }
1372
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001373 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001374 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001375 if (ParseIntelIdentifier(Val, Identifier, Info,
1376 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001377 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001378
1379 if (!getLexer().is(AsmToken::LBrac))
1380 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
1381 /*Scale=*/1, Start, End, Size, Identifier, Info);
1382
1383 Parser.Lex(); // Eat '['
1384
1385 // Parse Identifier [ ImmDisp ]
1386 IntelExprStateMachine SM(/*ImmDisp=*/0, /*StopOnLBrac=*/true,
1387 /*AddImmPrefix=*/false);
1388 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001389 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001390
1391 if (SM.getSym()) {
1392 Error(Start, "cannot use more than one symbol in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001393 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001394 }
1395 if (SM.getBaseReg()) {
1396 Error(Start, "cannot use base register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001397 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001398 }
1399 if (SM.getIndexReg()) {
1400 Error(Start, "cannot use index register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001401 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001402 }
1403
1404 const MCExpr *Disp = MCConstantExpr::Create(SM.getImm(), getContext());
1405 // BaseReg is non-zero to avoid assertions. In the context of inline asm,
1406 // we're pointing to a local variable in memory, so the base register is
1407 // really the frame or stack pointer.
1408 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/1, /*IndexReg=*/0,
1409 /*Scale=*/1, Start, End, Size, Identifier,
1410 Info.OpDecl);
Chad Rosier91c82662012-10-24 17:22:29 +00001411}
1412
Chad Rosier5dcb4662012-10-24 22:21:50 +00001413/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001414bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001415 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001416 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001417 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001418
1419 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001420 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001421 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001422 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001423 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001424
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001425 // Drop the optional '.'.
1426 StringRef DotDispStr = Tok.getString();
1427 if (DotDispStr.startswith("."))
1428 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001429
Chad Rosier5dcb4662012-10-24 22:21:50 +00001430 // .Imm gets lexed as a real.
1431 if (Tok.is(AsmToken::Real)) {
1432 APInt DotDisp;
1433 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001434 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001435 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001436 unsigned DotDisp;
1437 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1438 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001439 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001440 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001441 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001442 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001443 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001444
Chad Rosier240b7b92012-10-25 21:51:10 +00001445 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1446 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1447 unsigned Len = DotDispStr.size();
1448 unsigned Val = OrigDispVal + DotDispVal;
1449 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1450 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001451 }
1452
Chad Rosiercc541e82013-04-19 15:57:00 +00001453 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001454 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001455}
1456
Chad Rosier91c82662012-10-24 17:22:29 +00001457/// Parse the 'offset' operator. This operator is used to specify the
1458/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001459std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001460 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001461 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001462 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001463
Chad Rosier91c82662012-10-24 17:22:29 +00001464 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001465 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001466 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001467 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001468 if (ParseIntelIdentifier(Val, Identifier, Info,
1469 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001470 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001471
Chad Rosiere2f03772012-10-26 16:09:20 +00001472 // Don't emit the offset operator.
1473 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1474
Chad Rosier91c82662012-10-24 17:22:29 +00001475 // The offset operator will have an 'r' constraint, thus we need to create
1476 // register operand to ensure proper matching. Just pick a GPR based on
1477 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001478 unsigned RegNo =
1479 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001480 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001481 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001482}
1483
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001484enum IntelOperatorKind {
1485 IOK_LENGTH,
1486 IOK_SIZE,
1487 IOK_TYPE
1488};
1489
1490/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1491/// returns the number of elements in an array. It returns the value 1 for
1492/// non-array variables. The SIZE operator returns the size of a C or C++
1493/// variable. A variable's size is the product of its LENGTH and TYPE. The
1494/// TYPE operator returns the size of a C or C++ type or variable. If the
1495/// variable is an array, TYPE returns the size of a single element.
David Blaikie960ea3f2014-06-08 16:18:35 +00001496std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001497 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001498 SMLoc TypeLoc = Tok.getLoc();
1499 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001500
Craig Topper062a2ba2014-04-25 05:30:21 +00001501 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001502 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001503 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001504 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001505 if (ParseIntelIdentifier(Val, Identifier, Info,
1506 /*Unevaluated=*/true, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001507 return nullptr;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001508
1509 if (!Info.OpDecl)
1510 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001511
Chad Rosierf6675c32013-04-22 17:01:46 +00001512 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001513 switch(OpKind) {
1514 default: llvm_unreachable("Unexpected operand kind!");
1515 case IOK_LENGTH: CVal = Info.Length; break;
1516 case IOK_SIZE: CVal = Info.Size; break;
1517 case IOK_TYPE: CVal = Info.Type; break;
1518 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001519
1520 // Rewrite the type operator and the C or C++ type or variable in terms of an
1521 // immediate. E.g. TYPE foo -> $$4
1522 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001523 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001524
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001525 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001526 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001527}
1528
David Blaikie960ea3f2014-06-08 16:18:35 +00001529std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001530 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001531 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001532
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001533 // Offset, length, type and size operators.
1534 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001535 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001536 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001537 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001538 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001539 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001540 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001541 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001542 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001543 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001544 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001545
David Majnemeraa34d792013-08-27 21:56:17 +00001546 unsigned Size = getIntelMemOperandSize(Tok.getString());
1547 if (Size) {
1548 Parser.Lex(); // Eat operand size (e.g., byte, word).
1549 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1550 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1551 Parser.Lex(); // Eat ptr.
1552 }
1553 Start = Tok.getLoc();
1554
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001555 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001556 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001557 getLexer().is(AsmToken::Tilde) || getLexer().is(AsmToken::LParen)) {
Chad Rosierbfb70992013-04-17 00:11:46 +00001558 AsmToken StartTok = Tok;
1559 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1560 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001561 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001562 return nullptr;
Chad Rosierbfb70992013-04-17 00:11:46 +00001563
1564 int64_t Imm = SM.getImm();
1565 if (isParsingInlineAsm()) {
1566 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1567 if (StartTok.getString().size() == Len)
1568 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001569 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001570 else
1571 // Otherwise, rewrite the complex expression as a single immediate.
1572 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001573 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001574
1575 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001576 // If a directional label (ie. 1f or 2b) was parsed above from
1577 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1578 // to the MCExpr with the directional local symbol and this is a
1579 // memory operand not an immediate operand.
1580 if (SM.getSym())
1581 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1582
Chad Rosierbfb70992013-04-17 00:11:46 +00001583 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1584 return X86Operand::CreateImm(ImmExpr, Start, End);
1585 }
1586
1587 // Only positive immediates are valid.
1588 if (Imm < 0)
1589 return ErrorOperand(Start, "expected a positive immediate displacement "
1590 "before bracketed expr.");
1591
1592 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001593 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001594 }
1595
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001596 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001597 unsigned RegNo = 0;
1598 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001599 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001600 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001601 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001602 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001603
David Majnemeraa34d792013-08-27 21:56:17 +00001604 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001605 }
1606
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001607 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001608 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001609}
1610
David Blaikie960ea3f2014-06-08 16:18:35 +00001611std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001612 switch (getLexer().getKind()) {
1613 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001614 // Parse a memory operand with no segment register.
1615 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001616 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001617 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001618 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001619 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001620 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001621 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001622 Error(Start, "%eiz and %riz can only be used as index registers",
1623 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001624 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001625 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001626
Chris Lattnerb9270732010-04-17 18:56:34 +00001627 // If this is a segment register followed by a ':', then this is the start
1628 // of a memory reference, otherwise this is a normal register reference.
1629 if (getLexer().isNot(AsmToken::Colon))
1630 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001631
Chris Lattnerb9270732010-04-17 18:56:34 +00001632 getParser().Lex(); // Eat the colon.
1633 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001634 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001635 case AsmToken::Dollar: {
1636 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001637 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001638 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001639 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001640 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001641 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001642 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001643 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001644 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001645}
1646
David Blaikie960ea3f2014-06-08 16:18:35 +00001647bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1648 const MCParsedAsmOperand &Op) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001649 if(STI.getFeatureBits() & X86::FeatureAVX512) {
1650 if (getLexer().is(AsmToken::LCurly)) {
1651 // Eat "{" and mark the current place.
1652 const SMLoc consumedToken = consumeToken();
1653 // Distinguish {1to<NUM>} from {%k<NUM>}.
1654 if(getLexer().is(AsmToken::Integer)) {
1655 // Parse memory broadcasting ({1to<NUM>}).
1656 if (getLexer().getTok().getIntVal() != 1)
1657 return !ErrorAndEatStatement(getLexer().getLoc(),
1658 "Expected 1to<NUM> at this point");
1659 Parser.Lex(); // Eat "1" of 1to8
1660 if (!getLexer().is(AsmToken::Identifier) ||
1661 !getLexer().getTok().getIdentifier().startswith("to"))
1662 return !ErrorAndEatStatement(getLexer().getLoc(),
1663 "Expected 1to<NUM> at this point");
1664 // Recognize only reasonable suffixes.
1665 const char *BroadcastPrimitive =
1666 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
1667 .Case("to8", "{1to8}")
1668 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001669 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001670 if (!BroadcastPrimitive)
1671 return !ErrorAndEatStatement(getLexer().getLoc(),
1672 "Invalid memory broadcast primitive.");
1673 Parser.Lex(); // Eat "toN" of 1toN
1674 if (!getLexer().is(AsmToken::RCurly))
1675 return !ErrorAndEatStatement(getLexer().getLoc(),
1676 "Expected } at this point");
1677 Parser.Lex(); // Eat "}"
1678 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1679 consumedToken));
1680 // No AVX512 specific primitives can pass
1681 // after memory broadcasting, so return.
1682 return true;
1683 } else {
1684 // Parse mask register {%k1}
1685 Operands.push_back(X86Operand::CreateToken("{", consumedToken));
David Blaikie960ea3f2014-06-08 16:18:35 +00001686 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1687 Operands.push_back(std::move(Op));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001688 if (!getLexer().is(AsmToken::RCurly))
1689 return !ErrorAndEatStatement(getLexer().getLoc(),
1690 "Expected } at this point");
1691 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1692
1693 // Parse "zeroing non-masked" semantic {z}
1694 if (getLexer().is(AsmToken::LCurly)) {
1695 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1696 if (!getLexer().is(AsmToken::Identifier) ||
1697 getLexer().getTok().getIdentifier() != "z")
1698 return !ErrorAndEatStatement(getLexer().getLoc(),
1699 "Expected z at this point");
1700 Parser.Lex(); // Eat the z
1701 if (!getLexer().is(AsmToken::RCurly))
1702 return !ErrorAndEatStatement(getLexer().getLoc(),
1703 "Expected } at this point");
1704 Parser.Lex(); // Eat the }
1705 }
1706 }
1707 }
1708 }
1709 }
1710 return true;
1711}
1712
Chris Lattnerb9270732010-04-17 18:56:34 +00001713/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1714/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00001715std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
1716 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001717
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001718 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1719 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001720 // only way to do this without lookahead is to eat the '(' and see what is
1721 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001722 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001723 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001724 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00001725 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001726
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001727 // After parsing the base expression we could either have a parenthesized
1728 // memory address or not. If not, return now. If so, eat the (.
1729 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001730 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001731 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001732 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001733 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001734 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001735
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001736 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001737 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001738 } else {
1739 // Okay, we have a '('. We don't know if this is an expression or not, but
1740 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001741 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001742 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001743
Kevin Enderby7d912182009-09-03 17:15:07 +00001744 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001745 // Nothing to do here, fall into the code below with the '(' part of the
1746 // memory operand consumed.
1747 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001748 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001749
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001750 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001751 if (getParser().parseParenExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00001752 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001753
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001754 // After parsing the base expression we could either have a parenthesized
1755 // memory address or not. If not, return now. If so, eat the (.
1756 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001757 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001758 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001759 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001760 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001761 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001762
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001763 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001764 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001765 }
1766 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001767
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001768 // If we reached here, then we just ate the ( of the memory operand. Process
1769 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001770 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001771 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001772
Chris Lattner0c2538f2010-01-15 18:51:29 +00001773 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001774 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001775 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00001776 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001777 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001778 Error(StartLoc, "eiz and riz can only be used as index registers",
1779 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00001780 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001781 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001782 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001783
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001784 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001785 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001786 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001787
1788 // Following the comma we should have either an index register, or a scale
1789 // value. We don't support the later form, but we want to parse it
1790 // correctly.
1791 //
1792 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001793 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001794 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001795 SMLoc L;
Craig Topper062a2ba2014-04-25 05:30:21 +00001796 if (ParseRegister(IndexReg, L, L)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001797
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001798 if (getLexer().isNot(AsmToken::RParen)) {
1799 // Parse the scale amount:
1800 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001801 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001802 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001803 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001804 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001805 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001806 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001807
1808 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001809 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001810
1811 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001812 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001813 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001814 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001815 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001816
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001817 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001818 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1819 ScaleVal != 1) {
1820 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00001821 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001822 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001823 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1824 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00001825 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001826 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001827 Scale = (unsigned)ScaleVal;
1828 }
1829 }
1830 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001831 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001832 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001833 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001834
1835 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001836 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00001837 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001838
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001839 if (Value != 1)
1840 Warning(Loc, "scale factor without index register is ignored");
1841 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001842 }
1843 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001844
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001845 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001846 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001847 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001848 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001849 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001850 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001851 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001852
David Woodhouse6dbda442014-01-08 12:58:28 +00001853 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1854 // and then only in non-64-bit modes. Except for DX, which is a special case
1855 // because an unofficial form of in/out instructions uses it.
1856 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1857 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1858 BaseReg != X86::SI && BaseReg != X86::DI)) &&
1859 BaseReg != X86::DX) {
1860 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001861 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001862 }
1863 if (BaseReg == 0 &&
1864 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
1865 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001866 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001867 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001868
1869 StringRef ErrMsg;
1870 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1871 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001872 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001873 }
1874
Chris Lattner015cfb12010-01-15 19:33:43 +00001875 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1876 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001877}
1878
David Blaikie960ea3f2014-06-08 16:18:35 +00001879bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
1880 SMLoc NameLoc, OperandVector &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001881 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001882 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001883
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001884 // FIXME: Hack to recognize setneb as setne.
1885 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1886 PatchedName != "setb" && PatchedName != "setnb")
1887 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001888
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001889 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Craig Topper062a2ba2014-04-25 05:30:21 +00001890 const MCExpr *ExtraImmOp = nullptr;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001891 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001892 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1893 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001894 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001895 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001896 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001897 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001898 .Case("eq", 0x00)
1899 .Case("lt", 0x01)
1900 .Case("le", 0x02)
1901 .Case("unord", 0x03)
1902 .Case("neq", 0x04)
1903 .Case("nlt", 0x05)
1904 .Case("nle", 0x06)
1905 .Case("ord", 0x07)
1906 /* AVX only from here */
1907 .Case("eq_uq", 0x08)
1908 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001909 .Case("ngt", 0x0A)
1910 .Case("false", 0x0B)
1911 .Case("neq_oq", 0x0C)
1912 .Case("ge", 0x0D)
1913 .Case("gt", 0x0E)
1914 .Case("true", 0x0F)
1915 .Case("eq_os", 0x10)
1916 .Case("lt_oq", 0x11)
1917 .Case("le_oq", 0x12)
1918 .Case("unord_s", 0x13)
1919 .Case("neq_us", 0x14)
1920 .Case("nlt_uq", 0x15)
1921 .Case("nle_uq", 0x16)
1922 .Case("ord_s", 0x17)
1923 .Case("eq_us", 0x18)
1924 .Case("nge_uq", 0x19)
1925 .Case("ngt_uq", 0x1A)
1926 .Case("false_os", 0x1B)
1927 .Case("neq_os", 0x1C)
1928 .Case("ge_oq", 0x1D)
1929 .Case("gt_oq", 0x1E)
1930 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001931 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001932 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001933 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1934 getParser().getContext());
1935 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001936 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001937 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001938 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001939 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001940 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001941 } else {
1942 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001943 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001944 }
1945 }
1946 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001947
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001948 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001949
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001950 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001951 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001952
Chris Lattner086a83a2010-09-08 05:17:37 +00001953 // Determine whether this is an instruction prefix.
1954 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001955 Name == "lock" || Name == "rep" ||
1956 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001957 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001958 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001959
1960
Chris Lattner086a83a2010-09-08 05:17:37 +00001961 // This does the actual operand parsing. Don't parse any more if we have a
1962 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1963 // just want to parse the "lock" as the first instruction and the "incl" as
1964 // the next one.
1965 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001966
1967 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00001968 if (getLexer().is(AsmToken::Star))
1969 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00001970
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001971 // Read the operands.
1972 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00001973 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1974 Operands.push_back(std::move(Op));
1975 if (!HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00001976 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001977 } else {
1978 Parser.eatToEndOfStatement();
1979 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00001980 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001981 // check for comma and eat it
1982 if (getLexer().is(AsmToken::Comma))
1983 Parser.Lex();
1984 else
1985 break;
1986 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00001987
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001988 if (getLexer().isNot(AsmToken::EndOfStatement))
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001989 return ErrorAndEatStatement(getLexer().getLoc(),
1990 "unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001991 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001992
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001993 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00001994 if (getLexer().is(AsmToken::EndOfStatement) ||
1995 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001996 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001997
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001998 if (ExtraImmOp && isParsingIntelSyntax())
1999 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2000
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002001 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2002 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2003 // documented form in various unofficial manuals, so a lot of code uses it.
2004 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2005 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002006 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002007 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2008 isa<MCConstantExpr>(Op.Mem.Disp) &&
2009 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2010 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2011 SMLoc Loc = Op.getEndLoc();
2012 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002013 }
2014 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002015 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2016 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2017 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002018 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002019 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2020 isa<MCConstantExpr>(Op.Mem.Disp) &&
2021 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2022 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2023 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002024 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002025 }
2026 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002027
2028 // Append default arguments to "ins[bwld]"
2029 if (Name.startswith("ins") && Operands.size() == 1 &&
2030 (Name == "insb" || Name == "insw" || Name == "insl" ||
2031 Name == "insd" )) {
2032 if (isParsingIntelSyntax()) {
2033 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2034 Operands.push_back(DefaultMemDIOperand(NameLoc));
2035 } else {
2036 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2037 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002038 }
2039 }
2040
David Woodhousec472b812014-01-22 15:08:49 +00002041 // Append default arguments to "outs[bwld]"
2042 if (Name.startswith("outs") && Operands.size() == 1 &&
2043 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
2044 Name == "outsd" )) {
2045 if (isParsingIntelSyntax()) {
2046 Operands.push_back(DefaultMemSIOperand(NameLoc));
2047 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2048 } else {
2049 Operands.push_back(DefaultMemSIOperand(NameLoc));
2050 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002051 }
2052 }
2053
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002054 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2055 // values of $SIREG according to the mode. It would be nice if this
2056 // could be achieved with InstAlias in the tables.
2057 if (Name.startswith("lods") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002058 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002059 Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
2060 Operands.push_back(DefaultMemSIOperand(NameLoc));
2061
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002062 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2063 // values of $DIREG according to the mode. It would be nice if this
2064 // could be achieved with InstAlias in the tables.
2065 if (Name.startswith("stos") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002066 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002067 Name == "stosl" || Name == "stosd" || Name == "stosq"))
2068 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002069
David Woodhouse20fe4802014-01-22 15:08:27 +00002070 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2071 // values of $DIREG according to the mode. It would be nice if this
2072 // could be achieved with InstAlias in the tables.
2073 if (Name.startswith("scas") && Operands.size() == 1 &&
2074 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
2075 Name == "scasl" || Name == "scasd" || Name == "scasq"))
2076 Operands.push_back(DefaultMemDIOperand(NameLoc));
2077
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002078 // Add default SI and DI operands to "cmps[bwlq]".
2079 if (Name.startswith("cmps") &&
2080 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2081 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
2082 if (Operands.size() == 1) {
2083 if (isParsingIntelSyntax()) {
2084 Operands.push_back(DefaultMemSIOperand(NameLoc));
2085 Operands.push_back(DefaultMemDIOperand(NameLoc));
2086 } else {
2087 Operands.push_back(DefaultMemDIOperand(NameLoc));
2088 Operands.push_back(DefaultMemSIOperand(NameLoc));
2089 }
2090 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002091 X86Operand &Op = (X86Operand &)*Operands[1];
2092 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002093 if (!doSrcDstMatch(Op, Op2))
2094 return Error(Op.getStartLoc(),
2095 "mismatching source and destination index registers");
2096 }
2097 }
2098
David Woodhouse6f417de2014-01-22 15:08:42 +00002099 // Add default SI and DI operands to "movs[bwlq]".
2100 if ((Name.startswith("movs") &&
2101 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2102 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2103 (Name.startswith("smov") &&
2104 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2105 Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2106 if (Operands.size() == 1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002107 if (Name == "movsd")
David Woodhouse6f417de2014-01-22 15:08:42 +00002108 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2109 if (isParsingIntelSyntax()) {
2110 Operands.push_back(DefaultMemDIOperand(NameLoc));
2111 Operands.push_back(DefaultMemSIOperand(NameLoc));
2112 } else {
2113 Operands.push_back(DefaultMemSIOperand(NameLoc));
2114 Operands.push_back(DefaultMemDIOperand(NameLoc));
2115 }
2116 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002117 X86Operand &Op = (X86Operand &)*Operands[1];
2118 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse6f417de2014-01-22 15:08:42 +00002119 if (!doSrcDstMatch(Op, Op2))
2120 return Error(Op.getStartLoc(),
2121 "mismatching source and destination index registers");
2122 }
2123 }
2124
Chris Lattner4bd21712010-09-15 04:33:27 +00002125 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002126 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002127 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002128 Name.startswith("shl") || Name.startswith("sal") ||
2129 Name.startswith("rcl") || Name.startswith("rcr") ||
2130 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002131 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002132 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002133 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002134 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2135 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2136 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002137 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002138 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002139 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2140 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2141 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002142 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002143 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002144 }
Chad Rosier51afe632012-06-27 22:34:28 +00002145
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002146 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2147 // instalias with an immediate operand yet.
2148 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002149 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2150 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2151 cast<MCConstantExpr>(Op1.getImm())->getValue() == 3) {
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002152 Operands.erase(Operands.begin() + 1);
David Blaikie960ea3f2014-06-08 16:18:35 +00002153 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002154 }
2155 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002156
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002157 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002158}
2159
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002160static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2161 bool isCmp) {
2162 MCInst TmpInst;
2163 TmpInst.setOpcode(Opcode);
2164 if (!isCmp)
2165 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2166 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2167 TmpInst.addOperand(Inst.getOperand(0));
2168 Inst = TmpInst;
2169 return true;
2170}
2171
2172static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2173 bool isCmp = false) {
2174 if (!Inst.getOperand(0).isImm() ||
2175 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2176 return false;
2177
2178 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2179}
2180
2181static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2182 bool isCmp = false) {
2183 if (!Inst.getOperand(0).isImm() ||
2184 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2185 return false;
2186
2187 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2188}
2189
2190static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2191 bool isCmp = false) {
2192 if (!Inst.getOperand(0).isImm() ||
2193 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2194 return false;
2195
2196 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2197}
2198
David Blaikie960ea3f2014-06-08 16:18:35 +00002199bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Devang Patelde47cce2012-01-18 22:42:29 +00002200 switch (Inst.getOpcode()) {
2201 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002202 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2203 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2204 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2205 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2206 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2207 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2208 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2209 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2210 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2211 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2212 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2213 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2214 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2215 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2216 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2217 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2218 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2219 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002220 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2221 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2222 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2223 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2224 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2225 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002226 case X86::VMOVAPDrr:
2227 case X86::VMOVAPDYrr:
2228 case X86::VMOVAPSrr:
2229 case X86::VMOVAPSYrr:
2230 case X86::VMOVDQArr:
2231 case X86::VMOVDQAYrr:
2232 case X86::VMOVDQUrr:
2233 case X86::VMOVDQUYrr:
2234 case X86::VMOVUPDrr:
2235 case X86::VMOVUPDYrr:
2236 case X86::VMOVUPSrr:
2237 case X86::VMOVUPSYrr: {
2238 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2239 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2240 return false;
2241
2242 unsigned NewOpc;
2243 switch (Inst.getOpcode()) {
2244 default: llvm_unreachable("Invalid opcode");
2245 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2246 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2247 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2248 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2249 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2250 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2251 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2252 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2253 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2254 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2255 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2256 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2257 }
2258 Inst.setOpcode(NewOpc);
2259 return true;
2260 }
2261 case X86::VMOVSDrr:
2262 case X86::VMOVSSrr: {
2263 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2264 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2265 return false;
2266 unsigned NewOpc;
2267 switch (Inst.getOpcode()) {
2268 default: llvm_unreachable("Invalid opcode");
2269 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2270 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2271 }
2272 Inst.setOpcode(NewOpc);
2273 return true;
2274 }
Devang Patelde47cce2012-01-18 22:42:29 +00002275 }
Devang Patelde47cce2012-01-18 22:42:29 +00002276}
2277
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002278static const char *getSubtargetFeatureName(unsigned Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002279
David Blaikie960ea3f2014-06-08 16:18:35 +00002280void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2281 MCStreamer &Out) {
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +00002282 Instrumentation->InstrumentInstruction(Inst, Operands, getContext(), MII,
2283 Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002284 Out.EmitInstruction(Inst, STI);
2285}
2286
David Blaikie960ea3f2014-06-08 16:18:35 +00002287bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2288 OperandVector &Operands,
2289 MCStreamer &Out, unsigned &ErrorInfo,
2290 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002291 assert(!Operands.empty() && "Unexpect empty operand list!");
David Blaikie960ea3f2014-06-08 16:18:35 +00002292 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2293 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002294 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002295
Chris Lattnera63292a2010-09-29 01:50:45 +00002296 // First, handle aliases that expand to multiple instructions.
2297 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002298 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002299 // call.
David Blaikie960ea3f2014-06-08 16:18:35 +00002300 if (Op.getToken() == "fstsw" || Op.getToken() == "fstcw" ||
2301 Op.getToken() == "fstsww" || Op.getToken() == "fstcww" ||
2302 Op.getToken() == "finit" || Op.getToken() == "fsave" ||
2303 Op.getToken() == "fstenv" || Op.getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002304 MCInst Inst;
2305 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002306 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002307 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002308 EmitInstruction(Inst, Operands, Out);
Chris Lattnera63292a2010-09-29 01:50:45 +00002309
David Blaikie960ea3f2014-06-08 16:18:35 +00002310 const char *Repl = StringSwitch<const char *>(Op.getToken())
2311 .Case("finit", "fninit")
2312 .Case("fsave", "fnsave")
2313 .Case("fstcw", "fnstcw")
2314 .Case("fstcww", "fnstcw")
2315 .Case("fstenv", "fnstenv")
2316 .Case("fstsw", "fnstsw")
2317 .Case("fstsww", "fnstsw")
2318 .Case("fclex", "fnclex")
2319 .Default(nullptr);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002320 assert(Repl && "Unknown wait-prefixed instruction");
2321 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002322 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002323
Chris Lattner628fbec2010-09-06 21:54:15 +00002324 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002325 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002326
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002327 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002328 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002329 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002330 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002331 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002332 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002333 // Some instructions need post-processing to, for example, tweak which
2334 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002335 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002336 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002337 while (processInstruction(Inst, Operands))
2338 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002339
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002340 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002341 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002342 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002343 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002344 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002345 case Match_MissingFeature: {
2346 assert(ErrorInfo && "Unknown missing feature!");
2347 // Special case the error message for the very common case where only
2348 // a single subtarget feature is missing.
2349 std::string Msg = "instruction requires:";
2350 unsigned Mask = 1;
2351 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2352 if (ErrorInfo & Mask) {
2353 Msg += " ";
2354 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2355 }
2356 Mask <<= 1;
2357 }
2358 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2359 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002360 case Match_InvalidOperand:
2361 WasOriginallyInvalidOperand = true;
2362 break;
2363 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002364 break;
2365 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002366
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002367 // FIXME: Ideally, we would only attempt suffix matches for things which are
2368 // valid prefixes, and we could just infer the right unambiguous
2369 // type. However, that requires substantially more matcher support than the
2370 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002371
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002372 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002373 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002374 SmallString<16> Tmp;
2375 Tmp += Base;
2376 Tmp += ' ';
David Blaikie960ea3f2014-06-08 16:18:35 +00002377 Op.setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002378
Chris Lattnerfab94132010-11-06 18:28:02 +00002379 // If this instruction starts with an 'f', then it is a floating point stack
2380 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2381 // 80-bit floating point, which use the suffixes s,l,t respectively.
2382 //
2383 // Otherwise, we assume that this may be an integer instruction, which comes
2384 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2385 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002386
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002387 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002388 Tmp[Base.size()] = Suffixes[0];
2389 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002390 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002391 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002392
Chad Rosier2f480a82012-10-12 22:53:36 +00002393 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002394 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002395 // If this returned as a missing feature failure, remember that.
2396 if (Match1 == Match_MissingFeature)
2397 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002398 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002399 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002400 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002401 // If this returned as a missing feature failure, remember that.
2402 if (Match2 == Match_MissingFeature)
2403 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002404 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002405 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002406 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002407 // If this returned as a missing feature failure, remember that.
2408 if (Match3 == Match_MissingFeature)
2409 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002410 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002411 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002412 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002413 // If this returned as a missing feature failure, remember that.
2414 if (Match4 == Match_MissingFeature)
2415 ErrorInfoMissingFeature = ErrorInfoIgnore;
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 =
Chris Lattnerfab94132010-11-06 18:28:02 +00002424 (Match1 == Match_Success) + (Match2 == Match_Success) +
2425 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002426 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002427 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002428 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002429 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002430 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002431 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002432 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002433
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002434 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002435
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002436 // If we had multiple suffix matches, then identify this as an ambiguous
2437 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002438 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002439 char MatchChars[4];
2440 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002441 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2442 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2443 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2444 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002445
Alp Tokere69170a2014-06-26 22:52:05 +00002446 SmallString<126> Msg;
2447 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002448 OS << "ambiguous instructions require an explicit suffix (could be ";
2449 for (unsigned i = 0; i != NumMatches; ++i) {
2450 if (i != 0)
2451 OS << ", ";
2452 if (i + 1 == NumMatches)
2453 OS << "or ";
2454 OS << "'" << Base << MatchChars[i] << "'";
2455 }
2456 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002457 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002458 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002459 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002460
Chris Lattner628fbec2010-09-06 21:54:15 +00002461 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002462
Chris Lattner628fbec2010-09-06 21:54:15 +00002463 // If all of the instructions reported an invalid mnemonic, then the original
2464 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002465 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2466 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002467 if (!WasOriginallyInvalidOperand) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002468 ArrayRef<SMRange> Ranges =
2469 MatchingInlineAsm ? EmptyRanges : Op.getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002470 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002471 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002472 }
2473
2474 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002475 if (ErrorInfo != ~0U) {
2476 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002477 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002478 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002479
David Blaikie960ea3f2014-06-08 16:18:35 +00002480 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2481 if (Operand.getStartLoc().isValid()) {
2482 SMRange OperandRange = Operand.getLocRange();
2483 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002484 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002485 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002486 }
2487
Chad Rosier3d4bc622012-08-21 19:36:59 +00002488 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002489 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002490 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002491
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002492 // If one instruction matched with a missing feature, report this as a
2493 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002494 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2495 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002496 std::string Msg = "instruction requires:";
2497 unsigned Mask = 1;
2498 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2499 if (ErrorInfoMissingFeature & Mask) {
2500 Msg += " ";
2501 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2502 }
2503 Mask <<= 1;
2504 }
2505 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002506 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002507
Chris Lattner628fbec2010-09-06 21:54:15 +00002508 // If one instruction matched with an invalid operand, report this as an
2509 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002510 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2511 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002512 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002513 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002514 return true;
2515 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002516
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002517 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002518 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002519 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002520 return true;
2521}
2522
2523
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"