blob: 8cb75ae0f06448278a11f733f69cbb7959f7e07d [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"
Craig Topper690d8ea2013-07-24 07:33:14 +000014#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000019#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000022#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCParser/MCAsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000026#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/MC/MCRegisterInfo.h"
Michael Zuckerman02ecd432015-12-13 17:07:23 +000028#include "llvm/MC/MCSection.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSubtargetInfo.h"
31#include "llvm/MC/MCSymbol.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000032#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000034#include "llvm/Support/raw_ostream.h"
Reid Kleckner7b1e1a02014-07-30 22:23:11 +000035#include <algorithm>
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000036#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000037
Daniel Dunbar71475772009-07-17 20:42:00 +000038using namespace llvm;
39
40namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000041
Chad Rosier5362af92013-04-16 18:15:40 +000042static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000043 0, // IC_OR
Michael Kupersteine3de07a2015-06-14 12:59:45 +000044 1, // IC_XOR
45 2, // IC_AND
46 3, // IC_LSHIFT
47 3, // IC_RSHIFT
48 4, // IC_PLUS
49 4, // IC_MINUS
50 5, // IC_MULTIPLY
51 5, // IC_DIVIDE
52 6, // IC_RPAREN
53 7, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000054 0, // IC_IMM
55 0 // IC_REGISTER
56};
57
Devang Patel4a6e7782012-01-12 18:03:40 +000058class X86AsmParser : public MCTargetAsmParser {
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;
Nirav Dave6477ce22016-09-26 19:33:36 +000062 bool Code16GCC;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000063
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000064private:
Alp Tokera5b88a52013-12-02 16:06:06 +000065 SMLoc consumeToken() {
Rafael Espindola961d4692014-11-11 05:18:41 +000066 MCAsmParser &Parser = getParser();
Alp Tokera5b88a52013-12-02 16:06:06 +000067 SMLoc Result = Parser.getTok().getLoc();
68 Parser.Lex();
69 return Result;
70 }
71
Nirav Dave6477ce22016-09-26 19:33:36 +000072 unsigned MatchInstruction(const OperandVector &Operands, MCInst &Inst,
73 uint64_t &ErrorInfo, bool matchingInlineAsm,
74 unsigned VariantID = 0) {
75 // In Code16GCC mode, match as 32-bit.
76 if (Code16GCC)
77 SwitchMode(X86::Mode32Bit);
78 unsigned rv = MatchInstructionImpl(Operands, Inst, ErrorInfo,
79 matchingInlineAsm, VariantID);
80 if (Code16GCC)
81 SwitchMode(X86::Mode16Bit);
82 return rv;
83 }
84
Chad Rosier5362af92013-04-16 18:15:40 +000085 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000086 IC_OR = 0,
Michael Kupersteine3de07a2015-06-14 12:59:45 +000087 IC_XOR,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000088 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +000089 IC_LSHIFT,
90 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000091 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000092 IC_MINUS,
93 IC_MULTIPLY,
94 IC_DIVIDE,
95 IC_RPAREN,
96 IC_LPAREN,
97 IC_IMM,
98 IC_REGISTER
99 };
100
101 class InfixCalculator {
102 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
103 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
104 SmallVector<ICToken, 4> PostfixStack;
Michael Liao5bf95782014-12-04 05:20:33 +0000105
Chad Rosier5362af92013-04-16 18:15:40 +0000106 public:
107 int64_t popOperand() {
108 assert (!PostfixStack.empty() && "Poped an empty stack!");
109 ICToken Op = PostfixStack.pop_back_val();
110 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
111 && "Expected and immediate or register!");
112 return Op.second;
113 }
114 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
115 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
116 "Unexpected operand!");
117 PostfixStack.push_back(std::make_pair(Op, Val));
118 }
Michael Liao5bf95782014-12-04 05:20:33 +0000119
Jakub Staszak9c349222013-08-08 15:48:46 +0000120 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000121 void pushOperator(InfixCalculatorTok Op) {
122 // Push the new operator if the stack is empty.
123 if (InfixOperatorStack.empty()) {
124 InfixOperatorStack.push_back(Op);
125 return;
126 }
Michael Liao5bf95782014-12-04 05:20:33 +0000127
Chad Rosier5362af92013-04-16 18:15:40 +0000128 // Push the new operator if it has a higher precedence than the operator
129 // on the top of the stack or the operator on the top of the stack is a
130 // left parentheses.
131 unsigned Idx = InfixOperatorStack.size() - 1;
132 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
133 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
134 InfixOperatorStack.push_back(Op);
135 return;
136 }
Michael Liao5bf95782014-12-04 05:20:33 +0000137
Chad Rosier5362af92013-04-16 18:15:40 +0000138 // The operator on the top of the stack has higher precedence than the
139 // new operator.
140 unsigned ParenCount = 0;
141 while (1) {
142 // Nothing to process.
143 if (InfixOperatorStack.empty())
144 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000145
Chad Rosier5362af92013-04-16 18:15:40 +0000146 Idx = InfixOperatorStack.size() - 1;
147 StackOp = InfixOperatorStack[Idx];
148 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
149 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000150
Chad Rosier5362af92013-04-16 18:15:40 +0000151 // If we have an even parentheses count and we see a left parentheses,
152 // then stop processing.
153 if (!ParenCount && StackOp == IC_LPAREN)
154 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000155
Chad Rosier5362af92013-04-16 18:15:40 +0000156 if (StackOp == IC_RPAREN) {
157 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000158 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000159 } else if (StackOp == IC_LPAREN) {
160 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000161 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000162 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000163 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000164 PostfixStack.push_back(std::make_pair(StackOp, 0));
165 }
166 }
167 // Push the new operator.
168 InfixOperatorStack.push_back(Op);
169 }
Marina Yatsinaa0e02412015-08-10 11:33:10 +0000170
Chad Rosier5362af92013-04-16 18:15:40 +0000171 int64_t execute() {
172 // Push any remaining operators onto the postfix stack.
173 while (!InfixOperatorStack.empty()) {
174 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
175 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
176 PostfixStack.push_back(std::make_pair(StackOp, 0));
177 }
Michael Liao5bf95782014-12-04 05:20:33 +0000178
Chad Rosier5362af92013-04-16 18:15:40 +0000179 if (PostfixStack.empty())
180 return 0;
Michael Liao5bf95782014-12-04 05:20:33 +0000181
Chad Rosier5362af92013-04-16 18:15:40 +0000182 SmallVector<ICToken, 16> OperandStack;
183 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
184 ICToken Op = PostfixStack[i];
185 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
186 OperandStack.push_back(Op);
187 } else {
188 assert (OperandStack.size() > 1 && "Too few operands.");
189 int64_t Val;
190 ICToken Op2 = OperandStack.pop_back_val();
191 ICToken Op1 = OperandStack.pop_back_val();
192 switch (Op.first) {
193 default:
194 report_fatal_error("Unexpected operator!");
195 break;
196 case IC_PLUS:
197 Val = Op1.second + Op2.second;
198 OperandStack.push_back(std::make_pair(IC_IMM, Val));
199 break;
200 case IC_MINUS:
201 Val = Op1.second - Op2.second;
202 OperandStack.push_back(std::make_pair(IC_IMM, Val));
203 break;
204 case IC_MULTIPLY:
205 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
206 "Multiply operation with an immediate and a register!");
207 Val = Op1.second * Op2.second;
208 OperandStack.push_back(std::make_pair(IC_IMM, Val));
209 break;
210 case IC_DIVIDE:
211 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
212 "Divide operation with an immediate and a register!");
213 assert (Op2.second != 0 && "Division by zero!");
214 Val = Op1.second / Op2.second;
215 OperandStack.push_back(std::make_pair(IC_IMM, Val));
216 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000217 case IC_OR:
218 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
219 "Or 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;
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000223 case IC_XOR:
224 assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
225 "Xor operation with an immediate and a register!");
226 Val = Op1.second ^ Op2.second;
227 OperandStack.push_back(std::make_pair(IC_IMM, Val));
228 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000229 case IC_AND:
230 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
231 "And operation with an immediate and a register!");
232 Val = Op1.second & Op2.second;
233 OperandStack.push_back(std::make_pair(IC_IMM, Val));
234 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000235 case IC_LSHIFT:
236 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
237 "Left shift operation with an immediate and a register!");
238 Val = Op1.second << Op2.second;
239 OperandStack.push_back(std::make_pair(IC_IMM, Val));
240 break;
241 case IC_RSHIFT:
242 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
243 "Right shift operation with an immediate and a register!");
244 Val = Op1.second >> Op2.second;
245 OperandStack.push_back(std::make_pair(IC_IMM, Val));
246 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000247 }
248 }
249 }
250 assert (OperandStack.size() == 1 && "Expected a single result.");
251 return OperandStack.pop_back_val().second;
252 }
253 };
254
255 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000256 IES_OR,
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000257 IES_XOR,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000258 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000259 IES_LSHIFT,
260 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000261 IES_PLUS,
262 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000263 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000264 IES_MULTIPLY,
265 IES_DIVIDE,
266 IES_LBRAC,
267 IES_RBRAC,
268 IES_LPAREN,
269 IES_RPAREN,
270 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000271 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000272 IES_IDENTIFIER,
273 IES_ERROR
274 };
275
276 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000277 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000278 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000279 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000280 const MCExpr *Sym;
281 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000282 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000283 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000284 InlineAsmIdentifierInfo Info;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000285
Chad Rosier5362af92013-04-16 18:15:40 +0000286 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000287 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000288 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
Craig Topper062a2ba2014-04-25 05:30:21 +0000289 Scale(1), Imm(imm), Sym(nullptr), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000290 AddImmPrefix(addimmprefix) { Info.clear(); }
Michael Liao5bf95782014-12-04 05:20:33 +0000291
Chad Rosier5362af92013-04-16 18:15:40 +0000292 unsigned getBaseReg() { return BaseReg; }
293 unsigned getIndexReg() { return IndexReg; }
294 unsigned getScale() { return Scale; }
295 const MCExpr *getSym() { return Sym; }
296 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000297 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000298 bool isValidEndState() {
299 return State == IES_RBRAC || State == IES_INTEGER;
300 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000301 bool getStopOnLBrac() { return StopOnLBrac; }
302 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000303 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000304
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000305 InlineAsmIdentifierInfo &getIdentifierInfo() {
306 return Info;
307 }
308
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000309 void onOr() {
310 IntelExprState CurrState = State;
311 switch (State) {
312 default:
313 State = IES_ERROR;
314 break;
315 case IES_INTEGER:
316 case IES_RPAREN:
317 case IES_REGISTER:
318 State = IES_OR;
319 IC.pushOperator(IC_OR);
320 break;
321 }
322 PrevState = CurrState;
323 }
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000324 void onXor() {
325 IntelExprState CurrState = State;
326 switch (State) {
327 default:
328 State = IES_ERROR;
329 break;
330 case IES_INTEGER:
331 case IES_RPAREN:
332 case IES_REGISTER:
333 State = IES_XOR;
334 IC.pushOperator(IC_XOR);
335 break;
336 }
337 PrevState = CurrState;
338 }
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000339 void onAnd() {
340 IntelExprState CurrState = State;
341 switch (State) {
342 default:
343 State = IES_ERROR;
344 break;
345 case IES_INTEGER:
346 case IES_RPAREN:
347 case IES_REGISTER:
348 State = IES_AND;
349 IC.pushOperator(IC_AND);
350 break;
351 }
352 PrevState = CurrState;
353 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000354 void onLShift() {
355 IntelExprState CurrState = State;
356 switch (State) {
357 default:
358 State = IES_ERROR;
359 break;
360 case IES_INTEGER:
361 case IES_RPAREN:
362 case IES_REGISTER:
363 State = IES_LSHIFT;
364 IC.pushOperator(IC_LSHIFT);
365 break;
366 }
367 PrevState = CurrState;
368 }
369 void onRShift() {
370 IntelExprState CurrState = State;
371 switch (State) {
372 default:
373 State = IES_ERROR;
374 break;
375 case IES_INTEGER:
376 case IES_RPAREN:
377 case IES_REGISTER:
378 State = IES_RSHIFT;
379 IC.pushOperator(IC_RSHIFT);
380 break;
381 }
382 PrevState = CurrState;
383 }
Chad Rosier5362af92013-04-16 18:15:40 +0000384 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000385 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000386 switch (State) {
387 default:
388 State = IES_ERROR;
389 break;
390 case IES_INTEGER:
391 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000392 case IES_REGISTER:
393 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000394 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000395 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
396 // If we already have a BaseReg, then assume this is the IndexReg with
397 // a scale of 1.
398 if (!BaseReg) {
399 BaseReg = TmpReg;
400 } else {
401 assert (!IndexReg && "BaseReg/IndexReg already set!");
402 IndexReg = TmpReg;
403 Scale = 1;
404 }
405 }
Chad Rosier5362af92013-04-16 18:15:40 +0000406 break;
407 }
Chad Rosier31246272013-04-17 21:01:45 +0000408 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000409 }
410 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000411 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000412 switch (State) {
413 default:
414 State = IES_ERROR;
415 break;
416 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000417 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000418 case IES_MULTIPLY:
419 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000420 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000421 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000422 case IES_LBRAC:
423 case IES_RBRAC:
424 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000425 case IES_REGISTER:
426 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000427 // Only push the minus operator if it is not a unary operator.
428 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
429 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
430 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
431 IC.pushOperator(IC_MINUS);
432 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
433 // If we already have a BaseReg, then assume this is the IndexReg with
434 // a scale of 1.
435 if (!BaseReg) {
436 BaseReg = TmpReg;
437 } else {
438 assert (!IndexReg && "BaseReg/IndexReg already set!");
439 IndexReg = TmpReg;
440 Scale = 1;
441 }
Chad Rosier5362af92013-04-16 18:15:40 +0000442 }
Chad Rosier5362af92013-04-16 18:15:40 +0000443 break;
444 }
Chad Rosier31246272013-04-17 21:01:45 +0000445 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000446 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000447 void onNot() {
448 IntelExprState CurrState = State;
449 switch (State) {
450 default:
451 State = IES_ERROR;
452 break;
453 case IES_PLUS:
454 case IES_NOT:
455 State = IES_NOT;
456 break;
457 }
458 PrevState = CurrState;
459 }
Chad Rosier5362af92013-04-16 18:15:40 +0000460 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000461 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000462 switch (State) {
463 default:
464 State = IES_ERROR;
465 break;
466 case IES_PLUS:
467 case IES_LPAREN:
468 State = IES_REGISTER;
469 TmpReg = Reg;
470 IC.pushOperand(IC_REGISTER);
471 break;
Chad Rosier31246272013-04-17 21:01:45 +0000472 case IES_MULTIPLY:
473 // Index Register - Scale * Register
474 if (PrevState == IES_INTEGER) {
475 assert (!IndexReg && "IndexReg already set!");
476 State = IES_REGISTER;
477 IndexReg = Reg;
478 // Get the scale and replace the 'Scale * Register' with '0'.
479 Scale = IC.popOperand();
480 IC.pushOperand(IC_IMM);
481 IC.popOperator();
482 } else {
483 State = IES_ERROR;
484 }
Chad Rosier5362af92013-04-16 18:15:40 +0000485 break;
486 }
Chad Rosier31246272013-04-17 21:01:45 +0000487 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000488 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000489 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000490 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000491 switch (State) {
492 default:
493 State = IES_ERROR;
494 break;
495 case IES_PLUS:
496 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000497 case IES_NOT:
Chad Rosier5362af92013-04-16 18:15:40 +0000498 State = IES_INTEGER;
499 Sym = SymRef;
500 SymName = SymRefName;
501 IC.pushOperand(IC_IMM);
502 break;
503 }
504 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000505 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000506 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000507 switch (State) {
508 default:
509 State = IES_ERROR;
510 break;
511 case IES_PLUS:
512 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000513 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000514 case IES_OR:
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000515 case IES_XOR:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000516 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000517 case IES_LSHIFT:
518 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000519 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000520 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000521 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000522 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000523 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
524 // Index Register - Register * Scale
525 assert (!IndexReg && "IndexReg already set!");
526 IndexReg = TmpReg;
527 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000528 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
529 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
530 return true;
531 }
Chad Rosier31246272013-04-17 21:01:45 +0000532 // Get the scale and replace the 'Register * Scale' with '0'.
533 IC.popOperator();
534 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000535 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000536 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosier31246272013-04-17 21:01:45 +0000537 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000538 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000539 PrevState == IES_NOT || PrevState == IES_XOR) &&
Chad Rosier31246272013-04-17 21:01:45 +0000540 CurrState == IES_MINUS) {
541 // Unary minus. No need to pop the minus operand because it was never
542 // pushed.
543 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000544 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
545 PrevState == IES_OR || PrevState == IES_AND ||
546 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
547 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
548 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000549 PrevState == IES_NOT || PrevState == IES_XOR) &&
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000550 CurrState == IES_NOT) {
551 // Unary not. No need to pop the not operand because it was never
552 // pushed.
553 IC.pushOperand(IC_IMM, ~TmpInt); // Push ~Imm.
Chad Rosier31246272013-04-17 21:01:45 +0000554 } else {
555 IC.pushOperand(IC_IMM, TmpInt);
556 }
Chad Rosier5362af92013-04-16 18:15:40 +0000557 break;
558 }
Chad Rosier31246272013-04-17 21:01:45 +0000559 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000560 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000561 }
562 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000563 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000564 switch (State) {
565 default:
566 State = IES_ERROR;
567 break;
568 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000569 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000570 case IES_RPAREN:
571 State = IES_MULTIPLY;
572 IC.pushOperator(IC_MULTIPLY);
573 break;
574 }
575 }
576 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000577 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000578 switch (State) {
579 default:
580 State = IES_ERROR;
581 break;
582 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000583 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000584 State = IES_DIVIDE;
585 IC.pushOperator(IC_DIVIDE);
586 break;
587 }
588 }
589 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000590 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000591 switch (State) {
592 default:
593 State = IES_ERROR;
594 break;
595 case IES_RBRAC:
596 State = IES_PLUS;
597 IC.pushOperator(IC_PLUS);
598 break;
599 }
600 }
601 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000602 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000603 switch (State) {
604 default:
605 State = IES_ERROR;
606 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000607 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000608 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000609 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000610 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000611 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
612 // If we already have a BaseReg, then assume this is the IndexReg with
613 // a scale of 1.
614 if (!BaseReg) {
615 BaseReg = TmpReg;
616 } else {
617 assert (!IndexReg && "BaseReg/IndexReg already set!");
618 IndexReg = TmpReg;
619 Scale = 1;
620 }
Chad Rosier5362af92013-04-16 18:15:40 +0000621 }
622 break;
623 }
Chad Rosier31246272013-04-17 21:01:45 +0000624 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000625 }
626 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000627 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000628 switch (State) {
629 default:
630 State = IES_ERROR;
631 break;
632 case IES_PLUS:
633 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000634 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000635 case IES_OR:
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000636 case IES_XOR:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000637 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000638 case IES_LSHIFT:
639 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000640 case IES_MULTIPLY:
641 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000642 case IES_LPAREN:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000643 // FIXME: We don't handle this type of unary minus or not, yet.
Chad Rosierdb003992013-04-18 16:28:19 +0000644 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000645 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000646 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosierdb003992013-04-18 16:28:19 +0000647 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000648 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000649 PrevState == IES_NOT || PrevState == IES_XOR) &&
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000650 (CurrState == IES_MINUS || CurrState == IES_NOT)) {
Chad Rosierdb003992013-04-18 16:28:19 +0000651 State = IES_ERROR;
652 break;
653 }
Chad Rosier5362af92013-04-16 18:15:40 +0000654 State = IES_LPAREN;
655 IC.pushOperator(IC_LPAREN);
656 break;
657 }
Chad Rosier31246272013-04-17 21:01:45 +0000658 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000659 }
660 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000661 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000662 switch (State) {
663 default:
664 State = IES_ERROR;
665 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000666 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000667 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000668 case IES_RPAREN:
669 State = IES_RPAREN;
670 IC.pushOperator(IC_RPAREN);
671 break;
672 }
673 }
674 };
675
Nirav Dave2364748a2016-09-16 18:30:20 +0000676 bool Error(SMLoc L, const Twine &Msg, SMRange Range = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000677 bool MatchingInlineAsm = false) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000678 MCAsmParser &Parser = getParser();
Nirav Dave2364748a2016-09-16 18:30:20 +0000679 if (MatchingInlineAsm) {
680 if (!getLexer().isAtStartOfStatement())
681 Parser.eatToEndOfStatement();
682 return false;
683 }
684 return Parser.Error(L, Msg, Range);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000685 }
686
David Blaikie960ea3f2014-06-08 16:18:35 +0000687 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000688 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000689 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000690 }
691
David Blaikie960ea3f2014-06-08 16:18:35 +0000692 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
693 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
Marina Yatsinab9f4f622016-01-19 15:37:56 +0000694 bool IsSIReg(unsigned Reg);
695 unsigned GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, bool IsSIReg);
696 void
697 AddDefaultSrcDestOperands(OperandVector &Operands,
698 std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
699 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst);
700 bool VerifyAndAdjustOperands(OperandVector &OrigOperands,
701 OperandVector &FinalOperands);
David Blaikie960ea3f2014-06-08 16:18:35 +0000702 std::unique_ptr<X86Operand> ParseOperand();
703 std::unique_ptr<X86Operand> ParseATTOperand();
704 std::unique_ptr<X86Operand> ParseIntelOperand();
705 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000706 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
David Blaikie960ea3f2014-06-08 16:18:35 +0000707 std::unique_ptr<X86Operand> ParseIntelOperator(unsigned OpKind);
708 std::unique_ptr<X86Operand>
709 ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
Elena Demikhovsky18fd4962015-03-02 15:00:34 +0000710 std::unique_ptr<X86Operand> ParseRoundingModeOp(SMLoc Start, SMLoc End);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000711 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Nirav Dave8601ac12016-08-02 17:56:03 +0000712 std::unique_ptr<X86Operand>
713 ParseIntelBracExpression(unsigned SegReg, SMLoc Start, int64_t ImmDisp,
714 bool isSymbol, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000715 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
716 InlineAsmIdentifierInfo &Info,
717 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000718
David Blaikie960ea3f2014-06-08 16:18:35 +0000719 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000720
David Blaikie960ea3f2014-06-08 16:18:35 +0000721 std::unique_ptr<X86Operand>
722 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
723 unsigned IndexReg, unsigned Scale, SMLoc Start,
724 SMLoc End, unsigned Size, StringRef Identifier,
725 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000726
Michael Zuckerman02ecd432015-12-13 17:07:23 +0000727 bool parseDirectiveEven(SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000728 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000729 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000730
David Blaikie960ea3f2014-06-08 16:18:35 +0000731 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000732
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000733 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
734 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000735 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000736
Chad Rosier49963552012-10-13 00:26:04 +0000737 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000738 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000739 uint64_t &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000740 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000741
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000742 void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands,
743 MCStreamer &Out, bool MatchingInlineAsm);
744
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000745 bool ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000746 bool MatchingInlineAsm);
747
748 bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
749 OperandVector &Operands, MCStreamer &Out,
750 uint64_t &ErrorInfo,
751 bool MatchingInlineAsm);
752
753 bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
754 OperandVector &Operands, MCStreamer &Out,
755 uint64_t &ErrorInfo,
756 bool MatchingInlineAsm);
757
Craig Topperfd38cbe2014-08-30 16:48:34 +0000758 bool OmitRegisterFromClobberLists(unsigned RegNo) override;
Nico Weber42f79db2014-07-17 20:24:55 +0000759
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000760 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
761 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
762 /// \return \c true if no parsing errors occurred, \c false otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000763 bool HandleAVX512Operand(OperandVector &Operands,
764 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000765
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000766 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000767 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000768 return getSTI().getFeatureBits()[X86::Mode64Bit];
Evan Cheng4d1ca962011-07-08 01:53:10 +0000769 }
Craig Topper3c80d622014-01-06 04:55:54 +0000770 bool is32BitMode() const {
771 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000772 return getSTI().getFeatureBits()[X86::Mode32Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000773 }
774 bool is16BitMode() const {
775 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000776 return getSTI().getFeatureBits()[X86::Mode16Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000777 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000778 void SwitchMode(unsigned mode) {
Akira Hatanakab11ef082015-11-14 06:35:56 +0000779 MCSubtargetInfo &STI = copySTI();
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000780 FeatureBitset AllModes({X86::Mode64Bit, X86::Mode32Bit, X86::Mode16Bit});
781 FeatureBitset OldMode = STI.getFeatureBits() & AllModes;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000782 unsigned FB = ComputeAvailableFeatures(
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000783 STI.ToggleFeature(OldMode.flip(mode)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000784 setAvailableFeatures(FB);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000785
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000786 assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes));
Evan Cheng481ebb02011-07-27 00:38:12 +0000787 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000788
Reid Kleckner5b37c182014-08-01 20:21:24 +0000789 unsigned getPointerWidth() {
790 if (is16BitMode()) return 16;
791 if (is32BitMode()) return 32;
792 if (is64BitMode()) return 64;
793 llvm_unreachable("invalid mode");
794 }
795
Chad Rosierc2f055d2013-04-18 16:13:18 +0000796 bool isParsingIntelSyntax() {
797 return getParser().getAssemblerDialect();
798 }
799
Daniel Dunbareefe8612010-07-19 05:44:09 +0000800 /// @name Auto-generated Matcher Functions
801 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000802
Chris Lattner3e4582a2010-09-06 19:11:01 +0000803#define GET_ASSEMBLER_HEADER
804#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000805
Daniel Dunbar00331992009-07-29 00:02:19 +0000806 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000807
808public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000809 X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser,
Rafael Espindola961d4692014-11-11 05:18:41 +0000810 const MCInstrInfo &mii, const MCTargetOptions &Options)
Nirav Dave6477ce22016-09-26 19:33:36 +0000811 : MCTargetAsmParser(Options, sti), MII(mii), InstInfo(nullptr),
812 Code16GCC(false) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000813
Daniel Dunbareefe8612010-07-19 05:44:09 +0000814 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000815 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000816 Instrumentation.reset(
817 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000818 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000819
Craig Topper39012cc2014-03-09 18:03:14 +0000820 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000821
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000822 void SetFrameRegister(unsigned RegNo) override;
823
David Blaikie960ea3f2014-06-08 16:18:35 +0000824 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
825 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000826
Craig Topper39012cc2014-03-09 18:03:14 +0000827 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000828};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000829} // end anonymous namespace
830
Sean Callanan86c11812010-01-23 00:40:33 +0000831/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000832/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000833
Chris Lattner60db0a62010-02-09 00:34:28 +0000834static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000835
836/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000837
Kevin Enderbybc570f22014-01-23 22:34:42 +0000838static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
839 StringRef &ErrMsg) {
840 // If we have both a base register and an index register make sure they are
841 // both 64-bit or 32-bit registers.
842 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Douglas Katzman0411e862016-10-05 15:23:35 +0000843
844 if ((BaseReg == X86::RIP && IndexReg != 0) || (IndexReg == X86::RIP)) {
845 ErrMsg = "invalid base+index expression";
846 return true;
847 }
Kevin Enderbybc570f22014-01-23 22:34:42 +0000848 if (BaseReg != 0 && IndexReg != 0) {
849 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
850 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
851 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
852 IndexReg != X86::RIZ) {
853 ErrMsg = "base register is 64-bit, but index register is not";
854 return true;
855 }
856 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
857 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
858 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
859 IndexReg != X86::EIZ){
860 ErrMsg = "base register is 32-bit, but index register is not";
861 return true;
862 }
863 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
864 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
865 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
866 ErrMsg = "base register is 16-bit, but index register is not";
867 return true;
868 }
869 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
870 IndexReg != X86::SI && IndexReg != X86::DI) ||
871 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
872 IndexReg != X86::BX && IndexReg != X86::BP)) {
873 ErrMsg = "invalid 16-bit base/index register combination";
874 return true;
875 }
876 }
877 }
878 return false;
879}
880
Devang Patel4a6e7782012-01-12 18:03:40 +0000881bool X86AsmParser::ParseRegister(unsigned &RegNo,
882 SMLoc &StartLoc, SMLoc &EndLoc) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000883 MCAsmParser &Parser = getParser();
Chris Lattnercc2ad082010-01-15 18:27:19 +0000884 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000885 const AsmToken &PercentTok = Parser.getTok();
886 StartLoc = PercentTok.getLoc();
887
888 // If we encounter a %, ignore it. This code handles registers with and
889 // without the prefix, unprefixed registers can occur in cfi directives.
890 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000891 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000892
Sean Callanan936b0d32010-01-19 21:44:56 +0000893 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000894 EndLoc = Tok.getEndLoc();
895
Devang Patelce6a2ca2012-01-20 22:32:05 +0000896 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000897 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000898 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000900 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000901
Kevin Enderby7d912182009-09-03 17:15:07 +0000902 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000903
Chris Lattner1261b812010-09-22 04:11:10 +0000904 // If the match failed, try the register name as lowercase.
905 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000906 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000907
Michael Kupersteincdb076b2015-07-30 10:10:25 +0000908 // The "flags" register cannot be referenced directly.
909 // Treat it as an identifier instead.
910 if (isParsingInlineAsm() && isParsingIntelSyntax() && RegNo == X86::EFLAGS)
911 RegNo = 0;
912
Evan Chengeda1d4f2011-07-27 23:22:03 +0000913 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000914 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000915 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
916 // checked.
917 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
918 // REX prefix.
919 if (RegNo == X86::RIZ ||
920 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
921 X86II::isX86_64NonExtLowByteReg(RegNo) ||
Craig Topper6acca802016-08-27 17:13:37 +0000922 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000923 return Error(StartLoc, "register %"
924 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000925 SMRange(StartLoc, EndLoc));
Craig Topper29c22732016-02-26 05:29:32 +0000926 } else if (!getSTI().getFeatureBits()[X86::FeatureAVX512]) {
927 if (X86II::is32ExtendedReg(RegNo))
928 return Error(StartLoc, "register %"
Craig Topperd50b5f82016-02-26 06:50:24 +0000929 + Tok.getString() + " is only available with AVX512",
Craig Topper29c22732016-02-26 05:29:32 +0000930 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000931 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000932
Chris Lattner1261b812010-09-22 04:11:10 +0000933 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
934 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000935 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000936 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000937
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000938 // Check to see if we have '(4)' after %st.
939 if (getLexer().isNot(AsmToken::LParen))
940 return false;
941 // Lex the paren.
942 getParser().Lex();
943
944 const AsmToken &IntTok = Parser.getTok();
945 if (IntTok.isNot(AsmToken::Integer))
946 return Error(IntTok.getLoc(), "expected stack index");
947 switch (IntTok.getIntVal()) {
948 case 0: RegNo = X86::ST0; break;
949 case 1: RegNo = X86::ST1; break;
950 case 2: RegNo = X86::ST2; break;
951 case 3: RegNo = X86::ST3; break;
952 case 4: RegNo = X86::ST4; break;
953 case 5: RegNo = X86::ST5; break;
954 case 6: RegNo = X86::ST6; break;
955 case 7: RegNo = X86::ST7; break;
956 default: return Error(IntTok.getLoc(), "invalid stack index");
957 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000958
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000959 if (getParser().Lex().isNot(AsmToken::RParen))
960 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000961
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000962 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000963 Parser.Lex(); // Eat ')'
964 return false;
965 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000966
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000967 EndLoc = Parser.getTok().getEndLoc();
968
Chris Lattner80486622010-06-24 07:29:18 +0000969 // If this is "db[0-7]", match it as an alias
970 // for dr[0-7].
971 if (RegNo == 0 && Tok.getString().size() == 3 &&
972 Tok.getString().startswith("db")) {
973 switch (Tok.getString()[2]) {
974 case '0': RegNo = X86::DR0; break;
975 case '1': RegNo = X86::DR1; break;
976 case '2': RegNo = X86::DR2; break;
977 case '3': RegNo = X86::DR3; break;
978 case '4': RegNo = X86::DR4; break;
979 case '5': RegNo = X86::DR5; break;
980 case '6': RegNo = X86::DR6; break;
981 case '7': RegNo = X86::DR7; break;
982 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000983
Chris Lattner80486622010-06-24 07:29:18 +0000984 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000985 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000986 Parser.Lex(); // Eat it.
987 return false;
988 }
989 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000990
Devang Patelce6a2ca2012-01-20 22:32:05 +0000991 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000992 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000993 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000994 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000995 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000996
Sean Callanana83fd7d2010-01-19 20:27:46 +0000997 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000998 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000999}
1000
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001001void X86AsmParser::SetFrameRegister(unsigned RegNo) {
Yuri Gorshenine8c81fd2014-10-07 11:03:09 +00001002 Instrumentation->SetInitialFrameRegister(RegNo);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001003}
1004
David Blaikie960ea3f2014-06-08 16:18:35 +00001005std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001006 bool Parse32 = is32BitMode() || Code16GCC;
1007 unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001008 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001009 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001010 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001011 Loc, Loc, 0);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001012}
1013
David Blaikie960ea3f2014-06-08 16:18:35 +00001014std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001015 bool Parse32 = is32BitMode() || Code16GCC;
1016 unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001017 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001018 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001019 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001020 Loc, Loc, 0);
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001021}
1022
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001023bool X86AsmParser::IsSIReg(unsigned Reg) {
1024 switch (Reg) {
Craig Topper4d187632016-02-26 05:29:39 +00001025 default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001026 case X86::RSI:
1027 case X86::ESI:
1028 case X86::SI:
1029 return true;
1030 case X86::RDI:
1031 case X86::EDI:
1032 case X86::DI:
1033 return false;
1034 }
1035}
1036
1037unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg,
1038 bool IsSIReg) {
1039 switch (RegClassID) {
Craig Topper4d187632016-02-26 05:29:39 +00001040 default: llvm_unreachable("Unexpected register class");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001041 case X86::GR64RegClassID:
1042 return IsSIReg ? X86::RSI : X86::RDI;
1043 case X86::GR32RegClassID:
1044 return IsSIReg ? X86::ESI : X86::EDI;
1045 case X86::GR16RegClassID:
1046 return IsSIReg ? X86::SI : X86::DI;
1047 }
1048}
1049
Michael Kupersteinffcc7662015-07-23 10:23:48 +00001050void X86AsmParser::AddDefaultSrcDestOperands(
1051 OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
1052 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) {
1053 if (isParsingIntelSyntax()) {
1054 Operands.push_back(std::move(Dst));
1055 Operands.push_back(std::move(Src));
1056 }
1057 else {
1058 Operands.push_back(std::move(Src));
1059 Operands.push_back(std::move(Dst));
1060 }
1061}
1062
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001063bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands,
1064 OperandVector &FinalOperands) {
1065
1066 if (OrigOperands.size() > 1) {
Craig Topperd55f4bc2016-02-16 07:45:07 +00001067 // Check if sizes match, OrigOperands also contains the instruction name
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001068 assert(OrigOperands.size() == FinalOperands.size() + 1 &&
Craig Topperd55f4bc2016-02-16 07:45:07 +00001069 "Operand size mismatch");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001070
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001071 SmallVector<std::pair<SMLoc, std::string>, 2> Warnings;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001072 // Verify types match
1073 int RegClassID = -1;
1074 for (unsigned int i = 0; i < FinalOperands.size(); ++i) {
1075 X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]);
1076 X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]);
1077
1078 if (FinalOp.isReg() &&
1079 (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg()))
1080 // Return false and let a normal complaint about bogus operands happen
1081 return false;
1082
1083 if (FinalOp.isMem()) {
1084
1085 if (!OrigOp.isMem())
1086 // Return false and let a normal complaint about bogus operands happen
1087 return false;
1088
1089 unsigned OrigReg = OrigOp.Mem.BaseReg;
1090 unsigned FinalReg = FinalOp.Mem.BaseReg;
1091
1092 // If we've already encounterd a register class, make sure all register
1093 // bases are of the same register class
1094 if (RegClassID != -1 &&
1095 !X86MCRegisterClasses[RegClassID].contains(OrigReg)) {
1096 return Error(OrigOp.getStartLoc(),
1097 "mismatching source and destination index registers");
1098 }
1099
1100 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg))
1101 RegClassID = X86::GR64RegClassID;
1102 else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg))
1103 RegClassID = X86::GR32RegClassID;
1104 else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg))
1105 RegClassID = X86::GR16RegClassID;
Marina Yatsina701938d2016-01-20 14:03:47 +00001106 else
Craig Topper5a62f7e2016-02-16 07:28:03 +00001107 // Unexpected register class type
Marina Yatsina701938d2016-01-20 14:03:47 +00001108 // Return false and let a normal complaint about bogus operands happen
1109 return false;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001110
1111 bool IsSI = IsSIReg(FinalReg);
1112 FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI);
1113
1114 if (FinalReg != OrigReg) {
1115 std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI";
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001116 Warnings.push_back(std::make_pair(
1117 OrigOp.getStartLoc(),
1118 "memory operand is only for determining the size, " + RegName +
1119 " will be used for the location"));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001120 }
1121
1122 FinalOp.Mem.Size = OrigOp.Mem.Size;
1123 FinalOp.Mem.SegReg = OrigOp.Mem.SegReg;
1124 FinalOp.Mem.BaseReg = FinalReg;
1125 }
1126 }
1127
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001128 // Produce warnings only if all the operands passed the adjustment - prevent
1129 // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings
Craig Topper16d7eb22016-02-16 07:45:04 +00001130 for (auto &WarningMsg : Warnings) {
1131 Warning(WarningMsg.first, WarningMsg.second);
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001132 }
1133
1134 // Remove old operands
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001135 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1136 OrigOperands.pop_back();
1137 }
1138 // OrigOperands.append(FinalOperands.begin(), FinalOperands.end());
1139 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1140 OrigOperands.push_back(std::move(FinalOperands[i]));
1141
1142 return false;
1143}
1144
David Blaikie960ea3f2014-06-08 16:18:35 +00001145std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001146 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001147 return ParseIntelOperand();
1148 return ParseATTOperand();
1149}
1150
Devang Patel41b9dde2012-01-17 18:00:18 +00001151/// getIntelMemOperandSize - Return intel memory operand size.
1152static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001153 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001154 .Cases("BYTE", "byte", 8)
1155 .Cases("WORD", "word", 16)
1156 .Cases("DWORD", "dword", 32)
Marina Yatsina497d44a2015-12-07 13:09:20 +00001157 .Cases("FWORD", "fword", 48)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001158 .Cases("QWORD", "qword", 64)
Michael Zuckerman9beca2e2015-08-24 10:26:54 +00001159 .Cases("MMWORD","mmword", 64)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001160 .Cases("XWORD", "xword", 80)
Michael Kuperstein69e40a42015-07-19 11:03:08 +00001161 .Cases("TBYTE", "tbyte", 80)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001162 .Cases("XMMWORD", "xmmword", 128)
1163 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +00001164 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +00001165 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +00001166 .Default(0);
1167 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001168}
1169
David Blaikie960ea3f2014-06-08 16:18:35 +00001170std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
1171 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
1172 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
1173 InlineAsmIdentifierInfo &Info) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001174 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
1175 // some other label reference.
1176 if (isa<MCSymbolRefExpr>(Disp) && Info.OpDecl && !Info.IsVarDecl) {
1177 // Insert an explicit size if the user didn't have one.
1178 if (!Size) {
1179 Size = getPointerWidth();
Craig Topper7d5b2312015-10-10 05:25:02 +00001180 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
1181 /*Len=*/0, Size);
Reid Kleckner5b37c182014-08-01 20:21:24 +00001182 }
1183
1184 // Create an absolute memory reference in order to match against
1185 // instructions taking a PC relative operand.
Craig Topper055845f2015-01-02 07:02:25 +00001186 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size,
1187 Identifier, Info.OpDecl);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001188 }
1189
1190 // We either have a direct symbol reference, or an offset from a symbol. The
1191 // parser always puts the symbol on the LHS, so look there for size
1192 // calculation purposes.
1193 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
1194 bool IsSymRef =
1195 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
1196 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001197 if (!Size) {
1198 Size = Info.Type * 8; // Size is in terms of bits in this context.
1199 if (Size)
Craig Topper7d5b2312015-10-10 05:25:02 +00001200 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
1201 /*Len=*/0, Size);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001202 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001203 }
1204
Chad Rosier7ca135b2013-03-19 21:11:56 +00001205 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001206 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001207 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001208 BaseReg = BaseReg ? BaseReg : 1;
Craig Topper055845f2015-01-02 07:02:25 +00001209 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1210 IndexReg, Scale, Start, End, Size, Identifier,
1211 Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001212}
1213
Chad Rosierd383db52013-04-12 20:20:54 +00001214static void
Craig Topper7143d802015-10-10 05:25:06 +00001215RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> &AsmRewrites,
Chad Rosierd383db52013-04-12 20:20:54 +00001216 StringRef SymName, int64_t ImmDisp,
1217 int64_t FinalImmDisp, SMLoc &BracLoc,
1218 SMLoc &StartInBrac, SMLoc &End) {
1219 // Remove the '[' and ']' from the IR string.
Craig Topper7143d802015-10-10 05:25:06 +00001220 AsmRewrites.emplace_back(AOK_Skip, BracLoc, 1);
1221 AsmRewrites.emplace_back(AOK_Skip, End, 1);
Chad Rosierd383db52013-04-12 20:20:54 +00001222
1223 // If ImmDisp is non-zero, then we parsed a displacement before the
1224 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1225 // If ImmDisp doesn't match the displacement computed by the state machine
1226 // then we have an additional displacement in the bracketed expression.
1227 if (ImmDisp != FinalImmDisp) {
1228 if (ImmDisp) {
1229 // We have an immediate displacement before the bracketed expression.
1230 // Adjust this to match the final immediate displacement.
1231 bool Found = false;
Craig Topper7143d802015-10-10 05:25:06 +00001232 for (AsmRewrite &AR : AsmRewrites) {
1233 if (AR.Loc.getPointer() > BracLoc.getPointer())
Chad Rosierd383db52013-04-12 20:20:54 +00001234 continue;
Craig Topper7143d802015-10-10 05:25:06 +00001235 if (AR.Kind == AOK_ImmPrefix || AR.Kind == AOK_Imm) {
Chad Rosierbfb70992013-04-17 00:11:46 +00001236 assert (!Found && "ImmDisp already rewritten.");
Craig Topper7143d802015-10-10 05:25:06 +00001237 AR.Kind = AOK_Imm;
1238 AR.Len = BracLoc.getPointer() - AR.Loc.getPointer();
1239 AR.Val = FinalImmDisp;
Chad Rosierd383db52013-04-12 20:20:54 +00001240 Found = true;
1241 break;
1242 }
1243 }
1244 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001245 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001246 } else {
1247 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001248 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001249 // before the bracketed expression.
Craig Topper7143d802015-10-10 05:25:06 +00001250 AsmRewrites.emplace_back(AOK_Imm, BracLoc, 0, FinalImmDisp);
Chad Rosierd383db52013-04-12 20:20:54 +00001251 }
1252 }
1253 // Remove all the ImmPrefix rewrites within the brackets.
Craig Topper7143d802015-10-10 05:25:06 +00001254 for (AsmRewrite &AR : AsmRewrites) {
1255 if (AR.Loc.getPointer() < StartInBrac.getPointer())
Chad Rosierd383db52013-04-12 20:20:54 +00001256 continue;
Craig Topper7143d802015-10-10 05:25:06 +00001257 if (AR.Kind == AOK_ImmPrefix)
1258 AR.Kind = AOK_Delete;
Chad Rosierd383db52013-04-12 20:20:54 +00001259 }
1260 const char *SymLocPtr = SymName.data();
Michael Liao5bf95782014-12-04 05:20:33 +00001261 // Skip everything before the symbol.
Chad Rosierd383db52013-04-12 20:20:54 +00001262 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1263 assert(Len > 0 && "Expected a non-negative length.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001264 AsmRewrites.emplace_back(AOK_Skip, StartInBrac, Len);
Chad Rosierd383db52013-04-12 20:20:54 +00001265 }
1266 // Skip everything after the symbol.
1267 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1268 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1269 assert(Len > 0 && "Expected a non-negative length.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001270 AsmRewrites.emplace_back(AOK_Skip, Loc, Len);
Chad Rosierd383db52013-04-12 20:20:54 +00001271 }
1272}
1273
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001274bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001275 MCAsmParser &Parser = getParser();
Chad Rosier6844ea02012-10-24 22:13:37 +00001276 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001277
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001278 AsmToken::TokenKind PrevTK = AsmToken::Error;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001279 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001280 while (!Done) {
1281 bool UpdateLocLex = true;
1282
1283 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1284 // identifier. Don't try an parse it as a register.
Nirav Dave8601ac12016-08-02 17:56:03 +00001285 if (PrevTK != AsmToken::Error && Tok.getString().startswith("."))
Chad Rosier5c118fd2013-01-14 22:31:35 +00001286 break;
Michael Liao5bf95782014-12-04 05:20:33 +00001287
Chad Rosierbfb70992013-04-17 00:11:46 +00001288 // If we're parsing an immediate expression, we don't expect a '['.
1289 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1290 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001291
David Majnemer6a5b8122014-06-19 01:25:43 +00001292 AsmToken::TokenKind TK = getLexer().getKind();
1293 switch (TK) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001294 default: {
1295 if (SM.isValidEndState()) {
1296 Done = true;
1297 break;
1298 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001299 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001300 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001301 case AsmToken::EndOfStatement: {
1302 Done = true;
1303 break;
1304 }
David Majnemer6a5b8122014-06-19 01:25:43 +00001305 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001306 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001307 // This could be a register or a symbolic displacement.
1308 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001309 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001310 SMLoc IdentLoc = Tok.getLoc();
1311 StringRef Identifier = Tok.getString();
David Majnemer6a5b8122014-06-19 01:25:43 +00001312 if (TK != AsmToken::String && !ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001313 SM.onRegister(TmpReg);
1314 UpdateLocLex = false;
1315 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001316 } else {
1317 if (!isParsingInlineAsm()) {
1318 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001319 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001320 } else {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001321 // This is a dot operator, not an adjacent identifier.
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001322 if (Identifier.find('.') != StringRef::npos &&
1323 PrevTK == AsmToken::RBrac) {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001324 return false;
1325 } else {
1326 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1327 if (ParseIntelIdentifier(Val, Identifier, Info,
1328 /*Unevaluated=*/false, End))
1329 return true;
1330 }
Chad Rosier95ce8892013-04-19 18:39:50 +00001331 }
1332 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001333 UpdateLocLex = false;
1334 break;
1335 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001336 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001337 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001338 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001339 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001340 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Craig Topper7d5b2312015-10-10 05:25:02 +00001341 InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, Tok.getLoc());
Kevin Enderby36eba252013-12-19 23:16:14 +00001342 // Look for 'b' or 'f' following an Integer as a directional label
1343 SMLoc Loc = getTok().getLoc();
1344 int64_t IntVal = getTok().getIntVal();
1345 End = consumeToken();
1346 UpdateLocLex = false;
1347 if (getLexer().getKind() == AsmToken::Identifier) {
1348 StringRef IDVal = getTok().getString();
1349 if (IDVal == "f" || IDVal == "b") {
1350 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +00001351 getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001352 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Michael Liao5bf95782014-12-04 05:20:33 +00001353 const MCExpr *Val =
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001354 MCSymbolRefExpr::create(Sym, Variant, getContext());
Kevin Enderby36eba252013-12-19 23:16:14 +00001355 if (IDVal == "b" && Sym->isUndefined())
1356 return Error(Loc, "invalid reference to undefined symbol");
1357 StringRef Identifier = Sym->getName();
1358 SM.onIdentifierExpr(Val, Identifier);
1359 End = consumeToken();
1360 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001361 if (SM.onInteger(IntVal, ErrMsg))
1362 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001363 }
1364 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001365 if (SM.onInteger(IntVal, ErrMsg))
1366 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001367 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001368 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001369 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001370 case AsmToken::Plus: SM.onPlus(); break;
1371 case AsmToken::Minus: SM.onMinus(); break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001372 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001373 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001374 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001375 case AsmToken::Pipe: SM.onOr(); break;
Michael Kupersteine3de07a2015-06-14 12:59:45 +00001376 case AsmToken::Caret: SM.onXor(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001377 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001378 case AsmToken::LessLess:
1379 SM.onLShift(); break;
1380 case AsmToken::GreaterGreater:
1381 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001382 case AsmToken::LBrac: SM.onLBrac(); break;
1383 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001384 case AsmToken::LParen: SM.onLParen(); break;
1385 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001386 }
Chad Rosier31246272013-04-17 21:01:45 +00001387 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001388 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001389
Alp Tokera5b88a52013-12-02 16:06:06 +00001390 if (!Done && UpdateLocLex)
1391 End = consumeToken();
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001392
1393 PrevTK = TK;
Devang Patel41b9dde2012-01-17 18:00:18 +00001394 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001395 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001396}
1397
David Blaikie960ea3f2014-06-08 16:18:35 +00001398std::unique_ptr<X86Operand>
1399X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Nirav Dave8601ac12016-08-02 17:56:03 +00001400 int64_t ImmDisp, bool isSymbol,
1401 unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001402 MCAsmParser &Parser = getParser();
Chad Rosier5362af92013-04-16 18:15:40 +00001403 const AsmToken &Tok = Parser.getTok();
1404 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1405 if (getLexer().isNot(AsmToken::LBrac))
1406 return ErrorOperand(BracLoc, "Expected '[' token!");
1407 Parser.Lex(); // Eat '['
1408
Nirav Davea6c75952016-07-14 17:37:05 +00001409 SMLoc StartInBrac = Parser.getTok().getLoc();
Chad Rosier5362af92013-04-16 18:15:40 +00001410 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1411 // may have already parsed an immediate displacement before the bracketed
1412 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001413 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001414 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001415 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +00001416
Craig Topper062a2ba2014-04-25 05:30:21 +00001417 const MCExpr *Disp = nullptr;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001418 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001419 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001420 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001421 if (isParsingInlineAsm())
Craig Topper7143d802015-10-10 05:25:06 +00001422 RewriteIntelBracExpression(*InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001423 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001424 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001425 }
1426
1427 if (SM.getImm() || !Disp) {
Jim Grosbach13760bd2015-05-30 01:25:56 +00001428 const MCExpr *Imm = MCConstantExpr::create(SM.getImm(), getContext());
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001429 if (Disp)
Jim Grosbach13760bd2015-05-30 01:25:56 +00001430 Disp = MCBinaryExpr::createAdd(Disp, Imm, getContext());
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001431 else
1432 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001433 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001434
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001435 // Parse struct field access. Intel requires a dot, but MSVC doesn't. MSVC
1436 // will in fact do global lookup the field name inside all global typedefs,
1437 // but we don't emulate that.
Nirav Davea6c75952016-07-14 17:37:05 +00001438 if ((Parser.getTok().getKind() == AsmToken::Identifier ||
1439 Parser.getTok().getKind() == AsmToken::Dot ||
1440 Parser.getTok().getKind() == AsmToken::Real) &&
1441 Parser.getTok().getString().find('.') != StringRef::npos) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001442 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001443 if (ParseIntelDotOperator(Disp, NewDisp))
Craig Topper062a2ba2014-04-25 05:30:21 +00001444 return nullptr;
Michael Liao5bf95782014-12-04 05:20:33 +00001445
Chad Rosier70f47592013-04-10 20:07:47 +00001446 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001447 Parser.Lex(); // Eat the field.
1448 Disp = NewDisp;
1449 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001450
Nirav Dave8601ac12016-08-02 17:56:03 +00001451 if (isSymbol) {
1452 if (SM.getSym()) {
1453 Error(Start, "cannot use more than one symbol in memory operand");
1454 return nullptr;
1455 }
1456 if (SM.getBaseReg()) {
1457 Error(Start, "cannot use base register with variable reference");
1458 return nullptr;
1459 }
1460 if (SM.getIndexReg()) {
1461 Error(Start, "cannot use index register with variable reference");
1462 return nullptr;
1463 }
1464 }
1465
Chad Rosier5c118fd2013-01-14 22:31:35 +00001466 int BaseReg = SM.getBaseReg();
1467 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001468 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001469 if (!isParsingInlineAsm()) {
1470 // handle [-42]
1471 if (!BaseReg && !IndexReg) {
1472 if (!SegReg)
Craig Topper055845f2015-01-02 07:02:25 +00001473 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size);
1474 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
1475 Start, End, Size);
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001476 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001477 StringRef ErrMsg;
1478 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1479 Error(StartInBrac, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001480 return nullptr;
Kevin Enderbybc570f22014-01-23 22:34:42 +00001481 }
Craig Topper055845f2015-01-02 07:02:25 +00001482 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1483 IndexReg, Scale, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001484 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001485
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001486 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001487 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001488 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001489}
1490
Chad Rosier8a244662013-04-02 20:02:33 +00001491// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001492bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1493 StringRef &Identifier,
1494 InlineAsmIdentifierInfo &Info,
1495 bool IsUnevaluatedOperand, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001496 MCAsmParser &Parser = getParser();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001497 assert(isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001498 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001499
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001500 StringRef LineBuf(Identifier.data());
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001501 void *Result =
1502 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001503
Chad Rosier8a244662013-04-02 20:02:33 +00001504 const AsmToken &Tok = Parser.getTok();
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001505 SMLoc Loc = Tok.getLoc();
John McCallf73981b2013-05-03 00:15:41 +00001506
1507 // Advance the token stream until the end of the current token is
1508 // after the end of what the frontend claimed.
1509 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001510 do {
John McCallf73981b2013-05-03 00:15:41 +00001511 End = Tok.getEndLoc();
1512 getLexer().Lex();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001513 } while (End.getPointer() < EndPtr);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001514 Identifier = LineBuf;
1515
Reid Klecknerc2b92542015-08-26 21:57:25 +00001516 // The frontend should end parsing on an assembler token boundary, unless it
1517 // failed parsing.
1518 assert((End.getPointer() == EndPtr || !Result) &&
1519 "frontend claimed part of a token?");
1520
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001521 // If the identifier lookup was unsuccessful, assume that we are dealing with
1522 // a label.
1523 if (!Result) {
Ehsan Akhgaribb6bb072014-09-22 20:40:36 +00001524 StringRef InternalName =
1525 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(),
1526 Loc, false);
1527 assert(InternalName.size() && "We should have an internal name here.");
1528 // Push a rewrite for replacing the identifier name with the internal name.
Craig Topper7d5b2312015-10-10 05:25:02 +00001529 InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(),
1530 InternalName);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001531 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001532
1533 // Create the symbol reference.
Jim Grosbach6f482002015-05-18 18:43:14 +00001534 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Chad Rosier8a244662013-04-02 20:02:33 +00001535 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001536 Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001537 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001538}
1539
David Majnemeraa34d792013-08-27 21:56:17 +00001540/// \brief Parse intel style segment override.
David Blaikie960ea3f2014-06-08 16:18:35 +00001541std::unique_ptr<X86Operand>
1542X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start,
1543 unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001544 MCAsmParser &Parser = getParser();
David Majnemeraa34d792013-08-27 21:56:17 +00001545 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1546 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1547 if (Tok.isNot(AsmToken::Colon))
1548 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1549 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001550
David Majnemeraa34d792013-08-27 21:56:17 +00001551 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001552 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001553 ImmDisp = Tok.getIntVal();
1554 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1555
Chad Rosier1530ba52013-03-27 21:49:56 +00001556 if (isParsingInlineAsm())
Craig Topper7d5b2312015-10-10 05:25:02 +00001557 InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, ImmDispToken.getLoc());
David Majnemeraa34d792013-08-27 21:56:17 +00001558
1559 if (getLexer().isNot(AsmToken::LBrac)) {
1560 // An immediate following a 'segment register', 'colon' token sequence can
1561 // be followed by a bracketed expression. If it isn't we know we have our
1562 // final segment override.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001563 const MCExpr *Disp = MCConstantExpr::create(ImmDisp, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001564 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp,
1565 /*BaseReg=*/0, /*IndexReg=*/0, /*Scale=*/1,
1566 Start, ImmDispToken.getEndLoc(), Size);
David Majnemeraa34d792013-08-27 21:56:17 +00001567 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001568 }
1569
Chad Rosier91c82662012-10-24 17:22:29 +00001570 if (getLexer().is(AsmToken::LBrac))
Nirav Dave8601ac12016-08-02 17:56:03 +00001571 return ParseIntelBracExpression(SegReg, Start, ImmDisp, false, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001572
David Majnemeraa34d792013-08-27 21:56:17 +00001573 const MCExpr *Val;
1574 SMLoc End;
1575 if (!isParsingInlineAsm()) {
1576 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001577 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001578
Craig Topper055845f2015-01-02 07:02:25 +00001579 return X86Operand::CreateMem(getPointerWidth(), Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001580 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001581
David Majnemeraa34d792013-08-27 21:56:17 +00001582 InlineAsmIdentifierInfo Info;
1583 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001584 if (ParseIntelIdentifier(Val, Identifier, Info,
1585 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001586 return nullptr;
David Majnemeraa34d792013-08-27 21:56:17 +00001587 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1588 /*Scale=*/1, Start, End, Size, Identifier, Info);
1589}
1590
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001591//ParseRoundingModeOp - Parse AVX-512 rounding mode operand
1592std::unique_ptr<X86Operand>
1593X86AsmParser::ParseRoundingModeOp(SMLoc Start, SMLoc End) {
1594 MCAsmParser &Parser = getParser();
1595 const AsmToken &Tok = Parser.getTok();
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001596 // Eat "{" and mark the current place.
1597 const SMLoc consumedToken = consumeToken();
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001598 if (Tok.getIdentifier().startswith("r")){
1599 int rndMode = StringSwitch<int>(Tok.getIdentifier())
1600 .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT)
1601 .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF)
1602 .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF)
1603 .Case("rz", X86::STATIC_ROUNDING::TO_ZERO)
1604 .Default(-1);
1605 if (-1 == rndMode)
1606 return ErrorOperand(Tok.getLoc(), "Invalid rounding mode.");
1607 Parser.Lex(); // Eat "r*" of r*-sae
1608 if (!getLexer().is(AsmToken::Minus))
1609 return ErrorOperand(Tok.getLoc(), "Expected - at this point");
1610 Parser.Lex(); // Eat "-"
1611 Parser.Lex(); // Eat the sae
1612 if (!getLexer().is(AsmToken::RCurly))
1613 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1614 Parser.Lex(); // Eat "}"
1615 const MCExpr *RndModeOp =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001616 MCConstantExpr::create(rndMode, Parser.getContext());
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001617 return X86Operand::CreateImm(RndModeOp, Start, End);
1618 }
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001619 if(Tok.getIdentifier().equals("sae")){
1620 Parser.Lex(); // Eat the sae
1621 if (!getLexer().is(AsmToken::RCurly))
1622 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1623 Parser.Lex(); // Eat "}"
1624 return X86Operand::CreateToken("{sae}", consumedToken);
1625 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001626 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
1627}
Chad Rosier91c82662012-10-24 17:22:29 +00001628
Chad Rosier5dcb4662012-10-24 22:21:50 +00001629/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001630bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001631 const MCExpr *&NewDisp) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001632 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001633 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001634 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001635
1636 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001637 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001638 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001639 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001640 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001641
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001642 // Drop the optional '.'.
1643 StringRef DotDispStr = Tok.getString();
1644 if (DotDispStr.startswith("."))
1645 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001646
Chad Rosier5dcb4662012-10-24 22:21:50 +00001647 // .Imm gets lexed as a real.
1648 if (Tok.is(AsmToken::Real)) {
1649 APInt DotDisp;
1650 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001651 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001652 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001653 unsigned DotDisp;
1654 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1655 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001656 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001657 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001658 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001659 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001660 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001661
Chad Rosier240b7b92012-10-25 21:51:10 +00001662 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1663 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1664 unsigned Len = DotDispStr.size();
1665 unsigned Val = OrigDispVal + DotDispVal;
Craig Topper7d5b2312015-10-10 05:25:02 +00001666 InstInfo->AsmRewrites->emplace_back(AOK_DotOperator, Loc, Len, Val);
Chad Rosier911c1f32012-10-25 17:37:43 +00001667 }
1668
Jim Grosbach13760bd2015-05-30 01:25:56 +00001669 NewDisp = MCConstantExpr::create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001670 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001671}
1672
Chad Rosier91c82662012-10-24 17:22:29 +00001673/// Parse the 'offset' operator. This operator is used to specify the
1674/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001675std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001676 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001677 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001678 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001679 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001680
Chad Rosier91c82662012-10-24 17:22:29 +00001681 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001682 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001683 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001684 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001685 if (ParseIntelIdentifier(Val, Identifier, Info,
1686 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001687 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001688
Chad Rosiere2f03772012-10-26 16:09:20 +00001689 // Don't emit the offset operator.
Craig Topper7d5b2312015-10-10 05:25:02 +00001690 InstInfo->AsmRewrites->emplace_back(AOK_Skip, OffsetOfLoc, 7);
Chad Rosiere2f03772012-10-26 16:09:20 +00001691
Chad Rosier91c82662012-10-24 17:22:29 +00001692 // The offset operator will have an 'r' constraint, thus we need to create
1693 // register operand to ensure proper matching. Just pick a GPR based on
1694 // the size of a pointer.
Nirav Dave6477ce22016-09-26 19:33:36 +00001695 bool Parse32 = is32BitMode() || Code16GCC;
1696 unsigned RegNo = is64BitMode() ? X86::RBX : (Parse32 ? X86::EBX : X86::BX);
1697
Chad Rosiera4bc9432013-01-10 22:10:27 +00001698 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Peter Collingbournecc723cc2016-10-09 04:39:13 +00001699 Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001700}
1701
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001702enum IntelOperatorKind {
1703 IOK_LENGTH,
1704 IOK_SIZE,
1705 IOK_TYPE
1706};
1707
1708/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1709/// returns the number of elements in an array. It returns the value 1 for
1710/// non-array variables. The SIZE operator returns the size of a C or C++
1711/// variable. A variable's size is the product of its LENGTH and TYPE. The
1712/// TYPE operator returns the size of a C or C++ type or variable. If the
1713/// variable is an array, TYPE returns the size of a single element.
David Blaikie960ea3f2014-06-08 16:18:35 +00001714std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001715 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001716 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001717 SMLoc TypeLoc = Tok.getLoc();
1718 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001719
Craig Topper062a2ba2014-04-25 05:30:21 +00001720 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001721 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001722 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001723 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001724 if (ParseIntelIdentifier(Val, Identifier, Info,
1725 /*Unevaluated=*/true, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001726 return nullptr;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001727
1728 if (!Info.OpDecl)
1729 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001730
Chad Rosierf6675c32013-04-22 17:01:46 +00001731 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001732 switch(OpKind) {
1733 default: llvm_unreachable("Unexpected operand kind!");
1734 case IOK_LENGTH: CVal = Info.Length; break;
1735 case IOK_SIZE: CVal = Info.Size; break;
1736 case IOK_TYPE: CVal = Info.Type; break;
1737 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001738
1739 // Rewrite the type operator and the C or C++ type or variable in terms of an
1740 // immediate. E.g. TYPE foo -> $$4
1741 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Craig Topper7d5b2312015-10-10 05:25:02 +00001742 InstInfo->AsmRewrites->emplace_back(AOK_Imm, TypeLoc, Len, CVal);
Chad Rosier11c42f22012-10-26 18:04:20 +00001743
Jim Grosbach13760bd2015-05-30 01:25:56 +00001744 const MCExpr *Imm = MCConstantExpr::create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001745 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001746}
1747
David Blaikie960ea3f2014-06-08 16:18:35 +00001748std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001749 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001750 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001751 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001752
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001753 // Offset, length, type and size operators.
1754 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001755 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001756 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001757 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001758 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001759 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001760 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001761 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001762 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001763 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001764 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001765
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001766 bool PtrInOperand = false;
David Majnemeraa34d792013-08-27 21:56:17 +00001767 unsigned Size = getIntelMemOperandSize(Tok.getString());
1768 if (Size) {
1769 Parser.Lex(); // Eat operand size (e.g., byte, word).
1770 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
Reid Kleckner71ff3f22014-08-01 00:59:22 +00001771 return ErrorOperand(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
David Majnemeraa34d792013-08-27 21:56:17 +00001772 Parser.Lex(); // Eat ptr.
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001773 PtrInOperand = true;
David Majnemeraa34d792013-08-27 21:56:17 +00001774 }
Nirav Dave8601ac12016-08-02 17:56:03 +00001775
David Majnemeraa34d792013-08-27 21:56:17 +00001776 Start = Tok.getLoc();
1777
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001778 // rounding mode token
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001779 if (getSTI().getFeatureBits()[X86::FeatureAVX512] &&
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001780 getLexer().is(AsmToken::LCurly))
1781 return ParseRoundingModeOp(Start, End);
1782
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001783 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001784 unsigned RegNo = 0;
Nirav Dave8601ac12016-08-02 17:56:03 +00001785 if (getLexer().is(AsmToken::Identifier) &&
1786 !ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001787 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001788 // of a segment override, otherwise this is a normal register reference.
Douglas Katzman0411e862016-10-05 15:23:35 +00001789 // In case it is a normal register and there is ptr in the operand this
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001790 // is an error
Douglas Katzman0411e862016-10-05 15:23:35 +00001791 if (RegNo == X86::RIP)
1792 return ErrorOperand(Start, "rip can only be used as a base register");
1793 if (getLexer().isNot(AsmToken::Colon)) {
1794 if (PtrInOperand) {
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001795 return ErrorOperand(Start, "expected memory operand after "
1796 "'ptr', found register operand instead");
1797 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001798 return X86Operand::CreateReg(RegNo, Start, End);
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001799 }
David Majnemeraa34d792013-08-27 21:56:17 +00001800 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001801 }
1802
Nirav Dave8601ac12016-08-02 17:56:03 +00001803 // Immediates and Memory
1804
1805 // Parse [ BaseReg + Scale*IndexReg + Disp ].
1806 if (getLexer().is(AsmToken::LBrac))
1807 return ParseIntelBracExpression(/*SegReg=*/0, Start, /*ImmDisp=*/0, false,
1808 Size);
1809
1810 AsmToken StartTok = Tok;
1811 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1812 /*AddImmPrefix=*/false);
1813 if (ParseIntelExpression(SM, End))
1814 return nullptr;
1815
1816 bool isSymbol = SM.getSym() && SM.getSym()->getKind() != MCExpr::Constant;
1817 int64_t Imm = SM.getImm();
1818 if (SM.getSym() && SM.getSym()->getKind() == MCExpr::Constant)
1819 SM.getSym()->evaluateAsAbsolute(Imm);
1820
1821 if (StartTok.isNot(AsmToken::Identifier) &&
1822 StartTok.isNot(AsmToken::String) && isParsingInlineAsm()) {
1823 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1824 if (StartTok.getString().size() == Len)
1825 // Just add a prefix if this wasn't a complex immediate expression.
1826 InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, Start);
1827 else
1828 // Otherwise, rewrite the complex expression as a single immediate.
1829 InstInfo->AsmRewrites->emplace_back(AOK_Imm, Start, Len, Imm);
1830 }
1831
1832 if (getLexer().isNot(AsmToken::LBrac)) {
1833 // If a directional label (ie. 1f or 2b) was parsed above from
1834 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1835 // to the MCExpr with the directional local symbol and this is a
1836 // memory operand not an immediate operand.
1837 if (isSymbol) {
1838 if (isParsingInlineAsm())
1839 return CreateMemForInlineAsm(/*SegReg=*/0, SM.getSym(), /*BaseReg=*/0,
1840 /*IndexReg=*/0,
1841 /*Scale=*/1, Start, End, Size,
1842 SM.getSymName(), SM.getIdentifierInfo());
1843 return X86Operand::CreateMem(getPointerWidth(), SM.getSym(), Start, End,
1844 Size);
1845 }
1846
1847 const MCExpr *ImmExpr = MCConstantExpr::create(Imm, getContext());
1848 return X86Operand::CreateImm(ImmExpr, Start, End);
1849 }
1850
1851 // Only positive immediates are valid.
1852 if (Imm < 0)
1853 return ErrorOperand(Start, "expected a positive immediate displacement "
1854 "before bracketed expr.");
1855
1856 return ParseIntelBracExpression(/*SegReg=*/0, Start, Imm, isSymbol, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001857}
1858
David Blaikie960ea3f2014-06-08 16:18:35 +00001859std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001860 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001861 switch (getLexer().getKind()) {
1862 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001863 // Parse a memory operand with no segment register.
1864 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001865 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001866 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001867 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001868 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001869 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001870 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001871 Error(Start, "%eiz and %riz can only be used as index registers",
1872 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001873 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001874 }
Douglas Katzman0411e862016-10-05 15:23:35 +00001875 if (RegNo == X86::RIP) {
1876 Error(Start, "%rip can only be used as a base register",
1877 SMRange(Start, End));
1878 return nullptr;
1879 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001880
Chris Lattnerb9270732010-04-17 18:56:34 +00001881 // If this is a segment register followed by a ':', then this is the start
1882 // of a memory reference, otherwise this is a normal register reference.
1883 if (getLexer().isNot(AsmToken::Colon))
1884 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001885
Reid Kleckner0c5da972014-07-31 23:03:22 +00001886 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1887 return ErrorOperand(Start, "invalid segment register");
1888
Chris Lattnerb9270732010-04-17 18:56:34 +00001889 getParser().Lex(); // Eat the colon.
1890 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001891 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001892 case AsmToken::Dollar: {
1893 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001894 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001895 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001896 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001897 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001898 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001899 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001900 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001901 case AsmToken::LCurly:{
1902 SMLoc Start = Parser.getTok().getLoc(), End;
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001903 if (getSTI().getFeatureBits()[X86::FeatureAVX512])
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001904 return ParseRoundingModeOp(Start, End);
Nirav Dave8601ac12016-08-02 17:56:03 +00001905 return ErrorOperand(Start, "Unexpected '{' in expression");
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001906 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001907 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001908}
1909
David Blaikie960ea3f2014-06-08 16:18:35 +00001910bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1911 const MCParsedAsmOperand &Op) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001912 MCAsmParser &Parser = getParser();
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001913 if(getSTI().getFeatureBits()[X86::FeatureAVX512]) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001914 if (getLexer().is(AsmToken::LCurly)) {
1915 // Eat "{" and mark the current place.
1916 const SMLoc consumedToken = consumeToken();
1917 // Distinguish {1to<NUM>} from {%k<NUM>}.
1918 if(getLexer().is(AsmToken::Integer)) {
1919 // Parse memory broadcasting ({1to<NUM>}).
1920 if (getLexer().getTok().getIntVal() != 1)
Nirav Dave2364748a2016-09-16 18:30:20 +00001921 return !TokError("Expected 1to<NUM> at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001922 Parser.Lex(); // Eat "1" of 1to8
1923 if (!getLexer().is(AsmToken::Identifier) ||
1924 !getLexer().getTok().getIdentifier().startswith("to"))
Nirav Dave2364748a2016-09-16 18:30:20 +00001925 return !TokError("Expected 1to<NUM> at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001926 // Recognize only reasonable suffixes.
1927 const char *BroadcastPrimitive =
1928 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
Robert Khasanovbfa01312014-07-21 14:54:21 +00001929 .Case("to2", "{1to2}")
1930 .Case("to4", "{1to4}")
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001931 .Case("to8", "{1to8}")
1932 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001933 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001934 if (!BroadcastPrimitive)
Nirav Dave2364748a2016-09-16 18:30:20 +00001935 return !TokError("Invalid memory broadcast primitive.");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001936 Parser.Lex(); // Eat "toN" of 1toN
1937 if (!getLexer().is(AsmToken::RCurly))
Nirav Dave2364748a2016-09-16 18:30:20 +00001938 return !TokError("Expected } at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001939 Parser.Lex(); // Eat "}"
1940 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1941 consumedToken));
1942 // No AVX512 specific primitives can pass
1943 // after memory broadcasting, so return.
1944 return true;
1945 } else {
1946 // Parse mask register {%k1}
1947 Operands.push_back(X86Operand::CreateToken("{", consumedToken));
David Blaikie960ea3f2014-06-08 16:18:35 +00001948 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1949 Operands.push_back(std::move(Op));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001950 if (!getLexer().is(AsmToken::RCurly))
Nirav Dave2364748a2016-09-16 18:30:20 +00001951 return !TokError("Expected } at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001952 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1953
1954 // Parse "zeroing non-masked" semantic {z}
1955 if (getLexer().is(AsmToken::LCurly)) {
1956 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1957 if (!getLexer().is(AsmToken::Identifier) ||
1958 getLexer().getTok().getIdentifier() != "z")
Nirav Dave2364748a2016-09-16 18:30:20 +00001959 return !TokError("Expected z at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001960 Parser.Lex(); // Eat the z
1961 if (!getLexer().is(AsmToken::RCurly))
Nirav Dave2364748a2016-09-16 18:30:20 +00001962 return !TokError("Expected } at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001963 Parser.Lex(); // Eat the }
1964 }
1965 }
1966 }
1967 }
1968 }
1969 return true;
1970}
1971
Chris Lattnerb9270732010-04-17 18:56:34 +00001972/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1973/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00001974std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
1975 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001976
Rafael Espindola961d4692014-11-11 05:18:41 +00001977 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001978 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1979 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001980 // only way to do this without lookahead is to eat the '(' and see what is
1981 // after it.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001982 const MCExpr *Disp = MCConstantExpr::create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001983 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001984 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00001985 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001986
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001987 // After parsing the base expression we could either have a parenthesized
1988 // memory address or not. If not, return now. If so, eat the (.
1989 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001990 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001991 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00001992 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, ExprEnd);
1993 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
1994 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001995 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001996
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001997 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001998 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001999 } else {
2000 // Okay, we have a '('. We don't know if this is an expression or not, but
2001 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00002002 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002003 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002004
Kevin Enderby7d912182009-09-03 17:15:07 +00002005 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002006 // Nothing to do here, fall into the code below with the '(' part of the
2007 // memory operand consumed.
2008 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00002009 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002010
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002011 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002012 if (getParser().parseParenExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00002013 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002014
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002015 // After parsing the base expression we could either have a parenthesized
2016 // memory address or not. If not, return now. If so, eat the (.
2017 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002018 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002019 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002020 return X86Operand::CreateMem(getPointerWidth(), Disp, LParenLoc,
2021 ExprEnd);
2022 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2023 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002024 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002025
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002026 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002027 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002028 }
2029 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002030
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002031 // If we reached here, then we just ate the ( of the memory operand. Process
2032 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00002033 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00002034 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002035
Chris Lattner0c2538f2010-01-15 18:51:29 +00002036 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002037 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00002038 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00002039 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002040 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002041 Error(StartLoc, "eiz and riz can only be used as index registers",
2042 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00002043 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002044 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00002045 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002046
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002047 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002048 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002049 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002050
2051 // Following the comma we should have either an index register, or a scale
2052 // value. We don't support the later form, but we want to parse it
2053 // correctly.
2054 //
2055 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002056 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00002057 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00002058 SMLoc L;
Douglas Katzman0411e862016-10-05 15:23:35 +00002059 if (ParseRegister(IndexReg, L, L))
2060 return nullptr;
2061 if (BaseReg == X86::RIP) {
2062 Error(IndexLoc, "%rip as base register can not have an index register");
2063 return nullptr;
2064 }
2065 if (IndexReg == X86::RIP) {
2066 Error(IndexLoc, "%rip is not allowed as an index register");
2067 return nullptr;
2068 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002069
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002070 if (getLexer().isNot(AsmToken::RParen)) {
2071 // Parse the scale amount:
2072 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002073 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002074 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002075 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002076 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002077 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00002078 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002079
2080 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002081 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002082
2083 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002084 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00002085 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002086 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00002087 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002088
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002089 // Validate the scale amount.
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002090 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
David Woodhouse6dbda442014-01-08 12:58:28 +00002091 ScaleVal != 1) {
2092 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00002093 return nullptr;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002094 }
2095 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 &&
2096 ScaleVal != 8) {
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002097 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00002098 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002099 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002100 Scale = (unsigned)ScaleVal;
2101 }
2102 }
2103 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002104 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002105 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00002106 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002107
2108 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002109 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00002110 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002111
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002112 if (Value != 1)
2113 Warning(Loc, "scale factor without index register is ignored");
2114 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002115 }
2116 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002117
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002118 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002119 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002120 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00002121 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002122 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002123 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002124 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002125
David Woodhouse6dbda442014-01-08 12:58:28 +00002126 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
2127 // and then only in non-64-bit modes. Except for DX, which is a special case
2128 // because an unofficial form of in/out instructions uses it.
2129 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
2130 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
2131 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2132 BaseReg != X86::DX) {
2133 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002134 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002135 }
2136 if (BaseReg == 0 &&
2137 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2138 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002139 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002140 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00002141
2142 StringRef ErrMsg;
2143 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
2144 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00002145 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002146 }
2147
Reid Klecknerb7e2f602014-07-31 23:26:35 +00002148 if (SegReg || BaseReg || IndexReg)
Craig Topper055845f2015-01-02 07:02:25 +00002149 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
2150 IndexReg, Scale, MemStart, MemEnd);
2151 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002152}
2153
David Blaikie960ea3f2014-06-08 16:18:35 +00002154bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
2155 SMLoc NameLoc, OperandVector &Operands) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002156 MCAsmParser &Parser = getParser();
Chad Rosierf0e87202012-10-25 20:41:34 +00002157 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002158 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002159
Chris Lattner7e8a99b2010-11-28 20:23:50 +00002160 // FIXME: Hack to recognize setneb as setne.
2161 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
2162 PatchedName != "setb" && PatchedName != "setnb")
2163 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00002164
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002165 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002166 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002167 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
2168 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00002169 bool IsVCMP = PatchedName[0] == 'v';
Craig Topper78c424d2015-02-15 07:13:48 +00002170 unsigned CCIdx = IsVCMP ? 4 : 3;
2171 unsigned ComparisonCode = StringSwitch<unsigned>(
2172 PatchedName.slice(CCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00002173 .Case("eq", 0x00)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002174 .Case("eq_oq", 0x00)
Craig Toppera0a603e2012-03-29 07:11:23 +00002175 .Case("lt", 0x01)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002176 .Case("lt_os", 0x01)
Craig Toppera0a603e2012-03-29 07:11:23 +00002177 .Case("le", 0x02)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002178 .Case("le_os", 0x02)
Craig Toppera0a603e2012-03-29 07:11:23 +00002179 .Case("unord", 0x03)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002180 .Case("unord_q", 0x03)
Craig Toppera0a603e2012-03-29 07:11:23 +00002181 .Case("neq", 0x04)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002182 .Case("neq_uq", 0x04)
Craig Toppera0a603e2012-03-29 07:11:23 +00002183 .Case("nlt", 0x05)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002184 .Case("nlt_us", 0x05)
Craig Toppera0a603e2012-03-29 07:11:23 +00002185 .Case("nle", 0x06)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002186 .Case("nle_us", 0x06)
Craig Toppera0a603e2012-03-29 07:11:23 +00002187 .Case("ord", 0x07)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002188 .Case("ord_q", 0x07)
Craig Toppera0a603e2012-03-29 07:11:23 +00002189 /* AVX only from here */
2190 .Case("eq_uq", 0x08)
2191 .Case("nge", 0x09)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002192 .Case("nge_us", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002193 .Case("ngt", 0x0A)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002194 .Case("ngt_us", 0x0A)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002195 .Case("false", 0x0B)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002196 .Case("false_oq", 0x0B)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002197 .Case("neq_oq", 0x0C)
2198 .Case("ge", 0x0D)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002199 .Case("ge_os", 0x0D)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002200 .Case("gt", 0x0E)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002201 .Case("gt_os", 0x0E)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002202 .Case("true", 0x0F)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002203 .Case("true_uq", 0x0F)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002204 .Case("eq_os", 0x10)
2205 .Case("lt_oq", 0x11)
2206 .Case("le_oq", 0x12)
2207 .Case("unord_s", 0x13)
2208 .Case("neq_us", 0x14)
2209 .Case("nlt_uq", 0x15)
2210 .Case("nle_uq", 0x16)
2211 .Case("ord_s", 0x17)
2212 .Case("eq_us", 0x18)
2213 .Case("nge_uq", 0x19)
2214 .Case("ngt_uq", 0x1A)
2215 .Case("false_os", 0x1B)
2216 .Case("neq_os", 0x1C)
2217 .Case("ge_oq", 0x1D)
2218 .Case("gt_oq", 0x1E)
2219 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002220 .Default(~0U);
Craig Topper78c424d2015-02-15 07:13:48 +00002221 if (ComparisonCode != ~0U && (IsVCMP || ComparisonCode < 8)) {
Craig Topper43860832015-02-14 21:54:03 +00002222
Craig Topper78c424d2015-02-15 07:13:48 +00002223 Operands.push_back(X86Operand::CreateToken(PatchedName.slice(0, CCIdx),
Craig Topper43860832015-02-14 21:54:03 +00002224 NameLoc));
2225
Jim Grosbach13760bd2015-05-30 01:25:56 +00002226 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper43860832015-02-14 21:54:03 +00002227 getParser().getContext());
2228 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2229
2230 PatchedName = PatchedName.substr(PatchedName.size() - 2);
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002231 }
2232 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002233
Craig Topper78c424d2015-02-15 07:13:48 +00002234 // FIXME: Hack to recognize vpcmp<comparison code>{ub,uw,ud,uq,b,w,d,q}.
2235 if (PatchedName.startswith("vpcmp") &&
2236 (PatchedName.endswith("b") || PatchedName.endswith("w") ||
2237 PatchedName.endswith("d") || PatchedName.endswith("q"))) {
2238 unsigned CCIdx = PatchedName.drop_back().back() == 'u' ? 2 : 1;
2239 unsigned ComparisonCode = StringSwitch<unsigned>(
2240 PatchedName.slice(5, PatchedName.size() - CCIdx))
2241 .Case("eq", 0x0) // Only allowed on unsigned. Checked below.
2242 .Case("lt", 0x1)
2243 .Case("le", 0x2)
2244 //.Case("false", 0x3) // Not a documented alias.
2245 .Case("neq", 0x4)
2246 .Case("nlt", 0x5)
2247 .Case("nle", 0x6)
2248 //.Case("true", 0x7) // Not a documented alias.
2249 .Default(~0U);
2250 if (ComparisonCode != ~0U && (ComparisonCode != 0 || CCIdx == 2)) {
2251 Operands.push_back(X86Operand::CreateToken("vpcmp", NameLoc));
2252
Jim Grosbach13760bd2015-05-30 01:25:56 +00002253 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper78c424d2015-02-15 07:13:48 +00002254 getParser().getContext());
2255 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2256
2257 PatchedName = PatchedName.substr(PatchedName.size() - CCIdx);
2258 }
2259 }
2260
Craig Topper916708f2015-02-13 07:42:25 +00002261 // FIXME: Hack to recognize vpcom<comparison code>{ub,uw,ud,uq,b,w,d,q}.
2262 if (PatchedName.startswith("vpcom") &&
2263 (PatchedName.endswith("b") || PatchedName.endswith("w") ||
2264 PatchedName.endswith("d") || PatchedName.endswith("q"))) {
Craig Topper78c424d2015-02-15 07:13:48 +00002265 unsigned CCIdx = PatchedName.drop_back().back() == 'u' ? 2 : 1;
2266 unsigned ComparisonCode = StringSwitch<unsigned>(
2267 PatchedName.slice(5, PatchedName.size() - CCIdx))
Craig Topper916708f2015-02-13 07:42:25 +00002268 .Case("lt", 0x0)
2269 .Case("le", 0x1)
2270 .Case("gt", 0x2)
2271 .Case("ge", 0x3)
2272 .Case("eq", 0x4)
2273 .Case("neq", 0x5)
2274 .Case("false", 0x6)
2275 .Case("true", 0x7)
2276 .Default(~0U);
Craig Topper78c424d2015-02-15 07:13:48 +00002277 if (ComparisonCode != ~0U) {
Craig Topper916708f2015-02-13 07:42:25 +00002278 Operands.push_back(X86Operand::CreateToken("vpcom", NameLoc));
2279
Jim Grosbach13760bd2015-05-30 01:25:56 +00002280 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper916708f2015-02-13 07:42:25 +00002281 getParser().getContext());
2282 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2283
Craig Topper78c424d2015-02-15 07:13:48 +00002284 PatchedName = PatchedName.substr(PatchedName.size() - CCIdx);
Craig Topper916708f2015-02-13 07:42:25 +00002285 }
2286 }
2287
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002288 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002289
Chris Lattner086a83a2010-09-08 05:17:37 +00002290 // Determine whether this is an instruction prefix.
2291 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00002292 Name == "lock" || Name == "rep" ||
2293 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00002294 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00002295 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00002296
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002297 bool CurlyAsEndOfStatement = false;
Chris Lattner086a83a2010-09-08 05:17:37 +00002298 // This does the actual operand parsing. Don't parse any more if we have a
2299 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2300 // just want to parse the "lock" as the first instruction and the "incl" as
2301 // the next one.
2302 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002303
2304 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002305 if (getLexer().is(AsmToken::Star))
2306 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002307
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002308 // Read the operands.
2309 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002310 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
2311 Operands.push_back(std::move(Op));
2312 if (!HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00002313 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002314 } else {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002315 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00002316 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002317 // check for comma and eat it
2318 if (getLexer().is(AsmToken::Comma))
2319 Parser.Lex();
2320 else
2321 break;
2322 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00002323
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002324 // In MS inline asm curly braces mark the begining/end of a block, therefore
2325 // they should be interepreted as end of statement
2326 CurlyAsEndOfStatement =
2327 isParsingIntelSyntax() && isParsingInlineAsm() &&
2328 (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly));
2329 if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement)
Nirav Dave2364748a2016-09-16 18:30:20 +00002330 return TokError("unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002331 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002332
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002333 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002334 if (getLexer().is(AsmToken::EndOfStatement) ||
2335 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002336 Parser.Lex();
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002337 else if (CurlyAsEndOfStatement)
2338 // Add an actual EndOfStatement before the curly brace
2339 Info.AsmRewrites->emplace_back(AOK_EndOfStatement,
2340 getLexer().getTok().getLoc(), 0);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002341
Michael Zuckermanfd3fe9e2015-11-12 16:58:51 +00002342 // This is for gas compatibility and cannot be done in td.
2343 // Adding "p" for some floating point with no argument.
2344 // For example: fsub --> fsubp
2345 bool IsFp =
2346 Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr";
2347 if (IsFp && Operands.size() == 1) {
2348 const char *Repl = StringSwitch<const char *>(Name)
2349 .Case("fsub", "fsubp")
2350 .Case("fdiv", "fdivp")
2351 .Case("fsubr", "fsubrp")
2352 .Case("fdivr", "fdivrp");
2353 static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl);
2354 }
2355
Nirav Davef45fd2b2016-08-08 18:01:04 +00002356 // Moving a 32 or 16 bit value into a segment register has the same
2357 // behavior. Modify such instructions to always take shorter form.
2358 if ((Name == "mov" || Name == "movw" || Name == "movl") &&
2359 (Operands.size() == 3)) {
2360 X86Operand &Op1 = (X86Operand &)*Operands[1];
2361 X86Operand &Op2 = (X86Operand &)*Operands[2];
2362 SMLoc Loc = Op1.getEndLoc();
2363 if (Op1.isReg() && Op2.isReg() &&
2364 X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(
2365 Op2.getReg()) &&
2366 (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) ||
2367 X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) {
2368 // Change instruction name to match new instruction.
2369 if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) {
2370 Name = is16BitMode() ? "movw" : "movl";
2371 Operands[0] = X86Operand::CreateToken(Name, NameLoc);
2372 }
2373 // Select the correct equivalent 16-/32-bit source register.
2374 unsigned Reg =
2375 getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32);
2376 Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc);
2377 }
2378 }
2379
Nirav Dave8e103802016-06-29 19:54:27 +00002380 // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" ->
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002381 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2382 // documented form in various unofficial manuals, so a lot of code uses it.
Nirav Dave8e103802016-06-29 19:54:27 +00002383 if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" ||
2384 Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") &&
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002385 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002386 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002387 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2388 isa<MCConstantExpr>(Op.Mem.Disp) &&
2389 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2390 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2391 SMLoc Loc = Op.getEndLoc();
2392 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002393 }
2394 }
Nirav Dave8e103802016-06-29 19:54:27 +00002395 // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al".
2396 if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" ||
2397 Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") &&
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002398 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002399 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002400 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2401 isa<MCConstantExpr>(Op.Mem.Disp) &&
2402 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2403 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2404 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002405 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002406 }
2407 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002408
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002409 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands;
2410 bool HadVerifyError = false;
2411
David Woodhouse4ce66062014-01-22 15:08:55 +00002412 // Append default arguments to "ins[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002413 if (Name.startswith("ins") &&
2414 (Operands.size() == 1 || Operands.size() == 3) &&
2415 (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" ||
2416 Name == "ins")) {
2417
2418 AddDefaultSrcDestOperands(TmpOperands,
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002419 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc),
2420 DefaultMemDIOperand(NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002421 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002422 }
2423
David Woodhousec472b812014-01-22 15:08:49 +00002424 // Append default arguments to "outs[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002425 if (Name.startswith("outs") &&
2426 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhousec472b812014-01-22 15:08:49 +00002427 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002428 Name == "outsd" || Name == "outs")) {
2429 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002430 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002431 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002432 }
2433
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002434 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2435 // values of $SIREG according to the mode. It would be nice if this
2436 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002437 if (Name.startswith("lods") &&
2438 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002439 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002440 Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) {
2441 TmpOperands.push_back(DefaultMemSIOperand(NameLoc));
2442 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2443 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002444
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002445 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2446 // values of $DIREG according to the mode. It would be nice if this
2447 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002448 if (Name.startswith("stos") &&
2449 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002450 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002451 Name == "stosl" || Name == "stosd" || Name == "stosq")) {
2452 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2453 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2454 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002455
David Woodhouse20fe4802014-01-22 15:08:27 +00002456 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2457 // values of $DIREG according to the mode. It would be nice if this
2458 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002459 if (Name.startswith("scas") &&
2460 (Operands.size() == 1 || Operands.size() == 2) &&
David Woodhouse20fe4802014-01-22 15:08:27 +00002461 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002462 Name == "scasl" || Name == "scasd" || Name == "scasq")) {
2463 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2464 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2465 }
David Woodhouse20fe4802014-01-22 15:08:27 +00002466
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002467 // Add default SI and DI operands to "cmps[bwlq]".
2468 if (Name.startswith("cmps") &&
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002469 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002470 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2471 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002472 AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc),
2473 DefaultMemSIOperand(NameLoc));
2474 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002475 }
2476
David Woodhouse6f417de2014-01-22 15:08:42 +00002477 // Add default SI and DI operands to "movs[bwlq]".
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002478 if (((Name.startswith("movs") &&
2479 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2480 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2481 (Name.startswith("smov") &&
2482 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2483 Name == "smovl" || Name == "smovd" || Name == "smovq"))) &&
2484 (Operands.size() == 1 || Operands.size() == 3)) {
2485 if (Name == "movsd" && Operands.size() == 1)
2486 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2487 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
2488 DefaultMemDIOperand(NameLoc));
2489 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2490 }
2491
2492 // Check if we encountered an error for one the string insturctions
2493 if (HadVerifyError) {
2494 return HadVerifyError;
David Woodhouse6f417de2014-01-22 15:08:42 +00002495 }
2496
Chris Lattner4bd21712010-09-15 04:33:27 +00002497 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002498 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002499 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002500 Name.startswith("shl") || Name.startswith("sal") ||
2501 Name.startswith("rcl") || Name.startswith("rcr") ||
2502 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002503 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002504 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002505 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002506 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2507 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2508 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002509 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002510 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002511 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2512 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2513 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002514 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002515 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002516 }
Chad Rosier51afe632012-06-27 22:34:28 +00002517
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002518 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2519 // instalias with an immediate operand yet.
2520 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002521 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
Duncan P. N. Exon Smithd5313222015-07-23 19:27:07 +00002522 if (Op1.isImm())
2523 if (auto *CE = dyn_cast<MCConstantExpr>(Op1.getImm()))
2524 if (CE->getValue() == 3) {
2525 Operands.erase(Operands.begin() + 1);
2526 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
2527 }
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002528 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002529
Marina Yatsinad9658d12016-01-19 16:35:38 +00002530 // Transforms "xlat mem8" into "xlatb"
2531 if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) {
2532 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2533 if (Op1.isMem8()) {
2534 Warning(Op1.getStartLoc(), "memory operand is only for determining the "
2535 "size, (R|E)BX will be used for the location");
2536 Operands.pop_back();
2537 static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb");
2538 }
2539 }
2540
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002541 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002542}
2543
David Blaikie960ea3f2014-06-08 16:18:35 +00002544bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Aaron Ballmana81264b2016-05-23 15:52:59 +00002545 return false;
Devang Patelde47cce2012-01-18 22:42:29 +00002546}
2547
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002548static const char *getSubtargetFeatureName(uint64_t Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002549
David Blaikie960ea3f2014-06-08 16:18:35 +00002550void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2551 MCStreamer &Out) {
Evgeniy Stepanov77ad8662014-07-31 09:11:04 +00002552 Instrumentation->InstrumentAndEmitInstruction(Inst, Operands, getContext(),
2553 MII, Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002554}
2555
David Blaikie960ea3f2014-06-08 16:18:35 +00002556bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2557 OperandVector &Operands,
Tim Northover26bb14e2014-08-18 11:49:42 +00002558 MCStreamer &Out, uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00002559 bool MatchingInlineAsm) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002560 if (isParsingIntelSyntax())
2561 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002562 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002563 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002564 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002565}
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002566
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002567void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
2568 OperandVector &Operands, MCStreamer &Out,
2569 bool MatchingInlineAsm) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002570 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002571 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002572 // call.
Reid Klecknerb1f2d2f2014-07-31 00:07:33 +00002573 const char *Repl = StringSwitch<const char *>(Op.getToken())
2574 .Case("finit", "fninit")
2575 .Case("fsave", "fnsave")
2576 .Case("fstcw", "fnstcw")
2577 .Case("fstcww", "fnstcw")
2578 .Case("fstenv", "fnstenv")
2579 .Case("fstsw", "fnstsw")
2580 .Case("fstsww", "fnstsw")
2581 .Case("fclex", "fnclex")
2582 .Default(nullptr);
2583 if (Repl) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002584 MCInst Inst;
2585 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002586 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002587 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002588 EmitInstruction(Inst, Operands, Out);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002589 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002590 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002591}
2592
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002593bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002594 bool MatchingInlineAsm) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002595 assert(ErrorInfo && "Unknown missing feature!");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002596 SmallString<126> Msg;
2597 raw_svector_ostream OS(Msg);
2598 OS << "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002599 uint64_t Mask = 1;
2600 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2601 if (ErrorInfo & Mask)
2602 OS << ' ' << getSubtargetFeatureName(ErrorInfo & Mask);
2603 Mask <<= 1;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002604 }
Nirav Dave2364748a2016-09-16 18:30:20 +00002605 return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002606}
2607
2608bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
2609 OperandVector &Operands,
2610 MCStreamer &Out,
2611 uint64_t &ErrorInfo,
2612 bool MatchingInlineAsm) {
2613 assert(!Operands.empty() && "Unexpect empty operand list!");
2614 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2615 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Nirav Dave2364748a2016-09-16 18:30:20 +00002616 SMRange EmptyRange = None;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002617
2618 // First, handle aliases that expand to multiple instructions.
2619 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002620
Chris Lattner628fbec2010-09-06 21:54:15 +00002621 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002622 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002623
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002624 // First, try a direct match.
Nirav Dave6477ce22016-09-26 19:33:36 +00002625 switch (MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
2626 isParsingIntelSyntax())) {
Craig Topper589ceee2015-01-03 08:16:34 +00002627 default: llvm_unreachable("Unexpected match result!");
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002628 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002629 // Some instructions need post-processing to, for example, tweak which
2630 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002631 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002632 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002633 while (processInstruction(Inst, Operands))
2634 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002635
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002636 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002637 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002638 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002639 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002640 return false;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002641 case Match_MissingFeature:
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002642 return ErrorMissingFeature(IDLoc, ErrorInfo, MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002643 case Match_InvalidOperand:
2644 WasOriginallyInvalidOperand = true;
2645 break;
2646 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002647 break;
2648 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002649
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002650 // FIXME: Ideally, we would only attempt suffix matches for things which are
2651 // valid prefixes, and we could just infer the right unambiguous
2652 // type. However, that requires substantially more matcher support than the
2653 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002654
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002655 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002656 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002657 SmallString<16> Tmp;
2658 Tmp += Base;
2659 Tmp += ' ';
Yaron Keren075759a2015-03-30 15:42:36 +00002660 Op.setTokenValue(Tmp);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002661
Chris Lattnerfab94132010-11-06 18:28:02 +00002662 // If this instruction starts with an 'f', then it is a floating point stack
2663 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2664 // 80-bit floating point, which use the suffixes s,l,t respectively.
2665 //
2666 // Otherwise, we assume that this may be an integer instruction, which comes
2667 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2668 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002669
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002670 // Check for the various suffix matches.
Tim Northover26bb14e2014-08-18 11:49:42 +00002671 uint64_t ErrorInfoIgnore;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002672 uint64_t ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002673 unsigned Match[4];
Chad Rosier51afe632012-06-27 22:34:28 +00002674
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002675 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
2676 Tmp.back() = Suffixes[I];
Nirav Dave6477ce22016-09-26 19:33:36 +00002677 Match[I] = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2678 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002679 // If this returned as a missing feature failure, remember that.
2680 if (Match[I] == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002681 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002682 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002683
2684 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002685 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002686
2687 // If exactly one matched, then we treat that as a successful match (and the
2688 // instruction will already have been filled in correctly, since the failing
2689 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002690 unsigned NumSuccessfulMatches =
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002691 std::count(std::begin(Match), std::end(Match), Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002692 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002693 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002694 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002695 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002696 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002697 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002698 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002699
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002700 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002701
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002702 // If we had multiple suffix matches, then identify this as an ambiguous
2703 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002704 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002705 char MatchChars[4];
2706 unsigned NumMatches = 0;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002707 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
2708 if (Match[I] == Match_Success)
2709 MatchChars[NumMatches++] = Suffixes[I];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002710
Alp Tokere69170a2014-06-26 22:52:05 +00002711 SmallString<126> Msg;
2712 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002713 OS << "ambiguous instructions require an explicit suffix (could be ";
2714 for (unsigned i = 0; i != NumMatches; ++i) {
2715 if (i != 0)
2716 OS << ", ";
2717 if (i + 1 == NumMatches)
2718 OS << "or ";
2719 OS << "'" << Base << MatchChars[i] << "'";
2720 }
2721 OS << ")";
Nirav Dave2364748a2016-09-16 18:30:20 +00002722 Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002723 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002724 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002725
Chris Lattner628fbec2010-09-06 21:54:15 +00002726 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002727
Chris Lattner628fbec2010-09-06 21:54:15 +00002728 // If all of the instructions reported an invalid mnemonic, then the original
2729 // mnemonic was invalid.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002730 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002731 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002732 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002733 Op.getLocRange(), MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002734 }
2735
2736 // Recover location info for the operand if we know which was the problem.
Tim Northover26bb14e2014-08-18 11:49:42 +00002737 if (ErrorInfo != ~0ULL) {
Chad Rosier49963552012-10-13 00:26:04 +00002738 if (ErrorInfo >= Operands.size())
Nirav Dave2364748a2016-09-16 18:30:20 +00002739 return Error(IDLoc, "too few operands for instruction", EmptyRange,
2740 MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002741
David Blaikie960ea3f2014-06-08 16:18:35 +00002742 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2743 if (Operand.getStartLoc().isValid()) {
2744 SMRange OperandRange = Operand.getLocRange();
2745 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002746 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002747 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002748 }
2749
Nirav Dave2364748a2016-09-16 18:30:20 +00002750 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Chad Rosier4453e842012-10-12 23:09:25 +00002751 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002752 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002753
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002754 // If one instruction matched with a missing feature, report this as a
2755 // missing feature.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002756 if (std::count(std::begin(Match), std::end(Match),
2757 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002758 ErrorInfo = ErrorInfoMissingFeature;
2759 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002760 MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002761 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002762
Chris Lattner628fbec2010-09-06 21:54:15 +00002763 // If one instruction matched with an invalid operand, report this as an
2764 // operand failure.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002765 if (std::count(std::begin(Match), std::end(Match),
2766 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002767 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002768 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002769 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002770
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002771 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002772 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Nirav Dave2364748a2016-09-16 18:30:20 +00002773 EmptyRange, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002774 return true;
2775}
2776
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002777bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
2778 OperandVector &Operands,
2779 MCStreamer &Out,
2780 uint64_t &ErrorInfo,
2781 bool MatchingInlineAsm) {
2782 assert(!Operands.empty() && "Unexpect empty operand list!");
2783 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2784 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
2785 StringRef Mnemonic = Op.getToken();
Nirav Dave2364748a2016-09-16 18:30:20 +00002786 SMRange EmptyRange = None;
Nirav Daveee554e62016-10-06 15:28:08 +00002787 StringRef Base = Op.getToken();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002788
2789 // First, handle aliases that expand to multiple instructions.
2790 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
2791
2792 MCInst Inst;
2793
2794 // Find one unsized memory operand, if present.
2795 X86Operand *UnsizedMemOp = nullptr;
2796 for (const auto &Op : Operands) {
2797 X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002798 if (X86Op->isMemUnsized())
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002799 UnsizedMemOp = X86Op;
2800 }
2801
2802 // Allow some instructions to have implicitly pointer-sized operands. This is
2803 // compatible with gas.
2804 if (UnsizedMemOp) {
2805 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"};
2806 for (const char *Instr : PtrSizedInstrs) {
2807 if (Mnemonic == Instr) {
Craig Topper055845f2015-01-02 07:02:25 +00002808 UnsizedMemOp->Mem.Size = getPointerWidth();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002809 break;
2810 }
2811 }
2812 }
2813
Nirav Daveee554e62016-10-06 15:28:08 +00002814 SmallVector<unsigned, 8> Match;
2815 uint64_t ErrorInfoMissingFeature = 0;
2816
2817 // If unsized push has immediate operand we should default the default pointer
2818 // size for the size.
2819 if (Mnemonic == "push" && Operands.size() == 2) {
2820 auto *X86Op = static_cast<X86Operand *>(Operands[1].get());
2821 if (X86Op->isImm()) {
2822 // If it's not a constant fall through and let remainder take care of it.
2823 const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm());
2824 unsigned Size = getPointerWidth();
2825 if (CE &&
2826 (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) {
2827 SmallString<16> Tmp;
2828 Tmp += Base;
2829 Tmp += (is64BitMode())
2830 ? "q"
2831 : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " ";
2832 Op.setTokenValue(Tmp);
2833 // Do match in ATT mode to allow explicit suffix usage.
2834 Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo,
2835 MatchingInlineAsm,
2836 false /*isParsingIntelSyntax()*/));
2837 Op.setTokenValue(Base);
2838 }
2839 }
2840 }
2841
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002842 // If an unsized memory operand is present, try to match with each memory
2843 // operand size. In Intel assembly, the size is not part of the instruction
2844 // mnemonic.
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002845 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) {
Ahmed Bougachad65f7872014-12-03 02:03:26 +00002846 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512};
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002847 for (unsigned Size : MopSizes) {
2848 UnsizedMemOp->Mem.Size = Size;
2849 uint64_t ErrorInfoIgnore;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002850 unsigned LastOpcode = Inst.getOpcode();
Nirav Dave6477ce22016-09-26 19:33:36 +00002851 unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2852 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002853 if (Match.empty() || LastOpcode != Inst.getOpcode())
2854 Match.push_back(M);
2855
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002856 // If this returned as a missing feature failure, remember that.
2857 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002858 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002859 }
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002860
2861 // Restore the size of the unsized memory operand if we modified it.
2862 if (UnsizedMemOp)
2863 UnsizedMemOp->Mem.Size = 0;
2864 }
2865
2866 // If we haven't matched anything yet, this is not a basic integer or FPU
Saleem Abdulrasoolc3f8ad32015-01-16 20:16:06 +00002867 // operation. There shouldn't be any ambiguity in our mnemonic table, so try
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002868 // matching with the unsized operand.
2869 if (Match.empty()) {
Nirav Dave6477ce22016-09-26 19:33:36 +00002870 Match.push_back(MatchInstruction(
2871 Operands, Inst, ErrorInfo, MatchingInlineAsm, isParsingIntelSyntax()));
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002872 // If this returned as a missing feature failure, remember that.
2873 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002874 ErrorInfoMissingFeature = ErrorInfo;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002875 }
2876
2877 // Restore the size of the unsized memory operand if we modified it.
2878 if (UnsizedMemOp)
2879 UnsizedMemOp->Mem.Size = 0;
2880
2881 // If it's a bad mnemonic, all results will be the same.
2882 if (Match.back() == Match_MnemonicFail) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002883 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002884 Op.getLocRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002885 }
2886
2887 // If exactly one matched, then we treat that as a successful match (and the
2888 // instruction will already have been filled in correctly, since the failing
2889 // matches won't have modified it).
2890 unsigned NumSuccessfulMatches =
2891 std::count(std::begin(Match), std::end(Match), Match_Success);
2892 if (NumSuccessfulMatches == 1) {
2893 // Some instructions need post-processing to, for example, tweak which
2894 // encoding is selected. Loop on it while changes happen so the individual
2895 // transformations can chain off each other.
2896 if (!MatchingInlineAsm)
2897 while (processInstruction(Inst, Operands))
2898 ;
2899 Inst.setLoc(IDLoc);
2900 if (!MatchingInlineAsm)
2901 EmitInstruction(Inst, Operands, Out);
2902 Opcode = Inst.getOpcode();
2903 return false;
2904 } else if (NumSuccessfulMatches > 1) {
2905 assert(UnsizedMemOp &&
2906 "multiple matches only possible with unsized memory operands");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002907 return Error(UnsizedMemOp->getStartLoc(),
2908 "ambiguous operand size for instruction '" + Mnemonic + "\'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002909 UnsizedMemOp->getLocRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002910 }
2911
2912 // If one instruction matched with a missing feature, report this as a
2913 // missing feature.
2914 if (std::count(std::begin(Match), std::end(Match),
2915 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002916 ErrorInfo = ErrorInfoMissingFeature;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002917 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
2918 MatchingInlineAsm);
2919 }
2920
2921 // If one instruction matched with an invalid operand, report this as an
2922 // operand failure.
2923 if (std::count(std::begin(Match), std::end(Match),
2924 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002925 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002926 MatchingInlineAsm);
2927 }
2928
2929 // If all of these were an outright failure, report it in a useless way.
Nirav Dave2364748a2016-09-16 18:30:20 +00002930 return Error(IDLoc, "unknown instruction mnemonic", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002931 MatchingInlineAsm);
2932}
2933
Nico Weber42f79db2014-07-17 20:24:55 +00002934bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
2935 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
2936}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002937
Devang Patel4a6e7782012-01-12 18:03:40 +00002938bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002939 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00002940 StringRef IDVal = DirectiveID.getIdentifier();
2941 if (IDVal == ".word")
2942 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002943 else if (IDVal.startswith(".code"))
2944 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002945 else if (IDVal.startswith(".att_syntax")) {
Reid Klecknerce63b792014-08-06 23:21:13 +00002946 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2947 if (Parser.getTok().getString() == "prefix")
2948 Parser.Lex();
2949 else if (Parser.getTok().getString() == "noprefix")
2950 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
2951 "supported: registers must have a "
2952 "'%' prefix in .att_syntax");
2953 }
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002954 getParser().setAssemblerDialect(0);
2955 return false;
2956 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002957 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002958 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002959 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002960 Parser.Lex();
Reid Klecknerce63b792014-08-06 23:21:13 +00002961 else if (Parser.getTok().getString() == "prefix")
2962 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
2963 "supported: registers must not have "
2964 "a '%' prefix in .intel_syntax");
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002965 }
2966 return false;
Michael Zuckerman02ecd432015-12-13 17:07:23 +00002967 } else if (IDVal == ".even")
2968 return parseDirectiveEven(DirectiveID.getLoc());
Chris Lattner72c0b592010-10-30 17:38:55 +00002969 return true;
2970}
2971
Michael Zuckerman02ecd432015-12-13 17:07:23 +00002972/// parseDirectiveEven
2973/// ::= .even
2974bool X86AsmParser::parseDirectiveEven(SMLoc L) {
2975 const MCSection *Section = getStreamer().getCurrentSection().first;
2976 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2977 TokError("unexpected token in directive");
2978 return false;
2979 }
2980 if (!Section) {
2981 getStreamer().InitSections(false);
2982 Section = getStreamer().getCurrentSection().first;
2983 }
2984 if (Section->UseCodeAlign())
2985 getStreamer().EmitCodeAlignment(2, 0);
2986 else
2987 getStreamer().EmitValueToAlignment(2, 0, 1, 0);
2988 return false;
2989}
Chris Lattner72c0b592010-10-30 17:38:55 +00002990/// ParseDirectiveWord
2991/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002992bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002993 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00002994 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2995 for (;;) {
2996 const MCExpr *Value;
David Majnemera375b262015-10-26 02:45:50 +00002997 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002998 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002999 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00003000
David Majnemera375b262015-10-26 02:45:50 +00003001 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
3002 assert(Size <= 8 && "Invalid size");
3003 uint64_t IntValue = MCE->getValue();
3004 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
3005 return Error(ExprLoc, "literal value out of range for directive");
3006 getStreamer().EmitIntValue(IntValue, Size);
3007 } else {
3008 getStreamer().EmitValue(Value, Size, ExprLoc);
3009 }
Chad Rosier51afe632012-06-27 22:34:28 +00003010
Chris Lattner72c0b592010-10-30 17:38:55 +00003011 if (getLexer().is(AsmToken::EndOfStatement))
3012 break;
Chad Rosier51afe632012-06-27 22:34:28 +00003013
Chris Lattner72c0b592010-10-30 17:38:55 +00003014 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003015 if (getLexer().isNot(AsmToken::Comma)) {
3016 Error(L, "unexpected token in directive");
3017 return false;
3018 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003019 Parser.Lex();
3020 }
3021 }
Chad Rosier51afe632012-06-27 22:34:28 +00003022
Chris Lattner72c0b592010-10-30 17:38:55 +00003023 Parser.Lex();
3024 return false;
3025}
3026
Evan Cheng481ebb02011-07-27 00:38:12 +00003027/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00003028/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00003029bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003030 MCAsmParser &Parser = getParser();
Nirav Dave6477ce22016-09-26 19:33:36 +00003031 Code16GCC = false;
Craig Topper3c80d622014-01-06 04:55:54 +00003032 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00003033 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00003034 if (!is16BitMode()) {
3035 SwitchMode(X86::Mode16Bit);
3036 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3037 }
Nirav Dave6477ce22016-09-26 19:33:36 +00003038 } else if (IDVal == ".code16gcc") {
3039 // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode.
3040 Parser.Lex();
3041 Code16GCC = true;
3042 if (!is16BitMode()) {
3043 SwitchMode(X86::Mode16Bit);
3044 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3045 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003046 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00003047 Parser.Lex();
3048 if (!is32BitMode()) {
3049 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003050 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
3051 }
3052 } else if (IDVal == ".code64") {
3053 Parser.Lex();
3054 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00003055 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003056 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
3057 }
3058 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003059 Error(L, "unknown directive " + IDVal);
3060 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00003061 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003062
Evan Cheng481ebb02011-07-27 00:38:12 +00003063 return false;
3064}
Chris Lattner72c0b592010-10-30 17:38:55 +00003065
Daniel Dunbar71475772009-07-17 20:42:00 +00003066// Force static initialization.
3067extern "C" void LLVMInitializeX86AsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00003068 RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target());
3069 RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target());
Daniel Dunbar71475772009-07-17 20:42:00 +00003070}
Daniel Dunbar00331992009-07-29 00:02:19 +00003071
Chris Lattner3e4582a2010-09-06 19:11:01 +00003072#define GET_REGISTER_MATCHER
3073#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00003074#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00003075#include "X86GenAsmMatcher.inc"