blob: f680728995ebe32d0f6f93c07220326ff089b414 [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.
Michael Zuckerman1bee6342016-10-18 13:52:39 +0000762 /// return false if no parsing errors occurred, true 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
Michael Zuckerman1bee6342016-10-18 13:52:39 +0000766 bool ParseZ(std::unique_ptr<X86Operand> &Z, const SMLoc &StartLoc);
767
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000768 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000769 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000770 return getSTI().getFeatureBits()[X86::Mode64Bit];
Evan Cheng4d1ca962011-07-08 01:53:10 +0000771 }
Craig Topper3c80d622014-01-06 04:55:54 +0000772 bool is32BitMode() const {
773 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000774 return getSTI().getFeatureBits()[X86::Mode32Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000775 }
776 bool is16BitMode() const {
777 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000778 return getSTI().getFeatureBits()[X86::Mode16Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000779 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000780 void SwitchMode(unsigned mode) {
Akira Hatanakab11ef082015-11-14 06:35:56 +0000781 MCSubtargetInfo &STI = copySTI();
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000782 FeatureBitset AllModes({X86::Mode64Bit, X86::Mode32Bit, X86::Mode16Bit});
783 FeatureBitset OldMode = STI.getFeatureBits() & AllModes;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000784 unsigned FB = ComputeAvailableFeatures(
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000785 STI.ToggleFeature(OldMode.flip(mode)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000786 setAvailableFeatures(FB);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000787
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000788 assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes));
Evan Cheng481ebb02011-07-27 00:38:12 +0000789 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000790
Reid Kleckner5b37c182014-08-01 20:21:24 +0000791 unsigned getPointerWidth() {
792 if (is16BitMode()) return 16;
793 if (is32BitMode()) return 32;
794 if (is64BitMode()) return 64;
795 llvm_unreachable("invalid mode");
796 }
797
Chad Rosierc2f055d2013-04-18 16:13:18 +0000798 bool isParsingIntelSyntax() {
799 return getParser().getAssemblerDialect();
800 }
801
Daniel Dunbareefe8612010-07-19 05:44:09 +0000802 /// @name Auto-generated Matcher Functions
803 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000804
Chris Lattner3e4582a2010-09-06 19:11:01 +0000805#define GET_ASSEMBLER_HEADER
806#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000807
Daniel Dunbar00331992009-07-29 00:02:19 +0000808 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000809
810public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000811 X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser,
Rafael Espindola961d4692014-11-11 05:18:41 +0000812 const MCInstrInfo &mii, const MCTargetOptions &Options)
Nirav Dave6477ce22016-09-26 19:33:36 +0000813 : MCTargetAsmParser(Options, sti), MII(mii), InstInfo(nullptr),
814 Code16GCC(false) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000815
Daniel Dunbareefe8612010-07-19 05:44:09 +0000816 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000817 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000818 Instrumentation.reset(
819 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000820 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000821
Craig Topper39012cc2014-03-09 18:03:14 +0000822 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000823
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000824 void SetFrameRegister(unsigned RegNo) override;
825
David Blaikie960ea3f2014-06-08 16:18:35 +0000826 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
827 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000828
Craig Topper39012cc2014-03-09 18:03:14 +0000829 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000830};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000831} // end anonymous namespace
832
Sean Callanan86c11812010-01-23 00:40:33 +0000833/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000834/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000835
Chris Lattner60db0a62010-02-09 00:34:28 +0000836static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000837
838/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000839
Kevin Enderbybc570f22014-01-23 22:34:42 +0000840static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
841 StringRef &ErrMsg) {
842 // If we have both a base register and an index register make sure they are
843 // both 64-bit or 32-bit registers.
844 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Douglas Katzman0411e862016-10-05 15:23:35 +0000845
846 if ((BaseReg == X86::RIP && IndexReg != 0) || (IndexReg == X86::RIP)) {
847 ErrMsg = "invalid base+index expression";
848 return true;
849 }
Kevin Enderbybc570f22014-01-23 22:34:42 +0000850 if (BaseReg != 0 && IndexReg != 0) {
851 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
852 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
853 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
854 IndexReg != X86::RIZ) {
855 ErrMsg = "base register is 64-bit, but index register is not";
856 return true;
857 }
858 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
859 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
860 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
861 IndexReg != X86::EIZ){
862 ErrMsg = "base register is 32-bit, but index register is not";
863 return true;
864 }
865 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
866 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
867 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
868 ErrMsg = "base register is 16-bit, but index register is not";
869 return true;
870 }
871 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
872 IndexReg != X86::SI && IndexReg != X86::DI) ||
873 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
874 IndexReg != X86::BX && IndexReg != X86::BP)) {
875 ErrMsg = "invalid 16-bit base/index register combination";
876 return true;
877 }
878 }
879 }
880 return false;
881}
882
Devang Patel4a6e7782012-01-12 18:03:40 +0000883bool X86AsmParser::ParseRegister(unsigned &RegNo,
884 SMLoc &StartLoc, SMLoc &EndLoc) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000885 MCAsmParser &Parser = getParser();
Chris Lattnercc2ad082010-01-15 18:27:19 +0000886 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000887 const AsmToken &PercentTok = Parser.getTok();
888 StartLoc = PercentTok.getLoc();
889
890 // If we encounter a %, ignore it. This code handles registers with and
891 // without the prefix, unprefixed registers can occur in cfi directives.
892 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000893 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000894
Sean Callanan936b0d32010-01-19 21:44:56 +0000895 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000896 EndLoc = Tok.getEndLoc();
897
Devang Patelce6a2ca2012-01-20 22:32:05 +0000898 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000899 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000900 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000901 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000902 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000903
Kevin Enderby7d912182009-09-03 17:15:07 +0000904 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000905
Chris Lattner1261b812010-09-22 04:11:10 +0000906 // If the match failed, try the register name as lowercase.
907 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000908 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000909
Michael Kupersteincdb076b2015-07-30 10:10:25 +0000910 // The "flags" register cannot be referenced directly.
911 // Treat it as an identifier instead.
912 if (isParsingInlineAsm() && isParsingIntelSyntax() && RegNo == X86::EFLAGS)
913 RegNo = 0;
914
Evan Chengeda1d4f2011-07-27 23:22:03 +0000915 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000916 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000917 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
918 // checked.
919 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
920 // REX prefix.
921 if (RegNo == X86::RIZ ||
922 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
923 X86II::isX86_64NonExtLowByteReg(RegNo) ||
Craig Topper6acca802016-08-27 17:13:37 +0000924 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000925 return Error(StartLoc, "register %"
926 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000927 SMRange(StartLoc, EndLoc));
Craig Topper29c22732016-02-26 05:29:32 +0000928 } else if (!getSTI().getFeatureBits()[X86::FeatureAVX512]) {
929 if (X86II::is32ExtendedReg(RegNo))
930 return Error(StartLoc, "register %"
Craig Topperd50b5f82016-02-26 06:50:24 +0000931 + Tok.getString() + " is only available with AVX512",
Craig Topper29c22732016-02-26 05:29:32 +0000932 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000933 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000934
Chris Lattner1261b812010-09-22 04:11:10 +0000935 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
936 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000937 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000938 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000939
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000940 // Check to see if we have '(4)' after %st.
941 if (getLexer().isNot(AsmToken::LParen))
942 return false;
943 // Lex the paren.
944 getParser().Lex();
945
946 const AsmToken &IntTok = Parser.getTok();
947 if (IntTok.isNot(AsmToken::Integer))
948 return Error(IntTok.getLoc(), "expected stack index");
949 switch (IntTok.getIntVal()) {
950 case 0: RegNo = X86::ST0; break;
951 case 1: RegNo = X86::ST1; break;
952 case 2: RegNo = X86::ST2; break;
953 case 3: RegNo = X86::ST3; break;
954 case 4: RegNo = X86::ST4; break;
955 case 5: RegNo = X86::ST5; break;
956 case 6: RegNo = X86::ST6; break;
957 case 7: RegNo = X86::ST7; break;
958 default: return Error(IntTok.getLoc(), "invalid stack index");
959 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000960
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000961 if (getParser().Lex().isNot(AsmToken::RParen))
962 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000963
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000964 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000965 Parser.Lex(); // Eat ')'
966 return false;
967 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000968
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000969 EndLoc = Parser.getTok().getEndLoc();
970
Chris Lattner80486622010-06-24 07:29:18 +0000971 // If this is "db[0-7]", match it as an alias
972 // for dr[0-7].
973 if (RegNo == 0 && Tok.getString().size() == 3 &&
974 Tok.getString().startswith("db")) {
975 switch (Tok.getString()[2]) {
976 case '0': RegNo = X86::DR0; break;
977 case '1': RegNo = X86::DR1; break;
978 case '2': RegNo = X86::DR2; break;
979 case '3': RegNo = X86::DR3; break;
980 case '4': RegNo = X86::DR4; break;
981 case '5': RegNo = X86::DR5; break;
982 case '6': RegNo = X86::DR6; break;
983 case '7': RegNo = X86::DR7; break;
984 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000985
Chris Lattner80486622010-06-24 07:29:18 +0000986 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000987 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000988 Parser.Lex(); // Eat it.
989 return false;
990 }
991 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000992
Devang Patelce6a2ca2012-01-20 22:32:05 +0000993 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000994 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000995 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000996 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000997 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000998
Sean Callanana83fd7d2010-01-19 20:27:46 +0000999 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001000 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001001}
1002
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001003void X86AsmParser::SetFrameRegister(unsigned RegNo) {
Yuri Gorshenine8c81fd2014-10-07 11:03:09 +00001004 Instrumentation->SetInitialFrameRegister(RegNo);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001005}
1006
David Blaikie960ea3f2014-06-08 16:18:35 +00001007std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001008 bool Parse32 = is32BitMode() || Code16GCC;
1009 unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001010 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001011 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001012 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001013 Loc, Loc, 0);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001014}
1015
David Blaikie960ea3f2014-06-08 16:18:35 +00001016std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001017 bool Parse32 = is32BitMode() || Code16GCC;
1018 unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001019 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001020 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001021 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001022 Loc, Loc, 0);
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001023}
1024
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001025bool X86AsmParser::IsSIReg(unsigned Reg) {
1026 switch (Reg) {
Craig Topper4d187632016-02-26 05:29:39 +00001027 default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001028 case X86::RSI:
1029 case X86::ESI:
1030 case X86::SI:
1031 return true;
1032 case X86::RDI:
1033 case X86::EDI:
1034 case X86::DI:
1035 return false;
1036 }
1037}
1038
1039unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg,
1040 bool IsSIReg) {
1041 switch (RegClassID) {
Craig Topper4d187632016-02-26 05:29:39 +00001042 default: llvm_unreachable("Unexpected register class");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001043 case X86::GR64RegClassID:
1044 return IsSIReg ? X86::RSI : X86::RDI;
1045 case X86::GR32RegClassID:
1046 return IsSIReg ? X86::ESI : X86::EDI;
1047 case X86::GR16RegClassID:
1048 return IsSIReg ? X86::SI : X86::DI;
1049 }
1050}
1051
Michael Kupersteinffcc7662015-07-23 10:23:48 +00001052void X86AsmParser::AddDefaultSrcDestOperands(
1053 OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
1054 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) {
1055 if (isParsingIntelSyntax()) {
1056 Operands.push_back(std::move(Dst));
1057 Operands.push_back(std::move(Src));
1058 }
1059 else {
1060 Operands.push_back(std::move(Src));
1061 Operands.push_back(std::move(Dst));
1062 }
1063}
1064
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001065bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands,
1066 OperandVector &FinalOperands) {
1067
1068 if (OrigOperands.size() > 1) {
Craig Topperd55f4bc2016-02-16 07:45:07 +00001069 // Check if sizes match, OrigOperands also contains the instruction name
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001070 assert(OrigOperands.size() == FinalOperands.size() + 1 &&
Craig Topperd55f4bc2016-02-16 07:45:07 +00001071 "Operand size mismatch");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001072
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001073 SmallVector<std::pair<SMLoc, std::string>, 2> Warnings;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001074 // Verify types match
1075 int RegClassID = -1;
1076 for (unsigned int i = 0; i < FinalOperands.size(); ++i) {
1077 X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]);
1078 X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]);
1079
1080 if (FinalOp.isReg() &&
1081 (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg()))
1082 // Return false and let a normal complaint about bogus operands happen
1083 return false;
1084
1085 if (FinalOp.isMem()) {
1086
1087 if (!OrigOp.isMem())
1088 // Return false and let a normal complaint about bogus operands happen
1089 return false;
1090
1091 unsigned OrigReg = OrigOp.Mem.BaseReg;
1092 unsigned FinalReg = FinalOp.Mem.BaseReg;
1093
1094 // If we've already encounterd a register class, make sure all register
1095 // bases are of the same register class
1096 if (RegClassID != -1 &&
1097 !X86MCRegisterClasses[RegClassID].contains(OrigReg)) {
1098 return Error(OrigOp.getStartLoc(),
1099 "mismatching source and destination index registers");
1100 }
1101
1102 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg))
1103 RegClassID = X86::GR64RegClassID;
1104 else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg))
1105 RegClassID = X86::GR32RegClassID;
1106 else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg))
1107 RegClassID = X86::GR16RegClassID;
Marina Yatsina701938d2016-01-20 14:03:47 +00001108 else
Craig Topper5a62f7e2016-02-16 07:28:03 +00001109 // Unexpected register class type
Marina Yatsina701938d2016-01-20 14:03:47 +00001110 // Return false and let a normal complaint about bogus operands happen
1111 return false;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001112
1113 bool IsSI = IsSIReg(FinalReg);
1114 FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI);
1115
1116 if (FinalReg != OrigReg) {
1117 std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI";
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001118 Warnings.push_back(std::make_pair(
1119 OrigOp.getStartLoc(),
1120 "memory operand is only for determining the size, " + RegName +
1121 " will be used for the location"));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001122 }
1123
1124 FinalOp.Mem.Size = OrigOp.Mem.Size;
1125 FinalOp.Mem.SegReg = OrigOp.Mem.SegReg;
1126 FinalOp.Mem.BaseReg = FinalReg;
1127 }
1128 }
1129
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001130 // Produce warnings only if all the operands passed the adjustment - prevent
1131 // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings
Craig Topper16d7eb22016-02-16 07:45:04 +00001132 for (auto &WarningMsg : Warnings) {
1133 Warning(WarningMsg.first, WarningMsg.second);
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001134 }
1135
1136 // Remove old operands
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001137 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1138 OrigOperands.pop_back();
1139 }
1140 // OrigOperands.append(FinalOperands.begin(), FinalOperands.end());
1141 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1142 OrigOperands.push_back(std::move(FinalOperands[i]));
1143
1144 return false;
1145}
1146
David Blaikie960ea3f2014-06-08 16:18:35 +00001147std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001148 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001149 return ParseIntelOperand();
1150 return ParseATTOperand();
1151}
1152
Devang Patel41b9dde2012-01-17 18:00:18 +00001153/// getIntelMemOperandSize - Return intel memory operand size.
1154static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001155 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001156 .Cases("BYTE", "byte", 8)
1157 .Cases("WORD", "word", 16)
1158 .Cases("DWORD", "dword", 32)
Marina Yatsina497d44a2015-12-07 13:09:20 +00001159 .Cases("FWORD", "fword", 48)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001160 .Cases("QWORD", "qword", 64)
Michael Zuckerman9beca2e2015-08-24 10:26:54 +00001161 .Cases("MMWORD","mmword", 64)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001162 .Cases("XWORD", "xword", 80)
Michael Kuperstein69e40a42015-07-19 11:03:08 +00001163 .Cases("TBYTE", "tbyte", 80)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001164 .Cases("XMMWORD", "xmmword", 128)
1165 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +00001166 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +00001167 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +00001168 .Default(0);
1169 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001170}
1171
David Blaikie960ea3f2014-06-08 16:18:35 +00001172std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
1173 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
1174 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
1175 InlineAsmIdentifierInfo &Info) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001176 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
1177 // some other label reference.
1178 if (isa<MCSymbolRefExpr>(Disp) && Info.OpDecl && !Info.IsVarDecl) {
1179 // Insert an explicit size if the user didn't have one.
1180 if (!Size) {
1181 Size = getPointerWidth();
Craig Topper7d5b2312015-10-10 05:25:02 +00001182 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
1183 /*Len=*/0, Size);
Reid Kleckner5b37c182014-08-01 20:21:24 +00001184 }
1185
1186 // Create an absolute memory reference in order to match against
1187 // instructions taking a PC relative operand.
Craig Topper055845f2015-01-02 07:02:25 +00001188 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size,
1189 Identifier, Info.OpDecl);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001190 }
1191
1192 // We either have a direct symbol reference, or an offset from a symbol. The
1193 // parser always puts the symbol on the LHS, so look there for size
1194 // calculation purposes.
1195 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
1196 bool IsSymRef =
1197 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
1198 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001199 if (!Size) {
1200 Size = Info.Type * 8; // Size is in terms of bits in this context.
1201 if (Size)
Craig Topper7d5b2312015-10-10 05:25:02 +00001202 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
1203 /*Len=*/0, Size);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001204 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001205 }
1206
Chad Rosier7ca135b2013-03-19 21:11:56 +00001207 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001208 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001209 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001210 BaseReg = BaseReg ? BaseReg : 1;
Craig Topper055845f2015-01-02 07:02:25 +00001211 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1212 IndexReg, Scale, Start, End, Size, Identifier,
1213 Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001214}
1215
Chad Rosierd383db52013-04-12 20:20:54 +00001216static void
Craig Topper7143d802015-10-10 05:25:06 +00001217RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> &AsmRewrites,
Chad Rosierd383db52013-04-12 20:20:54 +00001218 StringRef SymName, int64_t ImmDisp,
1219 int64_t FinalImmDisp, SMLoc &BracLoc,
1220 SMLoc &StartInBrac, SMLoc &End) {
1221 // Remove the '[' and ']' from the IR string.
Craig Topper7143d802015-10-10 05:25:06 +00001222 AsmRewrites.emplace_back(AOK_Skip, BracLoc, 1);
1223 AsmRewrites.emplace_back(AOK_Skip, End, 1);
Chad Rosierd383db52013-04-12 20:20:54 +00001224
1225 // If ImmDisp is non-zero, then we parsed a displacement before the
1226 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1227 // If ImmDisp doesn't match the displacement computed by the state machine
1228 // then we have an additional displacement in the bracketed expression.
1229 if (ImmDisp != FinalImmDisp) {
1230 if (ImmDisp) {
1231 // We have an immediate displacement before the bracketed expression.
1232 // Adjust this to match the final immediate displacement.
1233 bool Found = false;
Craig Topper7143d802015-10-10 05:25:06 +00001234 for (AsmRewrite &AR : AsmRewrites) {
1235 if (AR.Loc.getPointer() > BracLoc.getPointer())
Chad Rosierd383db52013-04-12 20:20:54 +00001236 continue;
Craig Topper7143d802015-10-10 05:25:06 +00001237 if (AR.Kind == AOK_ImmPrefix || AR.Kind == AOK_Imm) {
Chad Rosierbfb70992013-04-17 00:11:46 +00001238 assert (!Found && "ImmDisp already rewritten.");
Craig Topper7143d802015-10-10 05:25:06 +00001239 AR.Kind = AOK_Imm;
1240 AR.Len = BracLoc.getPointer() - AR.Loc.getPointer();
1241 AR.Val = FinalImmDisp;
Chad Rosierd383db52013-04-12 20:20:54 +00001242 Found = true;
1243 break;
1244 }
1245 }
1246 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001247 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001248 } else {
1249 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001250 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001251 // before the bracketed expression.
Craig Topper7143d802015-10-10 05:25:06 +00001252 AsmRewrites.emplace_back(AOK_Imm, BracLoc, 0, FinalImmDisp);
Chad Rosierd383db52013-04-12 20:20:54 +00001253 }
1254 }
1255 // Remove all the ImmPrefix rewrites within the brackets.
Craig Topper7143d802015-10-10 05:25:06 +00001256 for (AsmRewrite &AR : AsmRewrites) {
1257 if (AR.Loc.getPointer() < StartInBrac.getPointer())
Chad Rosierd383db52013-04-12 20:20:54 +00001258 continue;
Craig Topper7143d802015-10-10 05:25:06 +00001259 if (AR.Kind == AOK_ImmPrefix)
1260 AR.Kind = AOK_Delete;
Chad Rosierd383db52013-04-12 20:20:54 +00001261 }
1262 const char *SymLocPtr = SymName.data();
Michael Liao5bf95782014-12-04 05:20:33 +00001263 // Skip everything before the symbol.
Chad Rosierd383db52013-04-12 20:20:54 +00001264 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1265 assert(Len > 0 && "Expected a non-negative length.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001266 AsmRewrites.emplace_back(AOK_Skip, StartInBrac, Len);
Chad Rosierd383db52013-04-12 20:20:54 +00001267 }
1268 // Skip everything after the symbol.
1269 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1270 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1271 assert(Len > 0 && "Expected a non-negative length.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001272 AsmRewrites.emplace_back(AOK_Skip, Loc, Len);
Chad Rosierd383db52013-04-12 20:20:54 +00001273 }
1274}
1275
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001276bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001277 MCAsmParser &Parser = getParser();
Chad Rosier6844ea02012-10-24 22:13:37 +00001278 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001279
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001280 AsmToken::TokenKind PrevTK = AsmToken::Error;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001281 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001282 while (!Done) {
1283 bool UpdateLocLex = true;
1284
1285 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1286 // identifier. Don't try an parse it as a register.
Nirav Dave8601ac12016-08-02 17:56:03 +00001287 if (PrevTK != AsmToken::Error && Tok.getString().startswith("."))
Chad Rosier5c118fd2013-01-14 22:31:35 +00001288 break;
Michael Liao5bf95782014-12-04 05:20:33 +00001289
Chad Rosierbfb70992013-04-17 00:11:46 +00001290 // If we're parsing an immediate expression, we don't expect a '['.
1291 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1292 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001293
David Majnemer6a5b8122014-06-19 01:25:43 +00001294 AsmToken::TokenKind TK = getLexer().getKind();
1295 switch (TK) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001296 default: {
1297 if (SM.isValidEndState()) {
1298 Done = true;
1299 break;
1300 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001301 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001302 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001303 case AsmToken::EndOfStatement: {
1304 Done = true;
1305 break;
1306 }
David Majnemer6a5b8122014-06-19 01:25:43 +00001307 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001308 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001309 // This could be a register or a symbolic displacement.
1310 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001311 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001312 SMLoc IdentLoc = Tok.getLoc();
1313 StringRef Identifier = Tok.getString();
David Majnemer6a5b8122014-06-19 01:25:43 +00001314 if (TK != AsmToken::String && !ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001315 SM.onRegister(TmpReg);
1316 UpdateLocLex = false;
1317 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001318 } else {
1319 if (!isParsingInlineAsm()) {
1320 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001321 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001322 } else {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001323 // This is a dot operator, not an adjacent identifier.
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001324 if (Identifier.find('.') != StringRef::npos &&
1325 PrevTK == AsmToken::RBrac) {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001326 return false;
1327 } else {
1328 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1329 if (ParseIntelIdentifier(Val, Identifier, Info,
1330 /*Unevaluated=*/false, End))
1331 return true;
1332 }
Chad Rosier95ce8892013-04-19 18:39:50 +00001333 }
1334 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001335 UpdateLocLex = false;
1336 break;
1337 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001338 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001339 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001340 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001341 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001342 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Craig Topper7d5b2312015-10-10 05:25:02 +00001343 InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, Tok.getLoc());
Kevin Enderby36eba252013-12-19 23:16:14 +00001344 // Look for 'b' or 'f' following an Integer as a directional label
1345 SMLoc Loc = getTok().getLoc();
1346 int64_t IntVal = getTok().getIntVal();
1347 End = consumeToken();
1348 UpdateLocLex = false;
1349 if (getLexer().getKind() == AsmToken::Identifier) {
1350 StringRef IDVal = getTok().getString();
1351 if (IDVal == "f" || IDVal == "b") {
1352 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +00001353 getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001354 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Michael Liao5bf95782014-12-04 05:20:33 +00001355 const MCExpr *Val =
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001356 MCSymbolRefExpr::create(Sym, Variant, getContext());
Kevin Enderby36eba252013-12-19 23:16:14 +00001357 if (IDVal == "b" && Sym->isUndefined())
1358 return Error(Loc, "invalid reference to undefined symbol");
1359 StringRef Identifier = Sym->getName();
1360 SM.onIdentifierExpr(Val, Identifier);
1361 End = consumeToken();
1362 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001363 if (SM.onInteger(IntVal, ErrMsg))
1364 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001365 }
1366 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001367 if (SM.onInteger(IntVal, ErrMsg))
1368 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001369 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001370 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001371 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001372 case AsmToken::Plus: SM.onPlus(); break;
1373 case AsmToken::Minus: SM.onMinus(); break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001374 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001375 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001376 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001377 case AsmToken::Pipe: SM.onOr(); break;
Michael Kupersteine3de07a2015-06-14 12:59:45 +00001378 case AsmToken::Caret: SM.onXor(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001379 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001380 case AsmToken::LessLess:
1381 SM.onLShift(); break;
1382 case AsmToken::GreaterGreater:
1383 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001384 case AsmToken::LBrac: SM.onLBrac(); break;
1385 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001386 case AsmToken::LParen: SM.onLParen(); break;
1387 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001388 }
Chad Rosier31246272013-04-17 21:01:45 +00001389 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001390 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001391
Alp Tokera5b88a52013-12-02 16:06:06 +00001392 if (!Done && UpdateLocLex)
1393 End = consumeToken();
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001394
1395 PrevTK = TK;
Devang Patel41b9dde2012-01-17 18:00:18 +00001396 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001397 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001398}
1399
David Blaikie960ea3f2014-06-08 16:18:35 +00001400std::unique_ptr<X86Operand>
1401X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Nirav Dave8601ac12016-08-02 17:56:03 +00001402 int64_t ImmDisp, bool isSymbol,
1403 unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001404 MCAsmParser &Parser = getParser();
Chad Rosier5362af92013-04-16 18:15:40 +00001405 const AsmToken &Tok = Parser.getTok();
1406 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1407 if (getLexer().isNot(AsmToken::LBrac))
1408 return ErrorOperand(BracLoc, "Expected '[' token!");
1409 Parser.Lex(); // Eat '['
1410
Nirav Davea6c75952016-07-14 17:37:05 +00001411 SMLoc StartInBrac = Parser.getTok().getLoc();
Chad Rosier5362af92013-04-16 18:15:40 +00001412 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1413 // may have already parsed an immediate displacement before the bracketed
1414 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001415 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001416 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001417 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +00001418
Craig Topper062a2ba2014-04-25 05:30:21 +00001419 const MCExpr *Disp = nullptr;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001420 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001421 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001422 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001423 if (isParsingInlineAsm())
Craig Topper7143d802015-10-10 05:25:06 +00001424 RewriteIntelBracExpression(*InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001425 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001426 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001427 }
1428
1429 if (SM.getImm() || !Disp) {
Jim Grosbach13760bd2015-05-30 01:25:56 +00001430 const MCExpr *Imm = MCConstantExpr::create(SM.getImm(), getContext());
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001431 if (Disp)
Jim Grosbach13760bd2015-05-30 01:25:56 +00001432 Disp = MCBinaryExpr::createAdd(Disp, Imm, getContext());
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001433 else
1434 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001435 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001436
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001437 // Parse struct field access. Intel requires a dot, but MSVC doesn't. MSVC
1438 // will in fact do global lookup the field name inside all global typedefs,
1439 // but we don't emulate that.
Nirav Davea6c75952016-07-14 17:37:05 +00001440 if ((Parser.getTok().getKind() == AsmToken::Identifier ||
1441 Parser.getTok().getKind() == AsmToken::Dot ||
1442 Parser.getTok().getKind() == AsmToken::Real) &&
1443 Parser.getTok().getString().find('.') != StringRef::npos) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001444 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001445 if (ParseIntelDotOperator(Disp, NewDisp))
Craig Topper062a2ba2014-04-25 05:30:21 +00001446 return nullptr;
Michael Liao5bf95782014-12-04 05:20:33 +00001447
Chad Rosier70f47592013-04-10 20:07:47 +00001448 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001449 Parser.Lex(); // Eat the field.
1450 Disp = NewDisp;
1451 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001452
Nirav Dave8601ac12016-08-02 17:56:03 +00001453 if (isSymbol) {
1454 if (SM.getSym()) {
1455 Error(Start, "cannot use more than one symbol in memory operand");
1456 return nullptr;
1457 }
1458 if (SM.getBaseReg()) {
1459 Error(Start, "cannot use base register with variable reference");
1460 return nullptr;
1461 }
1462 if (SM.getIndexReg()) {
1463 Error(Start, "cannot use index register with variable reference");
1464 return nullptr;
1465 }
1466 }
1467
Chad Rosier5c118fd2013-01-14 22:31:35 +00001468 int BaseReg = SM.getBaseReg();
1469 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001470 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001471 if (!isParsingInlineAsm()) {
1472 // handle [-42]
1473 if (!BaseReg && !IndexReg) {
1474 if (!SegReg)
Craig Topper055845f2015-01-02 07:02:25 +00001475 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size);
1476 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
1477 Start, End, Size);
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001478 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001479 StringRef ErrMsg;
1480 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1481 Error(StartInBrac, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001482 return nullptr;
Kevin Enderbybc570f22014-01-23 22:34:42 +00001483 }
Craig Topper055845f2015-01-02 07:02:25 +00001484 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1485 IndexReg, Scale, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001486 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001487
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001488 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001489 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001490 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001491}
1492
Chad Rosier8a244662013-04-02 20:02:33 +00001493// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001494bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1495 StringRef &Identifier,
1496 InlineAsmIdentifierInfo &Info,
1497 bool IsUnevaluatedOperand, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001498 MCAsmParser &Parser = getParser();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001499 assert(isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001500 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001501
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001502 StringRef LineBuf(Identifier.data());
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001503 void *Result =
1504 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001505
Chad Rosier8a244662013-04-02 20:02:33 +00001506 const AsmToken &Tok = Parser.getTok();
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001507 SMLoc Loc = Tok.getLoc();
John McCallf73981b2013-05-03 00:15:41 +00001508
1509 // Advance the token stream until the end of the current token is
1510 // after the end of what the frontend claimed.
1511 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001512 do {
John McCallf73981b2013-05-03 00:15:41 +00001513 End = Tok.getEndLoc();
1514 getLexer().Lex();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001515 } while (End.getPointer() < EndPtr);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001516 Identifier = LineBuf;
1517
Reid Klecknerc2b92542015-08-26 21:57:25 +00001518 // The frontend should end parsing on an assembler token boundary, unless it
1519 // failed parsing.
1520 assert((End.getPointer() == EndPtr || !Result) &&
1521 "frontend claimed part of a token?");
1522
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001523 // If the identifier lookup was unsuccessful, assume that we are dealing with
1524 // a label.
1525 if (!Result) {
Ehsan Akhgaribb6bb072014-09-22 20:40:36 +00001526 StringRef InternalName =
1527 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(),
1528 Loc, false);
1529 assert(InternalName.size() && "We should have an internal name here.");
1530 // Push a rewrite for replacing the identifier name with the internal name.
Craig Topper7d5b2312015-10-10 05:25:02 +00001531 InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(),
1532 InternalName);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001533 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001534
1535 // Create the symbol reference.
Jim Grosbach6f482002015-05-18 18:43:14 +00001536 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Chad Rosier8a244662013-04-02 20:02:33 +00001537 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001538 Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001539 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001540}
1541
David Majnemeraa34d792013-08-27 21:56:17 +00001542/// \brief Parse intel style segment override.
David Blaikie960ea3f2014-06-08 16:18:35 +00001543std::unique_ptr<X86Operand>
1544X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start,
1545 unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001546 MCAsmParser &Parser = getParser();
David Majnemeraa34d792013-08-27 21:56:17 +00001547 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1548 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1549 if (Tok.isNot(AsmToken::Colon))
1550 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1551 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001552
David Majnemeraa34d792013-08-27 21:56:17 +00001553 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001554 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001555 ImmDisp = Tok.getIntVal();
1556 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1557
Chad Rosier1530ba52013-03-27 21:49:56 +00001558 if (isParsingInlineAsm())
Craig Topper7d5b2312015-10-10 05:25:02 +00001559 InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, ImmDispToken.getLoc());
David Majnemeraa34d792013-08-27 21:56:17 +00001560
1561 if (getLexer().isNot(AsmToken::LBrac)) {
1562 // An immediate following a 'segment register', 'colon' token sequence can
1563 // be followed by a bracketed expression. If it isn't we know we have our
1564 // final segment override.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001565 const MCExpr *Disp = MCConstantExpr::create(ImmDisp, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001566 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp,
1567 /*BaseReg=*/0, /*IndexReg=*/0, /*Scale=*/1,
1568 Start, ImmDispToken.getEndLoc(), Size);
David Majnemeraa34d792013-08-27 21:56:17 +00001569 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001570 }
1571
Chad Rosier91c82662012-10-24 17:22:29 +00001572 if (getLexer().is(AsmToken::LBrac))
Nirav Dave8601ac12016-08-02 17:56:03 +00001573 return ParseIntelBracExpression(SegReg, Start, ImmDisp, false, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001574
David Majnemeraa34d792013-08-27 21:56:17 +00001575 const MCExpr *Val;
1576 SMLoc End;
1577 if (!isParsingInlineAsm()) {
1578 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001579 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001580
Craig Topper055845f2015-01-02 07:02:25 +00001581 return X86Operand::CreateMem(getPointerWidth(), Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001582 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001583
David Majnemeraa34d792013-08-27 21:56:17 +00001584 InlineAsmIdentifierInfo Info;
1585 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001586 if (ParseIntelIdentifier(Val, Identifier, Info,
1587 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001588 return nullptr;
David Majnemeraa34d792013-08-27 21:56:17 +00001589 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1590 /*Scale=*/1, Start, End, Size, Identifier, Info);
1591}
1592
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001593//ParseRoundingModeOp - Parse AVX-512 rounding mode operand
1594std::unique_ptr<X86Operand>
1595X86AsmParser::ParseRoundingModeOp(SMLoc Start, SMLoc End) {
1596 MCAsmParser &Parser = getParser();
1597 const AsmToken &Tok = Parser.getTok();
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001598 // Eat "{" and mark the current place.
1599 const SMLoc consumedToken = consumeToken();
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001600 if (Tok.getIdentifier().startswith("r")){
1601 int rndMode = StringSwitch<int>(Tok.getIdentifier())
1602 .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT)
1603 .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF)
1604 .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF)
1605 .Case("rz", X86::STATIC_ROUNDING::TO_ZERO)
1606 .Default(-1);
1607 if (-1 == rndMode)
1608 return ErrorOperand(Tok.getLoc(), "Invalid rounding mode.");
1609 Parser.Lex(); // Eat "r*" of r*-sae
1610 if (!getLexer().is(AsmToken::Minus))
1611 return ErrorOperand(Tok.getLoc(), "Expected - at this point");
1612 Parser.Lex(); // Eat "-"
1613 Parser.Lex(); // Eat the sae
1614 if (!getLexer().is(AsmToken::RCurly))
1615 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1616 Parser.Lex(); // Eat "}"
1617 const MCExpr *RndModeOp =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001618 MCConstantExpr::create(rndMode, Parser.getContext());
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001619 return X86Operand::CreateImm(RndModeOp, Start, End);
1620 }
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001621 if(Tok.getIdentifier().equals("sae")){
1622 Parser.Lex(); // Eat the sae
1623 if (!getLexer().is(AsmToken::RCurly))
1624 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1625 Parser.Lex(); // Eat "}"
1626 return X86Operand::CreateToken("{sae}", consumedToken);
1627 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001628 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
1629}
Chad Rosier91c82662012-10-24 17:22:29 +00001630
Chad Rosier5dcb4662012-10-24 22:21:50 +00001631/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001632bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001633 const MCExpr *&NewDisp) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001634 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001635 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001636 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001637
1638 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001639 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001640 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001641 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001642 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001643
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001644 // Drop the optional '.'.
1645 StringRef DotDispStr = Tok.getString();
1646 if (DotDispStr.startswith("."))
1647 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001648
Chad Rosier5dcb4662012-10-24 22:21:50 +00001649 // .Imm gets lexed as a real.
1650 if (Tok.is(AsmToken::Real)) {
1651 APInt DotDisp;
1652 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001653 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001654 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001655 unsigned DotDisp;
1656 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1657 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001658 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001659 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001660 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001661 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001662 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001663
Chad Rosier240b7b92012-10-25 21:51:10 +00001664 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1665 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1666 unsigned Len = DotDispStr.size();
1667 unsigned Val = OrigDispVal + DotDispVal;
Craig Topper7d5b2312015-10-10 05:25:02 +00001668 InstInfo->AsmRewrites->emplace_back(AOK_DotOperator, Loc, Len, Val);
Chad Rosier911c1f32012-10-25 17:37:43 +00001669 }
1670
Jim Grosbach13760bd2015-05-30 01:25:56 +00001671 NewDisp = MCConstantExpr::create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001672 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001673}
1674
Chad Rosier91c82662012-10-24 17:22:29 +00001675/// Parse the 'offset' operator. This operator is used to specify the
1676/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001677std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001678 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001679 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001680 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001681 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001682
Chad Rosier91c82662012-10-24 17:22:29 +00001683 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001684 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001685 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001686 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001687 if (ParseIntelIdentifier(Val, Identifier, Info,
1688 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001689 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001690
Chad Rosiere2f03772012-10-26 16:09:20 +00001691 // Don't emit the offset operator.
Craig Topper7d5b2312015-10-10 05:25:02 +00001692 InstInfo->AsmRewrites->emplace_back(AOK_Skip, OffsetOfLoc, 7);
Chad Rosiere2f03772012-10-26 16:09:20 +00001693
Chad Rosier91c82662012-10-24 17:22:29 +00001694 // The offset operator will have an 'r' constraint, thus we need to create
1695 // register operand to ensure proper matching. Just pick a GPR based on
1696 // the size of a pointer.
Nirav Dave6477ce22016-09-26 19:33:36 +00001697 bool Parse32 = is32BitMode() || Code16GCC;
1698 unsigned RegNo = is64BitMode() ? X86::RBX : (Parse32 ? X86::EBX : X86::BX);
1699
Chad Rosiera4bc9432013-01-10 22:10:27 +00001700 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Peter Collingbourne0da86302016-10-10 22:49:37 +00001701 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001702}
1703
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001704enum IntelOperatorKind {
1705 IOK_LENGTH,
1706 IOK_SIZE,
1707 IOK_TYPE
1708};
1709
1710/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1711/// returns the number of elements in an array. It returns the value 1 for
1712/// non-array variables. The SIZE operator returns the size of a C or C++
1713/// variable. A variable's size is the product of its LENGTH and TYPE. The
1714/// TYPE operator returns the size of a C or C++ type or variable. If the
1715/// variable is an array, TYPE returns the size of a single element.
David Blaikie960ea3f2014-06-08 16:18:35 +00001716std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001717 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001718 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001719 SMLoc TypeLoc = Tok.getLoc();
1720 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001721
Craig Topper062a2ba2014-04-25 05:30:21 +00001722 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001723 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001724 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001725 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001726 if (ParseIntelIdentifier(Val, Identifier, Info,
1727 /*Unevaluated=*/true, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001728 return nullptr;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001729
1730 if (!Info.OpDecl)
1731 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001732
Chad Rosierf6675c32013-04-22 17:01:46 +00001733 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001734 switch(OpKind) {
1735 default: llvm_unreachable("Unexpected operand kind!");
1736 case IOK_LENGTH: CVal = Info.Length; break;
1737 case IOK_SIZE: CVal = Info.Size; break;
1738 case IOK_TYPE: CVal = Info.Type; break;
1739 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001740
1741 // Rewrite the type operator and the C or C++ type or variable in terms of an
1742 // immediate. E.g. TYPE foo -> $$4
1743 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Craig Topper7d5b2312015-10-10 05:25:02 +00001744 InstInfo->AsmRewrites->emplace_back(AOK_Imm, TypeLoc, Len, CVal);
Chad Rosier11c42f22012-10-26 18:04:20 +00001745
Jim Grosbach13760bd2015-05-30 01:25:56 +00001746 const MCExpr *Imm = MCConstantExpr::create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001747 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001748}
1749
David Blaikie960ea3f2014-06-08 16:18:35 +00001750std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001751 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001752 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001753 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001754
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001755 // Offset, length, type and size operators.
1756 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001757 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001758 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001759 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001760 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001761 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001762 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001763 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001764 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001765 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001766 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001767
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001768 bool PtrInOperand = false;
David Majnemeraa34d792013-08-27 21:56:17 +00001769 unsigned Size = getIntelMemOperandSize(Tok.getString());
1770 if (Size) {
1771 Parser.Lex(); // Eat operand size (e.g., byte, word).
1772 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
Reid Kleckner71ff3f22014-08-01 00:59:22 +00001773 return ErrorOperand(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
David Majnemeraa34d792013-08-27 21:56:17 +00001774 Parser.Lex(); // Eat ptr.
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001775 PtrInOperand = true;
David Majnemeraa34d792013-08-27 21:56:17 +00001776 }
Nirav Dave8601ac12016-08-02 17:56:03 +00001777
David Majnemeraa34d792013-08-27 21:56:17 +00001778 Start = Tok.getLoc();
1779
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001780 // rounding mode token
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001781 if (getSTI().getFeatureBits()[X86::FeatureAVX512] &&
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001782 getLexer().is(AsmToken::LCurly))
1783 return ParseRoundingModeOp(Start, End);
1784
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001785 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001786 unsigned RegNo = 0;
Nirav Dave8601ac12016-08-02 17:56:03 +00001787 if (getLexer().is(AsmToken::Identifier) &&
1788 !ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001789 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001790 // of a segment override, otherwise this is a normal register reference.
Douglas Katzman0411e862016-10-05 15:23:35 +00001791 // In case it is a normal register and there is ptr in the operand this
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001792 // is an error
Douglas Katzman0411e862016-10-05 15:23:35 +00001793 if (RegNo == X86::RIP)
1794 return ErrorOperand(Start, "rip can only be used as a base register");
1795 if (getLexer().isNot(AsmToken::Colon)) {
1796 if (PtrInOperand) {
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001797 return ErrorOperand(Start, "expected memory operand after "
1798 "'ptr', found register operand instead");
1799 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001800 return X86Operand::CreateReg(RegNo, Start, End);
Marina Yatsina4b1aea02015-12-03 12:17:03 +00001801 }
David Majnemeraa34d792013-08-27 21:56:17 +00001802 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001803 }
1804
Nirav Dave8601ac12016-08-02 17:56:03 +00001805 // Immediates and Memory
1806
1807 // Parse [ BaseReg + Scale*IndexReg + Disp ].
1808 if (getLexer().is(AsmToken::LBrac))
1809 return ParseIntelBracExpression(/*SegReg=*/0, Start, /*ImmDisp=*/0, false,
1810 Size);
1811
1812 AsmToken StartTok = Tok;
1813 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1814 /*AddImmPrefix=*/false);
1815 if (ParseIntelExpression(SM, End))
1816 return nullptr;
1817
1818 bool isSymbol = SM.getSym() && SM.getSym()->getKind() != MCExpr::Constant;
1819 int64_t Imm = SM.getImm();
1820 if (SM.getSym() && SM.getSym()->getKind() == MCExpr::Constant)
1821 SM.getSym()->evaluateAsAbsolute(Imm);
1822
1823 if (StartTok.isNot(AsmToken::Identifier) &&
1824 StartTok.isNot(AsmToken::String) && isParsingInlineAsm()) {
1825 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1826 if (StartTok.getString().size() == Len)
1827 // Just add a prefix if this wasn't a complex immediate expression.
1828 InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, Start);
1829 else
1830 // Otherwise, rewrite the complex expression as a single immediate.
1831 InstInfo->AsmRewrites->emplace_back(AOK_Imm, Start, Len, Imm);
1832 }
1833
1834 if (getLexer().isNot(AsmToken::LBrac)) {
1835 // If a directional label (ie. 1f or 2b) was parsed above from
1836 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1837 // to the MCExpr with the directional local symbol and this is a
1838 // memory operand not an immediate operand.
1839 if (isSymbol) {
1840 if (isParsingInlineAsm())
1841 return CreateMemForInlineAsm(/*SegReg=*/0, SM.getSym(), /*BaseReg=*/0,
1842 /*IndexReg=*/0,
1843 /*Scale=*/1, Start, End, Size,
1844 SM.getSymName(), SM.getIdentifierInfo());
1845 return X86Operand::CreateMem(getPointerWidth(), SM.getSym(), Start, End,
1846 Size);
1847 }
1848
1849 const MCExpr *ImmExpr = MCConstantExpr::create(Imm, getContext());
1850 return X86Operand::CreateImm(ImmExpr, Start, End);
1851 }
1852
1853 // Only positive immediates are valid.
1854 if (Imm < 0)
1855 return ErrorOperand(Start, "expected a positive immediate displacement "
1856 "before bracketed expr.");
1857
1858 return ParseIntelBracExpression(/*SegReg=*/0, Start, Imm, isSymbol, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001859}
1860
David Blaikie960ea3f2014-06-08 16:18:35 +00001861std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001862 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001863 switch (getLexer().getKind()) {
1864 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001865 // Parse a memory operand with no segment register.
1866 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001867 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001868 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001869 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001870 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001871 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001872 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001873 Error(Start, "%eiz and %riz can only be used as index registers",
1874 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001875 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001876 }
Douglas Katzman0411e862016-10-05 15:23:35 +00001877 if (RegNo == X86::RIP) {
1878 Error(Start, "%rip can only be used as a base register",
1879 SMRange(Start, End));
1880 return nullptr;
1881 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001882
Chris Lattnerb9270732010-04-17 18:56:34 +00001883 // If this is a segment register followed by a ':', then this is the start
1884 // of a memory reference, otherwise this is a normal register reference.
1885 if (getLexer().isNot(AsmToken::Colon))
1886 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001887
Reid Kleckner0c5da972014-07-31 23:03:22 +00001888 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1889 return ErrorOperand(Start, "invalid segment register");
1890
Chris Lattnerb9270732010-04-17 18:56:34 +00001891 getParser().Lex(); // Eat the colon.
1892 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001893 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001894 case AsmToken::Dollar: {
1895 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001896 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001897 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001898 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001899 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001900 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001901 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001902 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001903 case AsmToken::LCurly:{
1904 SMLoc Start = Parser.getTok().getLoc(), End;
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001905 if (getSTI().getFeatureBits()[X86::FeatureAVX512])
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001906 return ParseRoundingModeOp(Start, End);
Nirav Dave8601ac12016-08-02 17:56:03 +00001907 return ErrorOperand(Start, "Unexpected '{' in expression");
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001908 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001909 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001910}
1911
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001912// true on failure, false otherwise
1913// If no {z} mark was found - Parser doesn't advance
1914bool X86AsmParser::ParseZ(std::unique_ptr<X86Operand> &Z,
1915 const SMLoc &StartLoc) {
1916 MCAsmParser &Parser = getParser();
1917 // Assuming we are just pass the '{' mark, quering the next token
1918 // Searched for {z}, but none was found. Return true, as no parsing error was
1919 // encountered
1920 if (!(getLexer().is(AsmToken::Identifier) &&
1921 (getLexer().getTok().getIdentifier() == "z")))
1922 return false;
1923 Parser.Lex(); // Eat z
1924 // Query and eat the '}' mark
1925 if (!getLexer().is(AsmToken::RCurly))
1926 return Error(getLexer().getLoc(), "Expected } at this point");
1927 Parser.Lex(); // Eat '}'
1928 // Assign Z with the {z} mark opernad
1929 Z.reset(X86Operand::CreateToken("{z}", StartLoc).release());
1930 return false;
1931}
1932
1933// true on failure, false otherwise
David Blaikie960ea3f2014-06-08 16:18:35 +00001934bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1935 const MCParsedAsmOperand &Op) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001936 MCAsmParser &Parser = getParser();
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001937 if(getSTI().getFeatureBits()[X86::FeatureAVX512]) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001938 if (getLexer().is(AsmToken::LCurly)) {
1939 // Eat "{" and mark the current place.
1940 const SMLoc consumedToken = consumeToken();
1941 // Distinguish {1to<NUM>} from {%k<NUM>}.
1942 if(getLexer().is(AsmToken::Integer)) {
1943 // Parse memory broadcasting ({1to<NUM>}).
1944 if (getLexer().getTok().getIntVal() != 1)
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001945 return TokError("Expected 1to<NUM> at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001946 Parser.Lex(); // Eat "1" of 1to8
1947 if (!getLexer().is(AsmToken::Identifier) ||
1948 !getLexer().getTok().getIdentifier().startswith("to"))
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001949 return TokError("Expected 1to<NUM> at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001950 // Recognize only reasonable suffixes.
1951 const char *BroadcastPrimitive =
1952 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
Robert Khasanovbfa01312014-07-21 14:54:21 +00001953 .Case("to2", "{1to2}")
1954 .Case("to4", "{1to4}")
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001955 .Case("to8", "{1to8}")
1956 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001957 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001958 if (!BroadcastPrimitive)
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001959 return TokError("Invalid memory broadcast primitive.");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001960 Parser.Lex(); // Eat "toN" of 1toN
1961 if (!getLexer().is(AsmToken::RCurly))
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001962 return TokError("Expected } at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001963 Parser.Lex(); // Eat "}"
1964 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1965 consumedToken));
1966 // No AVX512 specific primitives can pass
1967 // after memory broadcasting, so return.
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001968 return false;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001969 } else {
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001970 // Parse either {k}{z}, {z}{k}, {k} or {z}
1971 // last one have no meaning, but GCC accepts it
1972 // Currently, we're just pass a '{' mark
1973 std::unique_ptr<X86Operand> Z;
1974 if (ParseZ(Z, consumedToken))
1975 return true;
1976 // Reaching here means that parsing of the allegadly '{z}' mark yielded
1977 // no errors.
1978 // Query for the need of further parsing for a {%k<NUM>} mark
1979 if (!Z || getLexer().is(AsmToken::LCurly)) {
1980 const SMLoc StartLoc = Z ? consumeToken() : consumedToken;
1981 // Parse an op-mask register mark ({%k<NUM>}), which is now to be
1982 // expected
1983 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001984 if (!getLexer().is(AsmToken::RCurly))
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001985 return Error(getLexer().getLoc(), "Expected } at this point");
1986 Operands.push_back(X86Operand::CreateToken("{", StartLoc));
1987 Operands.push_back(std::move(Op));
1988 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1989 } else
1990 return Error(getLexer().getLoc(),
1991 "Expected an op-mask register at this point");
1992 // {%k<NUM>} mark is found, inquire for {z}
1993 if (getLexer().is(AsmToken::LCurly) && !Z) {
1994 // Have we've found a parsing error, or found no (expected) {z} mark
1995 // - report an error
1996 if (ParseZ(Z, consumeToken()) || !Z)
1997 return true;
1998
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001999 }
Michael Zuckerman1bee6342016-10-18 13:52:39 +00002000 // '{z}' on its own is meaningless, hence should be ignored.
2001 // on the contrary - have it been accompanied by a K register,
2002 // allow it.
2003 if (Z)
2004 Operands.push_back(std::move(Z));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002005 }
2006 }
2007 }
2008 }
Michael Zuckerman1bee6342016-10-18 13:52:39 +00002009 return false;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002010}
2011
Chris Lattnerb9270732010-04-17 18:56:34 +00002012/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
2013/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00002014std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
2015 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002016
Rafael Espindola961d4692014-11-11 05:18:41 +00002017 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002018 // We have to disambiguate a parenthesized expression "(4+5)" from the start
2019 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00002020 // only way to do this without lookahead is to eat the '(' and see what is
2021 // after it.
Jim Grosbach13760bd2015-05-30 01:25:56 +00002022 const MCExpr *Disp = MCConstantExpr::create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002023 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00002024 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00002025 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002026
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002027 // After parsing the base expression we could either have a parenthesized
2028 // memory address or not. If not, return now. If so, eat the (.
2029 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002030 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002031 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002032 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, ExprEnd);
2033 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2034 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002035 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002036
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002037 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002038 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002039 } else {
2040 // Okay, we have a '('. We don't know if this is an expression or not, but
2041 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00002042 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002043 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002044
Kevin Enderby7d912182009-09-03 17:15:07 +00002045 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002046 // Nothing to do here, fall into the code below with the '(' part of the
2047 // memory operand consumed.
2048 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00002049 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002050
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002051 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002052 if (getParser().parseParenExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00002053 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002054
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002055 // After parsing the base expression we could either have a parenthesized
2056 // memory address or not. If not, return now. If so, eat the (.
2057 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002058 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002059 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002060 return X86Operand::CreateMem(getPointerWidth(), Disp, LParenLoc,
2061 ExprEnd);
2062 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2063 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002064 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002065
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002066 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002067 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002068 }
2069 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002070
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002071 // If we reached here, then we just ate the ( of the memory operand. Process
2072 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00002073 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00002074 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002075
Chris Lattner0c2538f2010-01-15 18:51:29 +00002076 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002077 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00002078 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00002079 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002080 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002081 Error(StartLoc, "eiz and riz can only be used as index registers",
2082 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00002083 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002084 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00002085 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002086
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002087 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002088 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002089 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002090
2091 // Following the comma we should have either an index register, or a scale
2092 // value. We don't support the later form, but we want to parse it
2093 // correctly.
2094 //
2095 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002096 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00002097 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00002098 SMLoc L;
Douglas Katzman0411e862016-10-05 15:23:35 +00002099 if (ParseRegister(IndexReg, L, L))
2100 return nullptr;
2101 if (BaseReg == X86::RIP) {
2102 Error(IndexLoc, "%rip as base register can not have an index register");
2103 return nullptr;
2104 }
2105 if (IndexReg == X86::RIP) {
2106 Error(IndexLoc, "%rip is not allowed as an index register");
2107 return nullptr;
2108 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002109
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002110 if (getLexer().isNot(AsmToken::RParen)) {
2111 // Parse the scale amount:
2112 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002113 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002114 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002115 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002116 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002117 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00002118 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002119
2120 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002121 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002122
2123 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002124 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00002125 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002126 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00002127 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002128
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002129 // Validate the scale amount.
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002130 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
David Woodhouse6dbda442014-01-08 12:58:28 +00002131 ScaleVal != 1) {
2132 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00002133 return nullptr;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002134 }
2135 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 &&
2136 ScaleVal != 8) {
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002137 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00002138 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002139 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002140 Scale = (unsigned)ScaleVal;
2141 }
2142 }
2143 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002144 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002145 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00002146 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002147
2148 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002149 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00002150 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002151
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002152 if (Value != 1)
2153 Warning(Loc, "scale factor without index register is ignored");
2154 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002155 }
2156 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002157
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002158 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002159 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002160 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00002161 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002162 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002163 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002164 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002165
David Woodhouse6dbda442014-01-08 12:58:28 +00002166 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
2167 // and then only in non-64-bit modes. Except for DX, which is a special case
2168 // because an unofficial form of in/out instructions uses it.
2169 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
2170 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
2171 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2172 BaseReg != X86::DX) {
2173 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002174 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002175 }
2176 if (BaseReg == 0 &&
2177 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2178 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002179 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002180 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00002181
2182 StringRef ErrMsg;
2183 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
2184 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00002185 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002186 }
2187
Reid Klecknerb7e2f602014-07-31 23:26:35 +00002188 if (SegReg || BaseReg || IndexReg)
Craig Topper055845f2015-01-02 07:02:25 +00002189 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
2190 IndexReg, Scale, MemStart, MemEnd);
2191 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002192}
2193
David Blaikie960ea3f2014-06-08 16:18:35 +00002194bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
2195 SMLoc NameLoc, OperandVector &Operands) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002196 MCAsmParser &Parser = getParser();
Chad Rosierf0e87202012-10-25 20:41:34 +00002197 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002198 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002199
Michael Zuckerman174d2e72016-10-14 08:09:40 +00002200 if (Name == "jmp" && isParsingIntelSyntax() && isParsingInlineAsm()) {
2201 StringRef NextTok = Parser.getTok().getString();
2202 if (NextTok == "short") {
2203 SMLoc NameEndLoc =
2204 NameLoc.getFromPointer(NameLoc.getPointer() + Name.size());
2205 // Eat the short keyword
2206 Parser.Lex();
2207 // MS ignores the short keyword, it determines the jmp type based
2208 // on the distance of the label
2209 InstInfo->AsmRewrites->emplace_back(AOK_Skip, NameEndLoc,
2210 NextTok.size() + 1);
2211 }
2212 }
2213
Chris Lattner7e8a99b2010-11-28 20:23:50 +00002214 // FIXME: Hack to recognize setneb as setne.
2215 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
2216 PatchedName != "setb" && PatchedName != "setnb")
2217 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00002218
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002219 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002220 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002221 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
2222 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00002223 bool IsVCMP = PatchedName[0] == 'v';
Craig Topper78c424d2015-02-15 07:13:48 +00002224 unsigned CCIdx = IsVCMP ? 4 : 3;
2225 unsigned ComparisonCode = StringSwitch<unsigned>(
2226 PatchedName.slice(CCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00002227 .Case("eq", 0x00)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002228 .Case("eq_oq", 0x00)
Craig Toppera0a603e2012-03-29 07:11:23 +00002229 .Case("lt", 0x01)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002230 .Case("lt_os", 0x01)
Craig Toppera0a603e2012-03-29 07:11:23 +00002231 .Case("le", 0x02)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002232 .Case("le_os", 0x02)
Craig Toppera0a603e2012-03-29 07:11:23 +00002233 .Case("unord", 0x03)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002234 .Case("unord_q", 0x03)
Craig Toppera0a603e2012-03-29 07:11:23 +00002235 .Case("neq", 0x04)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002236 .Case("neq_uq", 0x04)
Craig Toppera0a603e2012-03-29 07:11:23 +00002237 .Case("nlt", 0x05)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002238 .Case("nlt_us", 0x05)
Craig Toppera0a603e2012-03-29 07:11:23 +00002239 .Case("nle", 0x06)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002240 .Case("nle_us", 0x06)
Craig Toppera0a603e2012-03-29 07:11:23 +00002241 .Case("ord", 0x07)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002242 .Case("ord_q", 0x07)
Craig Toppera0a603e2012-03-29 07:11:23 +00002243 /* AVX only from here */
2244 .Case("eq_uq", 0x08)
2245 .Case("nge", 0x09)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002246 .Case("nge_us", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002247 .Case("ngt", 0x0A)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002248 .Case("ngt_us", 0x0A)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002249 .Case("false", 0x0B)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002250 .Case("false_oq", 0x0B)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002251 .Case("neq_oq", 0x0C)
2252 .Case("ge", 0x0D)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002253 .Case("ge_os", 0x0D)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002254 .Case("gt", 0x0E)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002255 .Case("gt_os", 0x0E)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002256 .Case("true", 0x0F)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002257 .Case("true_uq", 0x0F)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002258 .Case("eq_os", 0x10)
2259 .Case("lt_oq", 0x11)
2260 .Case("le_oq", 0x12)
2261 .Case("unord_s", 0x13)
2262 .Case("neq_us", 0x14)
2263 .Case("nlt_uq", 0x15)
2264 .Case("nle_uq", 0x16)
2265 .Case("ord_s", 0x17)
2266 .Case("eq_us", 0x18)
2267 .Case("nge_uq", 0x19)
2268 .Case("ngt_uq", 0x1A)
2269 .Case("false_os", 0x1B)
2270 .Case("neq_os", 0x1C)
2271 .Case("ge_oq", 0x1D)
2272 .Case("gt_oq", 0x1E)
2273 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002274 .Default(~0U);
Craig Topper78c424d2015-02-15 07:13:48 +00002275 if (ComparisonCode != ~0U && (IsVCMP || ComparisonCode < 8)) {
Craig Topper43860832015-02-14 21:54:03 +00002276
Craig Topper78c424d2015-02-15 07:13:48 +00002277 Operands.push_back(X86Operand::CreateToken(PatchedName.slice(0, CCIdx),
Craig Topper43860832015-02-14 21:54:03 +00002278 NameLoc));
2279
Jim Grosbach13760bd2015-05-30 01:25:56 +00002280 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper43860832015-02-14 21:54:03 +00002281 getParser().getContext());
2282 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2283
2284 PatchedName = PatchedName.substr(PatchedName.size() - 2);
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002285 }
2286 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002287
Craig Topper78c424d2015-02-15 07:13:48 +00002288 // FIXME: Hack to recognize vpcmp<comparison code>{ub,uw,ud,uq,b,w,d,q}.
2289 if (PatchedName.startswith("vpcmp") &&
2290 (PatchedName.endswith("b") || PatchedName.endswith("w") ||
2291 PatchedName.endswith("d") || PatchedName.endswith("q"))) {
2292 unsigned CCIdx = PatchedName.drop_back().back() == 'u' ? 2 : 1;
2293 unsigned ComparisonCode = StringSwitch<unsigned>(
2294 PatchedName.slice(5, PatchedName.size() - CCIdx))
2295 .Case("eq", 0x0) // Only allowed on unsigned. Checked below.
2296 .Case("lt", 0x1)
2297 .Case("le", 0x2)
2298 //.Case("false", 0x3) // Not a documented alias.
2299 .Case("neq", 0x4)
2300 .Case("nlt", 0x5)
2301 .Case("nle", 0x6)
2302 //.Case("true", 0x7) // Not a documented alias.
2303 .Default(~0U);
2304 if (ComparisonCode != ~0U && (ComparisonCode != 0 || CCIdx == 2)) {
2305 Operands.push_back(X86Operand::CreateToken("vpcmp", NameLoc));
2306
Jim Grosbach13760bd2015-05-30 01:25:56 +00002307 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper78c424d2015-02-15 07:13:48 +00002308 getParser().getContext());
2309 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2310
2311 PatchedName = PatchedName.substr(PatchedName.size() - CCIdx);
2312 }
2313 }
2314
Craig Topper916708f2015-02-13 07:42:25 +00002315 // FIXME: Hack to recognize vpcom<comparison code>{ub,uw,ud,uq,b,w,d,q}.
2316 if (PatchedName.startswith("vpcom") &&
2317 (PatchedName.endswith("b") || PatchedName.endswith("w") ||
2318 PatchedName.endswith("d") || PatchedName.endswith("q"))) {
Craig Topper78c424d2015-02-15 07:13:48 +00002319 unsigned CCIdx = PatchedName.drop_back().back() == 'u' ? 2 : 1;
2320 unsigned ComparisonCode = StringSwitch<unsigned>(
2321 PatchedName.slice(5, PatchedName.size() - CCIdx))
Craig Topper916708f2015-02-13 07:42:25 +00002322 .Case("lt", 0x0)
2323 .Case("le", 0x1)
2324 .Case("gt", 0x2)
2325 .Case("ge", 0x3)
2326 .Case("eq", 0x4)
2327 .Case("neq", 0x5)
2328 .Case("false", 0x6)
2329 .Case("true", 0x7)
2330 .Default(~0U);
Craig Topper78c424d2015-02-15 07:13:48 +00002331 if (ComparisonCode != ~0U) {
Craig Topper916708f2015-02-13 07:42:25 +00002332 Operands.push_back(X86Operand::CreateToken("vpcom", NameLoc));
2333
Jim Grosbach13760bd2015-05-30 01:25:56 +00002334 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper916708f2015-02-13 07:42:25 +00002335 getParser().getContext());
2336 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2337
Craig Topper78c424d2015-02-15 07:13:48 +00002338 PatchedName = PatchedName.substr(PatchedName.size() - CCIdx);
Craig Topper916708f2015-02-13 07:42:25 +00002339 }
2340 }
2341
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002342 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002343
Chris Lattner086a83a2010-09-08 05:17:37 +00002344 // Determine whether this is an instruction prefix.
2345 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00002346 Name == "lock" || Name == "rep" ||
2347 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00002348 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00002349 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00002350
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002351 bool CurlyAsEndOfStatement = false;
Chris Lattner086a83a2010-09-08 05:17:37 +00002352 // This does the actual operand parsing. Don't parse any more if we have a
2353 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2354 // just want to parse the "lock" as the first instruction and the "incl" as
2355 // the next one.
2356 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002357
2358 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002359 if (getLexer().is(AsmToken::Star))
2360 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002361
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002362 // Read the operands.
2363 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002364 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
2365 Operands.push_back(std::move(Op));
Michael Zuckerman1bee6342016-10-18 13:52:39 +00002366 if (HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00002367 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002368 } else {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002369 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00002370 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002371 // check for comma and eat it
2372 if (getLexer().is(AsmToken::Comma))
2373 Parser.Lex();
2374 else
2375 break;
2376 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00002377
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002378 // In MS inline asm curly braces mark the begining/end of a block, therefore
2379 // they should be interepreted as end of statement
2380 CurlyAsEndOfStatement =
2381 isParsingIntelSyntax() && isParsingInlineAsm() &&
2382 (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly));
2383 if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement)
Nirav Dave2364748a2016-09-16 18:30:20 +00002384 return TokError("unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002385 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002386
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002387 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002388 if (getLexer().is(AsmToken::EndOfStatement) ||
2389 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002390 Parser.Lex();
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002391 else if (CurlyAsEndOfStatement)
2392 // Add an actual EndOfStatement before the curly brace
2393 Info.AsmRewrites->emplace_back(AOK_EndOfStatement,
2394 getLexer().getTok().getLoc(), 0);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002395
Michael Zuckermanfd3fe9e2015-11-12 16:58:51 +00002396 // This is for gas compatibility and cannot be done in td.
2397 // Adding "p" for some floating point with no argument.
2398 // For example: fsub --> fsubp
2399 bool IsFp =
2400 Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr";
2401 if (IsFp && Operands.size() == 1) {
2402 const char *Repl = StringSwitch<const char *>(Name)
2403 .Case("fsub", "fsubp")
2404 .Case("fdiv", "fdivp")
2405 .Case("fsubr", "fsubrp")
2406 .Case("fdivr", "fdivrp");
2407 static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl);
2408 }
2409
Nirav Davef45fd2b2016-08-08 18:01:04 +00002410 // Moving a 32 or 16 bit value into a segment register has the same
2411 // behavior. Modify such instructions to always take shorter form.
2412 if ((Name == "mov" || Name == "movw" || Name == "movl") &&
2413 (Operands.size() == 3)) {
2414 X86Operand &Op1 = (X86Operand &)*Operands[1];
2415 X86Operand &Op2 = (X86Operand &)*Operands[2];
2416 SMLoc Loc = Op1.getEndLoc();
2417 if (Op1.isReg() && Op2.isReg() &&
2418 X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(
2419 Op2.getReg()) &&
2420 (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) ||
2421 X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) {
2422 // Change instruction name to match new instruction.
2423 if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) {
2424 Name = is16BitMode() ? "movw" : "movl";
2425 Operands[0] = X86Operand::CreateToken(Name, NameLoc);
2426 }
2427 // Select the correct equivalent 16-/32-bit source register.
2428 unsigned Reg =
2429 getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32);
2430 Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc);
2431 }
2432 }
2433
Nirav Dave8e103802016-06-29 19:54:27 +00002434 // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" ->
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002435 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2436 // documented form in various unofficial manuals, so a lot of code uses it.
Nirav Dave8e103802016-06-29 19:54:27 +00002437 if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" ||
2438 Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") &&
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002439 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002440 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002441 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2442 isa<MCConstantExpr>(Op.Mem.Disp) &&
2443 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2444 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2445 SMLoc Loc = Op.getEndLoc();
2446 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002447 }
2448 }
Nirav Dave8e103802016-06-29 19:54:27 +00002449 // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al".
2450 if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" ||
2451 Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") &&
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002452 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002453 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002454 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2455 isa<MCConstantExpr>(Op.Mem.Disp) &&
2456 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2457 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2458 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002459 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002460 }
2461 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002462
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002463 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands;
2464 bool HadVerifyError = false;
2465
David Woodhouse4ce66062014-01-22 15:08:55 +00002466 // Append default arguments to "ins[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002467 if (Name.startswith("ins") &&
2468 (Operands.size() == 1 || Operands.size() == 3) &&
2469 (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" ||
2470 Name == "ins")) {
2471
2472 AddDefaultSrcDestOperands(TmpOperands,
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002473 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc),
2474 DefaultMemDIOperand(NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002475 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002476 }
2477
David Woodhousec472b812014-01-22 15:08:49 +00002478 // Append default arguments to "outs[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002479 if (Name.startswith("outs") &&
2480 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhousec472b812014-01-22 15:08:49 +00002481 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002482 Name == "outsd" || Name == "outs")) {
2483 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002484 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002485 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002486 }
2487
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002488 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2489 // values of $SIREG according to the mode. It would be nice if this
2490 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002491 if (Name.startswith("lods") &&
2492 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002493 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002494 Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) {
2495 TmpOperands.push_back(DefaultMemSIOperand(NameLoc));
2496 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2497 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002498
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002499 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2500 // values of $DIREG according to the mode. It would be nice if this
2501 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002502 if (Name.startswith("stos") &&
2503 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002504 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002505 Name == "stosl" || Name == "stosd" || Name == "stosq")) {
2506 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2507 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2508 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002509
David Woodhouse20fe4802014-01-22 15:08:27 +00002510 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2511 // values of $DIREG according to the mode. It would be nice if this
2512 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002513 if (Name.startswith("scas") &&
2514 (Operands.size() == 1 || Operands.size() == 2) &&
David Woodhouse20fe4802014-01-22 15:08:27 +00002515 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002516 Name == "scasl" || Name == "scasd" || Name == "scasq")) {
2517 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2518 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2519 }
David Woodhouse20fe4802014-01-22 15:08:27 +00002520
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002521 // Add default SI and DI operands to "cmps[bwlq]".
2522 if (Name.startswith("cmps") &&
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002523 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002524 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2525 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002526 AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc),
2527 DefaultMemSIOperand(NameLoc));
2528 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002529 }
2530
David Woodhouse6f417de2014-01-22 15:08:42 +00002531 // Add default SI and DI operands to "movs[bwlq]".
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002532 if (((Name.startswith("movs") &&
2533 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2534 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2535 (Name.startswith("smov") &&
2536 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2537 Name == "smovl" || Name == "smovd" || Name == "smovq"))) &&
2538 (Operands.size() == 1 || Operands.size() == 3)) {
2539 if (Name == "movsd" && Operands.size() == 1)
2540 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2541 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
2542 DefaultMemDIOperand(NameLoc));
2543 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2544 }
2545
2546 // Check if we encountered an error for one the string insturctions
2547 if (HadVerifyError) {
2548 return HadVerifyError;
David Woodhouse6f417de2014-01-22 15:08:42 +00002549 }
2550
Chris Lattner4bd21712010-09-15 04:33:27 +00002551 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002552 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002553 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002554 Name.startswith("shl") || Name.startswith("sal") ||
2555 Name.startswith("rcl") || Name.startswith("rcr") ||
2556 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002557 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002558 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002559 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002560 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2561 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2562 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002563 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002564 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002565 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2566 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2567 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002568 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002569 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002570 }
Chad Rosier51afe632012-06-27 22:34:28 +00002571
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002572 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2573 // instalias with an immediate operand yet.
2574 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002575 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
Duncan P. N. Exon Smithd5313222015-07-23 19:27:07 +00002576 if (Op1.isImm())
2577 if (auto *CE = dyn_cast<MCConstantExpr>(Op1.getImm()))
2578 if (CE->getValue() == 3) {
2579 Operands.erase(Operands.begin() + 1);
2580 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
2581 }
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002582 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002583
Marina Yatsinad9658d12016-01-19 16:35:38 +00002584 // Transforms "xlat mem8" into "xlatb"
2585 if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) {
2586 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2587 if (Op1.isMem8()) {
2588 Warning(Op1.getStartLoc(), "memory operand is only for determining the "
2589 "size, (R|E)BX will be used for the location");
2590 Operands.pop_back();
2591 static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb");
2592 }
2593 }
2594
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002595 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002596}
2597
David Blaikie960ea3f2014-06-08 16:18:35 +00002598bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Aaron Ballmana81264b2016-05-23 15:52:59 +00002599 return false;
Devang Patelde47cce2012-01-18 22:42:29 +00002600}
2601
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002602static const char *getSubtargetFeatureName(uint64_t Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002603
David Blaikie960ea3f2014-06-08 16:18:35 +00002604void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2605 MCStreamer &Out) {
Evgeniy Stepanov77ad8662014-07-31 09:11:04 +00002606 Instrumentation->InstrumentAndEmitInstruction(Inst, Operands, getContext(),
2607 MII, Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002608}
2609
David Blaikie960ea3f2014-06-08 16:18:35 +00002610bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2611 OperandVector &Operands,
Tim Northover26bb14e2014-08-18 11:49:42 +00002612 MCStreamer &Out, uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00002613 bool MatchingInlineAsm) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002614 if (isParsingIntelSyntax())
2615 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002616 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002617 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002618 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002619}
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002620
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002621void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
2622 OperandVector &Operands, MCStreamer &Out,
2623 bool MatchingInlineAsm) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002624 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002625 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002626 // call.
Reid Klecknerb1f2d2f2014-07-31 00:07:33 +00002627 const char *Repl = StringSwitch<const char *>(Op.getToken())
2628 .Case("finit", "fninit")
2629 .Case("fsave", "fnsave")
2630 .Case("fstcw", "fnstcw")
2631 .Case("fstcww", "fnstcw")
2632 .Case("fstenv", "fnstenv")
2633 .Case("fstsw", "fnstsw")
2634 .Case("fstsww", "fnstsw")
2635 .Case("fclex", "fnclex")
2636 .Default(nullptr);
2637 if (Repl) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002638 MCInst Inst;
2639 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002640 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002641 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002642 EmitInstruction(Inst, Operands, Out);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002643 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002644 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002645}
2646
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002647bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002648 bool MatchingInlineAsm) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002649 assert(ErrorInfo && "Unknown missing feature!");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002650 SmallString<126> Msg;
2651 raw_svector_ostream OS(Msg);
2652 OS << "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002653 uint64_t Mask = 1;
2654 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2655 if (ErrorInfo & Mask)
2656 OS << ' ' << getSubtargetFeatureName(ErrorInfo & Mask);
2657 Mask <<= 1;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002658 }
Nirav Dave2364748a2016-09-16 18:30:20 +00002659 return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002660}
2661
2662bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
2663 OperandVector &Operands,
2664 MCStreamer &Out,
2665 uint64_t &ErrorInfo,
2666 bool MatchingInlineAsm) {
2667 assert(!Operands.empty() && "Unexpect empty operand list!");
2668 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2669 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Nirav Dave2364748a2016-09-16 18:30:20 +00002670 SMRange EmptyRange = None;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002671
2672 // First, handle aliases that expand to multiple instructions.
2673 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002674
Chris Lattner628fbec2010-09-06 21:54:15 +00002675 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002676 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002677
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002678 // First, try a direct match.
Nirav Dave6477ce22016-09-26 19:33:36 +00002679 switch (MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
2680 isParsingIntelSyntax())) {
Craig Topper589ceee2015-01-03 08:16:34 +00002681 default: llvm_unreachable("Unexpected match result!");
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002682 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002683 // Some instructions need post-processing to, for example, tweak which
2684 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002685 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002686 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002687 while (processInstruction(Inst, Operands))
2688 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002689
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002690 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002691 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002692 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002693 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002694 return false;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002695 case Match_MissingFeature:
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002696 return ErrorMissingFeature(IDLoc, ErrorInfo, MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002697 case Match_InvalidOperand:
2698 WasOriginallyInvalidOperand = true;
2699 break;
2700 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002701 break;
2702 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002703
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002704 // FIXME: Ideally, we would only attempt suffix matches for things which are
2705 // valid prefixes, and we could just infer the right unambiguous
2706 // type. However, that requires substantially more matcher support than the
2707 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002708
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002709 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002710 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002711 SmallString<16> Tmp;
2712 Tmp += Base;
2713 Tmp += ' ';
Yaron Keren075759a2015-03-30 15:42:36 +00002714 Op.setTokenValue(Tmp);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002715
Chris Lattnerfab94132010-11-06 18:28:02 +00002716 // If this instruction starts with an 'f', then it is a floating point stack
2717 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2718 // 80-bit floating point, which use the suffixes s,l,t respectively.
2719 //
2720 // Otherwise, we assume that this may be an integer instruction, which comes
2721 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2722 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002723
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002724 // Check for the various suffix matches.
Tim Northover26bb14e2014-08-18 11:49:42 +00002725 uint64_t ErrorInfoIgnore;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002726 uint64_t ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002727 unsigned Match[4];
Chad Rosier51afe632012-06-27 22:34:28 +00002728
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002729 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
2730 Tmp.back() = Suffixes[I];
Nirav Dave6477ce22016-09-26 19:33:36 +00002731 Match[I] = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2732 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002733 // If this returned as a missing feature failure, remember that.
2734 if (Match[I] == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002735 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002736 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002737
2738 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002739 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002740
2741 // If exactly one matched, then we treat that as a successful match (and the
2742 // instruction will already have been filled in correctly, since the failing
2743 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002744 unsigned NumSuccessfulMatches =
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002745 std::count(std::begin(Match), std::end(Match), Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002746 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002747 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002748 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002749 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002750 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002751 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002752 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002753
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002754 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002755
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002756 // If we had multiple suffix matches, then identify this as an ambiguous
2757 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002758 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002759 char MatchChars[4];
2760 unsigned NumMatches = 0;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002761 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
2762 if (Match[I] == Match_Success)
2763 MatchChars[NumMatches++] = Suffixes[I];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002764
Alp Tokere69170a2014-06-26 22:52:05 +00002765 SmallString<126> Msg;
2766 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002767 OS << "ambiguous instructions require an explicit suffix (could be ";
2768 for (unsigned i = 0; i != NumMatches; ++i) {
2769 if (i != 0)
2770 OS << ", ";
2771 if (i + 1 == NumMatches)
2772 OS << "or ";
2773 OS << "'" << Base << MatchChars[i] << "'";
2774 }
2775 OS << ")";
Nirav Dave2364748a2016-09-16 18:30:20 +00002776 Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002777 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002778 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002779
Chris Lattner628fbec2010-09-06 21:54:15 +00002780 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002781
Chris Lattner628fbec2010-09-06 21:54:15 +00002782 // If all of the instructions reported an invalid mnemonic, then the original
2783 // mnemonic was invalid.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002784 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002785 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002786 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002787 Op.getLocRange(), MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002788 }
2789
2790 // Recover location info for the operand if we know which was the problem.
Tim Northover26bb14e2014-08-18 11:49:42 +00002791 if (ErrorInfo != ~0ULL) {
Chad Rosier49963552012-10-13 00:26:04 +00002792 if (ErrorInfo >= Operands.size())
Nirav Dave2364748a2016-09-16 18:30:20 +00002793 return Error(IDLoc, "too few operands for instruction", EmptyRange,
2794 MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002795
David Blaikie960ea3f2014-06-08 16:18:35 +00002796 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2797 if (Operand.getStartLoc().isValid()) {
2798 SMRange OperandRange = Operand.getLocRange();
2799 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002800 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002801 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002802 }
2803
Nirav Dave2364748a2016-09-16 18:30:20 +00002804 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Chad Rosier4453e842012-10-12 23:09:25 +00002805 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002806 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002807
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002808 // If one instruction matched with a missing feature, report this as a
2809 // missing feature.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002810 if (std::count(std::begin(Match), std::end(Match),
2811 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002812 ErrorInfo = ErrorInfoMissingFeature;
2813 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002814 MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002815 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002816
Chris Lattner628fbec2010-09-06 21:54:15 +00002817 // If one instruction matched with an invalid operand, report this as an
2818 // operand failure.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002819 if (std::count(std::begin(Match), std::end(Match),
2820 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002821 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002822 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002823 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002824
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002825 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002826 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Nirav Dave2364748a2016-09-16 18:30:20 +00002827 EmptyRange, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002828 return true;
2829}
2830
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002831bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
2832 OperandVector &Operands,
2833 MCStreamer &Out,
2834 uint64_t &ErrorInfo,
2835 bool MatchingInlineAsm) {
2836 assert(!Operands.empty() && "Unexpect empty operand list!");
2837 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2838 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
2839 StringRef Mnemonic = Op.getToken();
Nirav Dave2364748a2016-09-16 18:30:20 +00002840 SMRange EmptyRange = None;
Nirav Daveee554e62016-10-06 15:28:08 +00002841 StringRef Base = Op.getToken();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002842
2843 // First, handle aliases that expand to multiple instructions.
2844 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
2845
2846 MCInst Inst;
2847
2848 // Find one unsized memory operand, if present.
2849 X86Operand *UnsizedMemOp = nullptr;
2850 for (const auto &Op : Operands) {
2851 X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002852 if (X86Op->isMemUnsized())
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002853 UnsizedMemOp = X86Op;
2854 }
2855
2856 // Allow some instructions to have implicitly pointer-sized operands. This is
2857 // compatible with gas.
2858 if (UnsizedMemOp) {
2859 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"};
2860 for (const char *Instr : PtrSizedInstrs) {
2861 if (Mnemonic == Instr) {
Craig Topper055845f2015-01-02 07:02:25 +00002862 UnsizedMemOp->Mem.Size = getPointerWidth();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002863 break;
2864 }
2865 }
2866 }
2867
Nirav Daveee554e62016-10-06 15:28:08 +00002868 SmallVector<unsigned, 8> Match;
2869 uint64_t ErrorInfoMissingFeature = 0;
2870
2871 // If unsized push has immediate operand we should default the default pointer
2872 // size for the size.
2873 if (Mnemonic == "push" && Operands.size() == 2) {
2874 auto *X86Op = static_cast<X86Operand *>(Operands[1].get());
2875 if (X86Op->isImm()) {
2876 // If it's not a constant fall through and let remainder take care of it.
2877 const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm());
2878 unsigned Size = getPointerWidth();
2879 if (CE &&
2880 (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) {
2881 SmallString<16> Tmp;
2882 Tmp += Base;
2883 Tmp += (is64BitMode())
2884 ? "q"
2885 : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " ";
2886 Op.setTokenValue(Tmp);
2887 // Do match in ATT mode to allow explicit suffix usage.
2888 Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo,
2889 MatchingInlineAsm,
2890 false /*isParsingIntelSyntax()*/));
2891 Op.setTokenValue(Base);
2892 }
2893 }
2894 }
2895
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002896 // If an unsized memory operand is present, try to match with each memory
2897 // operand size. In Intel assembly, the size is not part of the instruction
2898 // mnemonic.
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002899 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) {
Ahmed Bougachad65f7872014-12-03 02:03:26 +00002900 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512};
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002901 for (unsigned Size : MopSizes) {
2902 UnsizedMemOp->Mem.Size = Size;
2903 uint64_t ErrorInfoIgnore;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002904 unsigned LastOpcode = Inst.getOpcode();
Nirav Dave6477ce22016-09-26 19:33:36 +00002905 unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2906 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002907 if (Match.empty() || LastOpcode != Inst.getOpcode())
2908 Match.push_back(M);
2909
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002910 // If this returned as a missing feature failure, remember that.
2911 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002912 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002913 }
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002914
2915 // Restore the size of the unsized memory operand if we modified it.
2916 if (UnsizedMemOp)
2917 UnsizedMemOp->Mem.Size = 0;
2918 }
2919
2920 // If we haven't matched anything yet, this is not a basic integer or FPU
Saleem Abdulrasoolc3f8ad32015-01-16 20:16:06 +00002921 // operation. There shouldn't be any ambiguity in our mnemonic table, so try
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002922 // matching with the unsized operand.
2923 if (Match.empty()) {
Nirav Dave6477ce22016-09-26 19:33:36 +00002924 Match.push_back(MatchInstruction(
2925 Operands, Inst, ErrorInfo, MatchingInlineAsm, isParsingIntelSyntax()));
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002926 // If this returned as a missing feature failure, remember that.
2927 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002928 ErrorInfoMissingFeature = ErrorInfo;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002929 }
2930
2931 // Restore the size of the unsized memory operand if we modified it.
2932 if (UnsizedMemOp)
2933 UnsizedMemOp->Mem.Size = 0;
2934
2935 // If it's a bad mnemonic, all results will be the same.
2936 if (Match.back() == Match_MnemonicFail) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002937 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002938 Op.getLocRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002939 }
2940
2941 // If exactly one matched, then we treat that as a successful match (and the
2942 // instruction will already have been filled in correctly, since the failing
2943 // matches won't have modified it).
2944 unsigned NumSuccessfulMatches =
2945 std::count(std::begin(Match), std::end(Match), Match_Success);
2946 if (NumSuccessfulMatches == 1) {
2947 // Some instructions need post-processing to, for example, tweak which
2948 // encoding is selected. Loop on it while changes happen so the individual
2949 // transformations can chain off each other.
2950 if (!MatchingInlineAsm)
2951 while (processInstruction(Inst, Operands))
2952 ;
2953 Inst.setLoc(IDLoc);
2954 if (!MatchingInlineAsm)
2955 EmitInstruction(Inst, Operands, Out);
2956 Opcode = Inst.getOpcode();
2957 return false;
2958 } else if (NumSuccessfulMatches > 1) {
2959 assert(UnsizedMemOp &&
2960 "multiple matches only possible with unsized memory operands");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002961 return Error(UnsizedMemOp->getStartLoc(),
2962 "ambiguous operand size for instruction '" + Mnemonic + "\'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002963 UnsizedMemOp->getLocRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002964 }
2965
2966 // If one instruction matched with a missing feature, report this as a
2967 // missing feature.
2968 if (std::count(std::begin(Match), std::end(Match),
2969 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002970 ErrorInfo = ErrorInfoMissingFeature;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002971 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
2972 MatchingInlineAsm);
2973 }
2974
2975 // If one instruction matched with an invalid operand, report this as an
2976 // operand failure.
2977 if (std::count(std::begin(Match), std::end(Match),
2978 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002979 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002980 MatchingInlineAsm);
2981 }
2982
2983 // If all of these were an outright failure, report it in a useless way.
Nirav Dave2364748a2016-09-16 18:30:20 +00002984 return Error(IDLoc, "unknown instruction mnemonic", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002985 MatchingInlineAsm);
2986}
2987
Nico Weber42f79db2014-07-17 20:24:55 +00002988bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
2989 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
2990}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002991
Devang Patel4a6e7782012-01-12 18:03:40 +00002992bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002993 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00002994 StringRef IDVal = DirectiveID.getIdentifier();
2995 if (IDVal == ".word")
2996 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002997 else if (IDVal.startswith(".code"))
2998 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002999 else if (IDVal.startswith(".att_syntax")) {
Reid Klecknerce63b792014-08-06 23:21:13 +00003000 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3001 if (Parser.getTok().getString() == "prefix")
3002 Parser.Lex();
3003 else if (Parser.getTok().getString() == "noprefix")
3004 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
3005 "supported: registers must have a "
3006 "'%' prefix in .att_syntax");
3007 }
Chad Rosier6f8d8b22012-09-10 20:54:39 +00003008 getParser().setAssemblerDialect(0);
3009 return false;
3010 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00003011 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00003012 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003013 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00003014 Parser.Lex();
Reid Klecknerce63b792014-08-06 23:21:13 +00003015 else if (Parser.getTok().getString() == "prefix")
3016 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
3017 "supported: registers must not have "
3018 "a '%' prefix in .intel_syntax");
Devang Patel9a9bb5c2012-01-30 20:02:42 +00003019 }
3020 return false;
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003021 } else if (IDVal == ".even")
3022 return parseDirectiveEven(DirectiveID.getLoc());
Chris Lattner72c0b592010-10-30 17:38:55 +00003023 return true;
3024}
3025
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003026/// parseDirectiveEven
3027/// ::= .even
3028bool X86AsmParser::parseDirectiveEven(SMLoc L) {
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003029 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3030 TokError("unexpected token in directive");
3031 return false;
3032 }
Eric Christopher445c9522016-10-14 05:47:37 +00003033 const MCSection *Section = getStreamer().getCurrentSectionOnly();
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003034 if (!Section) {
3035 getStreamer().InitSections(false);
Eric Christopher445c9522016-10-14 05:47:37 +00003036 Section = getStreamer().getCurrentSectionOnly();
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003037 }
3038 if (Section->UseCodeAlign())
3039 getStreamer().EmitCodeAlignment(2, 0);
3040 else
3041 getStreamer().EmitValueToAlignment(2, 0, 1, 0);
3042 return false;
3043}
Chris Lattner72c0b592010-10-30 17:38:55 +00003044/// ParseDirectiveWord
3045/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00003046bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003047 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00003048 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3049 for (;;) {
3050 const MCExpr *Value;
David Majnemera375b262015-10-26 02:45:50 +00003051 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003052 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003053 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00003054
David Majnemera375b262015-10-26 02:45:50 +00003055 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
3056 assert(Size <= 8 && "Invalid size");
3057 uint64_t IntValue = MCE->getValue();
3058 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
3059 return Error(ExprLoc, "literal value out of range for directive");
3060 getStreamer().EmitIntValue(IntValue, Size);
3061 } else {
3062 getStreamer().EmitValue(Value, Size, ExprLoc);
3063 }
Chad Rosier51afe632012-06-27 22:34:28 +00003064
Chris Lattner72c0b592010-10-30 17:38:55 +00003065 if (getLexer().is(AsmToken::EndOfStatement))
3066 break;
Chad Rosier51afe632012-06-27 22:34:28 +00003067
Chris Lattner72c0b592010-10-30 17:38:55 +00003068 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003069 if (getLexer().isNot(AsmToken::Comma)) {
3070 Error(L, "unexpected token in directive");
3071 return false;
3072 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003073 Parser.Lex();
3074 }
3075 }
Chad Rosier51afe632012-06-27 22:34:28 +00003076
Chris Lattner72c0b592010-10-30 17:38:55 +00003077 Parser.Lex();
3078 return false;
3079}
3080
Evan Cheng481ebb02011-07-27 00:38:12 +00003081/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00003082/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00003083bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003084 MCAsmParser &Parser = getParser();
Nirav Dave6477ce22016-09-26 19:33:36 +00003085 Code16GCC = false;
Craig Topper3c80d622014-01-06 04:55:54 +00003086 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00003087 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00003088 if (!is16BitMode()) {
3089 SwitchMode(X86::Mode16Bit);
3090 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3091 }
Nirav Dave6477ce22016-09-26 19:33:36 +00003092 } else if (IDVal == ".code16gcc") {
3093 // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode.
3094 Parser.Lex();
3095 Code16GCC = true;
3096 if (!is16BitMode()) {
3097 SwitchMode(X86::Mode16Bit);
3098 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3099 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003100 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00003101 Parser.Lex();
3102 if (!is32BitMode()) {
3103 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003104 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
3105 }
3106 } else if (IDVal == ".code64") {
3107 Parser.Lex();
3108 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00003109 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003110 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
3111 }
3112 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003113 Error(L, "unknown directive " + IDVal);
3114 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00003115 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003116
Evan Cheng481ebb02011-07-27 00:38:12 +00003117 return false;
3118}
Chris Lattner72c0b592010-10-30 17:38:55 +00003119
Daniel Dunbar71475772009-07-17 20:42:00 +00003120// Force static initialization.
3121extern "C" void LLVMInitializeX86AsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00003122 RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target());
3123 RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target());
Daniel Dunbar71475772009-07-17 20:42:00 +00003124}
Daniel Dunbar00331992009-07-29 00:02:19 +00003125
Chris Lattner3e4582a2010-09-06 19:11:01 +00003126#define GET_REGISTER_MATCHER
3127#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00003128#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00003129#include "X86GenAsmMatcher.inc"