blob: 2e45d9eee125c774efbcd28f23c9770d9be408ce [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
Reid Kleckner9cdd4df2017-10-11 21:24:33 +000010#include "InstPrinter/X86IntelInstPrinter.h"
Evan Cheng11424442011-07-26 00:24:13 +000011#include "MCTargetDesc/X86BaseInfo.h"
Reid Kleckner9cdd4df2017-10-11 21:24:33 +000012#include "MCTargetDesc/X86TargetStreamer.h"
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000013#include "X86AsmInstrumentation.h"
Evgeniy Stepanove3804d42014-02-28 12:28:07 +000014#include "X86AsmParserCommon.h"
15#include "X86Operand.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000019#include "llvm/ADT/StringSwitch.h"
20#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000021#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000024#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/MC/MCParser/MCAsmLexer.h"
26#include "llvm/MC/MCParser/MCAsmParser.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000028#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/MC/MCRegisterInfo.h"
Michael Zuckerman02ecd432015-12-13 17:07:23 +000030#include "llvm/MC/MCSection.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCSubtargetInfo.h"
33#include "llvm/MC/MCSymbol.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000034#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000035#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000036#include "llvm/Support/raw_ostream.h"
Reid Kleckner7b1e1a02014-07-30 22:23:11 +000037#include <algorithm>
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000038#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000039
Daniel Dunbar71475772009-07-17 20:42:00 +000040using namespace llvm;
41
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +000042static bool checkScale(unsigned Scale, StringRef &ErrMsg) {
43 if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
44 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
45 return true;
46 }
47 return false;
48}
49
Daniel Dunbar71475772009-07-17 20:42:00 +000050namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000051
Chad Rosier5362af92013-04-16 18:15:40 +000052static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000053 0, // IC_OR
Michael Kupersteine3de07a2015-06-14 12:59:45 +000054 1, // IC_XOR
55 2, // IC_AND
56 3, // IC_LSHIFT
57 3, // IC_RSHIFT
58 4, // IC_PLUS
59 4, // IC_MINUS
60 5, // IC_MULTIPLY
61 5, // IC_DIVIDE
Coby Tayree41a5b552017-06-27 16:58:27 +000062 5, // IC_MOD
63 6, // IC_NOT
64 7, // IC_NEG
65 8, // IC_RPAREN
66 9, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000067 0, // IC_IMM
68 0 // IC_REGISTER
69};
70
Devang Patel4a6e7782012-01-12 18:03:40 +000071class X86AsmParser : public MCTargetAsmParser {
Chad Rosierf0e87202012-10-25 20:41:34 +000072 ParseInstructionInfo *InstInfo;
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000073 std::unique_ptr<X86AsmInstrumentation> Instrumentation;
Nirav Dave6477ce22016-09-26 19:33:36 +000074 bool Code16GCC;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000075
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000076private:
Alp Tokera5b88a52013-12-02 16:06:06 +000077 SMLoc consumeToken() {
Rafael Espindola961d4692014-11-11 05:18:41 +000078 MCAsmParser &Parser = getParser();
Alp Tokera5b88a52013-12-02 16:06:06 +000079 SMLoc Result = Parser.getTok().getLoc();
80 Parser.Lex();
81 return Result;
82 }
83
Reid Kleckner9cdd4df2017-10-11 21:24:33 +000084 X86TargetStreamer &getTargetStreamer() {
85 assert(getParser().getStreamer().getTargetStreamer() &&
86 "do not have a target streamer");
87 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
88 return static_cast<X86TargetStreamer &>(TS);
89 }
90
Nirav Dave6477ce22016-09-26 19:33:36 +000091 unsigned MatchInstruction(const OperandVector &Operands, MCInst &Inst,
92 uint64_t &ErrorInfo, bool matchingInlineAsm,
93 unsigned VariantID = 0) {
94 // In Code16GCC mode, match as 32-bit.
95 if (Code16GCC)
96 SwitchMode(X86::Mode32Bit);
97 unsigned rv = MatchInstructionImpl(Operands, Inst, ErrorInfo,
98 matchingInlineAsm, VariantID);
99 if (Code16GCC)
100 SwitchMode(X86::Mode16Bit);
101 return rv;
102 }
103
Chad Rosier5362af92013-04-16 18:15:40 +0000104 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000105 IC_OR = 0,
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000106 IC_XOR,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000107 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000108 IC_LSHIFT,
109 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000110 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +0000111 IC_MINUS,
112 IC_MULTIPLY,
113 IC_DIVIDE,
Coby Tayree41a5b552017-06-27 16:58:27 +0000114 IC_MOD,
115 IC_NOT,
116 IC_NEG,
Chad Rosier5362af92013-04-16 18:15:40 +0000117 IC_RPAREN,
118 IC_LPAREN,
119 IC_IMM,
120 IC_REGISTER
121 };
122
Coby Tayree07a89742017-03-21 19:31:55 +0000123 enum IntelOperatorKind {
124 IOK_INVALID = 0,
125 IOK_LENGTH,
126 IOK_SIZE,
127 IOK_TYPE,
128 IOK_OFFSET
129 };
130
Chad Rosier5362af92013-04-16 18:15:40 +0000131 class InfixCalculator {
132 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
133 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
134 SmallVector<ICToken, 4> PostfixStack;
Michael Liao5bf95782014-12-04 05:20:33 +0000135
Coby Tayree41a5b552017-06-27 16:58:27 +0000136 bool isUnaryOperator(const InfixCalculatorTok Op) {
137 return Op == IC_NEG || Op == IC_NOT;
138 }
139
Chad Rosier5362af92013-04-16 18:15:40 +0000140 public:
141 int64_t popOperand() {
142 assert (!PostfixStack.empty() && "Poped an empty stack!");
143 ICToken Op = PostfixStack.pop_back_val();
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000144 if (!(Op.first == IC_IMM || Op.first == IC_REGISTER))
145 return -1; // The invalid Scale value will be caught later by checkScale
Chad Rosier5362af92013-04-16 18:15:40 +0000146 return Op.second;
147 }
148 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
149 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
150 "Unexpected operand!");
151 PostfixStack.push_back(std::make_pair(Op, Val));
152 }
Michael Liao5bf95782014-12-04 05:20:33 +0000153
Jakub Staszak9c349222013-08-08 15:48:46 +0000154 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000155 void pushOperator(InfixCalculatorTok Op) {
156 // Push the new operator if the stack is empty.
157 if (InfixOperatorStack.empty()) {
158 InfixOperatorStack.push_back(Op);
159 return;
160 }
Michael Liao5bf95782014-12-04 05:20:33 +0000161
Chad Rosier5362af92013-04-16 18:15:40 +0000162 // Push the new operator if it has a higher precedence than the operator
163 // on the top of the stack or the operator on the top of the stack is a
164 // left parentheses.
165 unsigned Idx = InfixOperatorStack.size() - 1;
166 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
167 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
168 InfixOperatorStack.push_back(Op);
169 return;
170 }
Michael Liao5bf95782014-12-04 05:20:33 +0000171
Chad Rosier5362af92013-04-16 18:15:40 +0000172 // The operator on the top of the stack has higher precedence than the
173 // new operator.
174 unsigned ParenCount = 0;
Kirill Bobyrev6afbaf02017-01-18 16:34:25 +0000175 while (1) {
Chad Rosier5362af92013-04-16 18:15:40 +0000176 // Nothing to process.
177 if (InfixOperatorStack.empty())
178 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000179
Chad Rosier5362af92013-04-16 18:15:40 +0000180 Idx = InfixOperatorStack.size() - 1;
181 StackOp = InfixOperatorStack[Idx];
182 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
183 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000184
Chad Rosier5362af92013-04-16 18:15:40 +0000185 // If we have an even parentheses count and we see a left parentheses,
186 // then stop processing.
187 if (!ParenCount && StackOp == IC_LPAREN)
188 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000189
Chad Rosier5362af92013-04-16 18:15:40 +0000190 if (StackOp == IC_RPAREN) {
191 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000192 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000193 } else if (StackOp == IC_LPAREN) {
194 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000195 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000196 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000197 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000198 PostfixStack.push_back(std::make_pair(StackOp, 0));
199 }
200 }
201 // Push the new operator.
202 InfixOperatorStack.push_back(Op);
203 }
Marina Yatsinaa0e02412015-08-10 11:33:10 +0000204
Chad Rosier5362af92013-04-16 18:15:40 +0000205 int64_t execute() {
206 // Push any remaining operators onto the postfix stack.
207 while (!InfixOperatorStack.empty()) {
208 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
209 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
210 PostfixStack.push_back(std::make_pair(StackOp, 0));
211 }
Michael Liao5bf95782014-12-04 05:20:33 +0000212
Chad Rosier5362af92013-04-16 18:15:40 +0000213 if (PostfixStack.empty())
214 return 0;
Michael Liao5bf95782014-12-04 05:20:33 +0000215
Chad Rosier5362af92013-04-16 18:15:40 +0000216 SmallVector<ICToken, 16> OperandStack;
217 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
218 ICToken Op = PostfixStack[i];
219 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
220 OperandStack.push_back(Op);
Coby Tayree41a5b552017-06-27 16:58:27 +0000221 } else if (isUnaryOperator(Op.first)) {
222 assert (OperandStack.size() > 0 && "Too few operands.");
223 ICToken Operand = OperandStack.pop_back_val();
224 assert (Operand.first == IC_IMM &&
225 "Unary operation with a register!");
226 switch (Op.first) {
227 default:
228 report_fatal_error("Unexpected operator!");
229 break;
230 case IC_NEG:
231 OperandStack.push_back(std::make_pair(IC_IMM, -Operand.second));
232 break;
233 case IC_NOT:
234 OperandStack.push_back(std::make_pair(IC_IMM, ~Operand.second));
235 break;
236 }
Chad Rosier5362af92013-04-16 18:15:40 +0000237 } else {
238 assert (OperandStack.size() > 1 && "Too few operands.");
239 int64_t Val;
240 ICToken Op2 = OperandStack.pop_back_val();
241 ICToken Op1 = OperandStack.pop_back_val();
242 switch (Op.first) {
243 default:
244 report_fatal_error("Unexpected operator!");
245 break;
246 case IC_PLUS:
247 Val = Op1.second + Op2.second;
248 OperandStack.push_back(std::make_pair(IC_IMM, Val));
249 break;
250 case IC_MINUS:
251 Val = Op1.second - Op2.second;
252 OperandStack.push_back(std::make_pair(IC_IMM, Val));
253 break;
254 case IC_MULTIPLY:
255 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
256 "Multiply operation with an immediate and a register!");
257 Val = Op1.second * Op2.second;
258 OperandStack.push_back(std::make_pair(IC_IMM, Val));
259 break;
260 case IC_DIVIDE:
261 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
262 "Divide operation with an immediate and a register!");
263 assert (Op2.second != 0 && "Division by zero!");
264 Val = Op1.second / Op2.second;
265 OperandStack.push_back(std::make_pair(IC_IMM, Val));
266 break;
Coby Tayree41a5b552017-06-27 16:58:27 +0000267 case IC_MOD:
268 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
269 "Modulo operation with an immediate and a register!");
270 Val = Op1.second % Op2.second;
271 OperandStack.push_back(std::make_pair(IC_IMM, Val));
272 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000273 case IC_OR:
274 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
275 "Or operation with an immediate and a register!");
276 Val = Op1.second | Op2.second;
277 OperandStack.push_back(std::make_pair(IC_IMM, Val));
278 break;
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000279 case IC_XOR:
280 assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
281 "Xor operation with an immediate and a register!");
282 Val = Op1.second ^ Op2.second;
283 OperandStack.push_back(std::make_pair(IC_IMM, Val));
284 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000285 case IC_AND:
286 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
287 "And operation with an immediate and a register!");
288 Val = Op1.second & Op2.second;
289 OperandStack.push_back(std::make_pair(IC_IMM, Val));
290 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000291 case IC_LSHIFT:
292 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
293 "Left shift operation with an immediate and a register!");
294 Val = Op1.second << Op2.second;
295 OperandStack.push_back(std::make_pair(IC_IMM, Val));
296 break;
297 case IC_RSHIFT:
298 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
299 "Right shift operation with an immediate and a register!");
300 Val = Op1.second >> Op2.second;
301 OperandStack.push_back(std::make_pair(IC_IMM, Val));
302 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000303 }
304 }
305 }
306 assert (OperandStack.size() == 1 && "Expected a single result.");
307 return OperandStack.pop_back_val().second;
308 }
309 };
310
311 enum IntelExprState {
Coby Tayreed8912892017-08-24 08:46:25 +0000312 IES_INIT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000313 IES_OR,
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000314 IES_XOR,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000315 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000316 IES_LSHIFT,
317 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000318 IES_PLUS,
319 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000320 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000321 IES_MULTIPLY,
322 IES_DIVIDE,
Coby Tayree41a5b552017-06-27 16:58:27 +0000323 IES_MOD,
Chad Rosier5362af92013-04-16 18:15:40 +0000324 IES_LBRAC,
325 IES_RBRAC,
326 IES_LPAREN,
327 IES_RPAREN,
328 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000329 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000330 IES_IDENTIFIER,
331 IES_ERROR
332 };
333
334 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000335 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000336 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000337 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000338 const MCExpr *Sym;
339 StringRef SymName;
340 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000341 InlineAsmIdentifierInfo Info;
Coby Tayreed8912892017-08-24 08:46:25 +0000342 short BracCount;
343 bool MemExpr;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000344
Chad Rosier5362af92013-04-16 18:15:40 +0000345 public:
Coby Tayreed8912892017-08-24 08:46:25 +0000346 IntelExprStateMachine()
347 : State(IES_INIT), PrevState(IES_ERROR), BaseReg(0), IndexReg(0),
348 TmpReg(0), Scale(1), Imm(0), Sym(nullptr), BracCount(0),
Coby Tayreec3d24112017-09-29 07:02:46 +0000349 MemExpr(false) {}
Michael Liao5bf95782014-12-04 05:20:33 +0000350
Coby Tayreed8912892017-08-24 08:46:25 +0000351 void addImm(int64_t imm) { Imm += imm; }
352 short getBracCount() { return BracCount; }
353 bool isMemExpr() { return MemExpr; }
Chad Rosier5362af92013-04-16 18:15:40 +0000354 unsigned getBaseReg() { return BaseReg; }
355 unsigned getIndexReg() { return IndexReg; }
356 unsigned getScale() { return Scale; }
357 const MCExpr *getSym() { return Sym; }
358 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000359 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000360 bool isValidEndState() {
361 return State == IES_RBRAC || State == IES_INTEGER;
362 }
Chad Rosier31246272013-04-17 21:01:45 +0000363 bool hadError() { return State == IES_ERROR; }
Coby Tayreed8912892017-08-24 08:46:25 +0000364 InlineAsmIdentifierInfo &getIdentifierInfo() { return Info; }
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000365
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000366 void onOr() {
367 IntelExprState CurrState = State;
368 switch (State) {
369 default:
370 State = IES_ERROR;
371 break;
372 case IES_INTEGER:
373 case IES_RPAREN:
374 case IES_REGISTER:
375 State = IES_OR;
376 IC.pushOperator(IC_OR);
377 break;
378 }
379 PrevState = CurrState;
380 }
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000381 void onXor() {
382 IntelExprState CurrState = State;
383 switch (State) {
384 default:
385 State = IES_ERROR;
386 break;
387 case IES_INTEGER:
388 case IES_RPAREN:
389 case IES_REGISTER:
390 State = IES_XOR;
391 IC.pushOperator(IC_XOR);
392 break;
393 }
394 PrevState = CurrState;
395 }
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000396 void onAnd() {
397 IntelExprState CurrState = State;
398 switch (State) {
399 default:
400 State = IES_ERROR;
401 break;
402 case IES_INTEGER:
403 case IES_RPAREN:
404 case IES_REGISTER:
405 State = IES_AND;
406 IC.pushOperator(IC_AND);
407 break;
408 }
409 PrevState = CurrState;
410 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000411 void onLShift() {
412 IntelExprState CurrState = State;
413 switch (State) {
414 default:
415 State = IES_ERROR;
416 break;
417 case IES_INTEGER:
418 case IES_RPAREN:
419 case IES_REGISTER:
420 State = IES_LSHIFT;
421 IC.pushOperator(IC_LSHIFT);
422 break;
423 }
424 PrevState = CurrState;
425 }
426 void onRShift() {
427 IntelExprState CurrState = State;
428 switch (State) {
429 default:
430 State = IES_ERROR;
431 break;
432 case IES_INTEGER:
433 case IES_RPAREN:
434 case IES_REGISTER:
435 State = IES_RSHIFT;
436 IC.pushOperator(IC_RSHIFT);
437 break;
438 }
439 PrevState = CurrState;
440 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000441 bool onPlus(StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000442 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000443 switch (State) {
444 default:
445 State = IES_ERROR;
446 break;
447 case IES_INTEGER:
448 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000449 case IES_REGISTER:
450 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000451 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000452 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
453 // If we already have a BaseReg, then assume this is the IndexReg with
454 // a scale of 1.
455 if (!BaseReg) {
456 BaseReg = TmpReg;
457 } else {
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000458 if (IndexReg) {
459 ErrMsg = "BaseReg/IndexReg already set!";
460 return true;
461 }
Chad Rosier31246272013-04-17 21:01:45 +0000462 IndexReg = TmpReg;
463 Scale = 1;
464 }
465 }
Chad Rosier5362af92013-04-16 18:15:40 +0000466 break;
467 }
Chad Rosier31246272013-04-17 21:01:45 +0000468 PrevState = CurrState;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000469 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000470 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000471 bool onMinus(StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000472 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000473 switch (State) {
474 default:
475 State = IES_ERROR;
476 break;
Coby Tayree41a5b552017-06-27 16:58:27 +0000477 case IES_OR:
478 case IES_XOR:
479 case IES_AND:
480 case IES_LSHIFT:
481 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000482 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000483 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000484 case IES_MULTIPLY:
485 case IES_DIVIDE:
Coby Tayree41a5b552017-06-27 16:58:27 +0000486 case IES_MOD:
Chad Rosier5362af92013-04-16 18:15:40 +0000487 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000488 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000489 case IES_LBRAC:
490 case IES_RBRAC:
491 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000492 case IES_REGISTER:
Coby Tayreed8912892017-08-24 08:46:25 +0000493 case IES_INIT:
Chad Rosier5362af92013-04-16 18:15:40 +0000494 State = IES_MINUS;
Coby Tayree41a5b552017-06-27 16:58:27 +0000495 // push minus operator if it is not a negate operator
496 if (CurrState == IES_REGISTER || CurrState == IES_RPAREN ||
497 CurrState == IES_INTEGER || CurrState == IES_RBRAC)
Chad Rosier31246272013-04-17 21:01:45 +0000498 IC.pushOperator(IC_MINUS);
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000499 else if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
500 // We have negate operator for Scale: it's illegal
501 ErrMsg = "Scale can't be negative";
502 return true;
503 } else
Coby Tayree41a5b552017-06-27 16:58:27 +0000504 IC.pushOperator(IC_NEG);
Chad Rosier31246272013-04-17 21:01:45 +0000505 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
506 // If we already have a BaseReg, then assume this is the IndexReg with
507 // a scale of 1.
508 if (!BaseReg) {
509 BaseReg = TmpReg;
510 } else {
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000511 if (IndexReg) {
512 ErrMsg = "BaseReg/IndexReg already set!";
513 return true;
514 }
Chad Rosier31246272013-04-17 21:01:45 +0000515 IndexReg = TmpReg;
516 Scale = 1;
517 }
Chad Rosier5362af92013-04-16 18:15:40 +0000518 }
Chad Rosier5362af92013-04-16 18:15:40 +0000519 break;
520 }
Chad Rosier31246272013-04-17 21:01:45 +0000521 PrevState = CurrState;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000522 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000523 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000524 void onNot() {
525 IntelExprState CurrState = State;
526 switch (State) {
527 default:
528 State = IES_ERROR;
529 break;
Coby Tayree41a5b552017-06-27 16:58:27 +0000530 case IES_OR:
531 case IES_XOR:
532 case IES_AND:
533 case IES_LSHIFT:
534 case IES_RSHIFT:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000535 case IES_PLUS:
Coby Tayree41a5b552017-06-27 16:58:27 +0000536 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000537 case IES_NOT:
Coby Tayree41a5b552017-06-27 16:58:27 +0000538 case IES_MULTIPLY:
539 case IES_DIVIDE:
540 case IES_MOD:
541 case IES_LPAREN:
542 case IES_LBRAC:
Coby Tayreed8912892017-08-24 08:46:25 +0000543 case IES_INIT:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000544 State = IES_NOT;
Coby Tayree41a5b552017-06-27 16:58:27 +0000545 IC.pushOperator(IC_NOT);
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000546 break;
547 }
548 PrevState = CurrState;
549 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000550
551 bool onRegister(unsigned Reg, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000552 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000553 switch (State) {
554 default:
555 State = IES_ERROR;
556 break;
557 case IES_PLUS:
558 case IES_LPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000559 case IES_LBRAC:
Chad Rosier5362af92013-04-16 18:15:40 +0000560 State = IES_REGISTER;
561 TmpReg = Reg;
562 IC.pushOperand(IC_REGISTER);
563 break;
Chad Rosier31246272013-04-17 21:01:45 +0000564 case IES_MULTIPLY:
565 // Index Register - Scale * Register
566 if (PrevState == IES_INTEGER) {
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000567 if (IndexReg) {
568 ErrMsg = "BaseReg/IndexReg already set!";
569 return true;
570 }
Chad Rosier31246272013-04-17 21:01:45 +0000571 State = IES_REGISTER;
572 IndexReg = Reg;
573 // Get the scale and replace the 'Scale * Register' with '0'.
574 Scale = IC.popOperand();
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000575 if (checkScale(Scale, ErrMsg))
576 return true;
Chad Rosier31246272013-04-17 21:01:45 +0000577 IC.pushOperand(IC_IMM);
578 IC.popOperator();
579 } else {
580 State = IES_ERROR;
581 }
Chad Rosier5362af92013-04-16 18:15:40 +0000582 break;
583 }
Chad Rosier31246272013-04-17 21:01:45 +0000584 PrevState = CurrState;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000585 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000586 }
Coby Tayreed8912892017-08-24 08:46:25 +0000587 bool onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName,
Coby Tayreec3d24112017-09-29 07:02:46 +0000588 const InlineAsmIdentifierInfo &IDInfo,
589 bool ParsingInlineAsm, StringRef &ErrMsg) {
590 // InlineAsm: Treat an enum value as an integer
591 if (ParsingInlineAsm)
592 if (IDInfo.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
593 return onInteger(IDInfo.Enum.EnumVal, ErrMsg);
594 // Treat a symbolic constant like an integer
595 if (auto *CE = dyn_cast<MCConstantExpr>(SymRef))
596 return onInteger(CE->getValue(), ErrMsg);
Chad Rosierdb003992013-04-18 16:28:19 +0000597 PrevState = State;
Coby Tayreed8912892017-08-24 08:46:25 +0000598 bool HasSymbol = Sym != nullptr;
Chad Rosier5362af92013-04-16 18:15:40 +0000599 switch (State) {
600 default:
601 State = IES_ERROR;
602 break;
603 case IES_PLUS:
604 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000605 case IES_NOT:
Coby Tayreed8912892017-08-24 08:46:25 +0000606 case IES_INIT:
607 case IES_LBRAC:
Coby Tayreec3d24112017-09-29 07:02:46 +0000608 MemExpr = true;
Chad Rosier5362af92013-04-16 18:15:40 +0000609 State = IES_INTEGER;
610 Sym = SymRef;
611 SymName = SymRefName;
612 IC.pushOperand(IC_IMM);
Coby Tayreec3d24112017-09-29 07:02:46 +0000613 if (ParsingInlineAsm)
614 Info = IDInfo;
Chad Rosier5362af92013-04-16 18:15:40 +0000615 break;
616 }
Coby Tayreed8912892017-08-24 08:46:25 +0000617 if (HasSymbol)
618 ErrMsg = "cannot use more than one symbol in memory operand";
619 return HasSymbol;
Chad Rosier5362af92013-04-16 18:15:40 +0000620 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000621 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000622 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000623 switch (State) {
624 default:
625 State = IES_ERROR;
626 break;
627 case IES_PLUS:
628 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000629 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000630 case IES_OR:
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000631 case IES_XOR:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000632 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000633 case IES_LSHIFT:
634 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000635 case IES_DIVIDE:
Coby Tayree41a5b552017-06-27 16:58:27 +0000636 case IES_MOD:
Chad Rosier31246272013-04-17 21:01:45 +0000637 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000638 case IES_LPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000639 case IES_INIT:
640 case IES_LBRAC:
Chad Rosier5362af92013-04-16 18:15:40 +0000641 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000642 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
643 // Index Register - Register * Scale
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000644 if (IndexReg) {
645 ErrMsg = "BaseReg/IndexReg already set!";
Kevin Enderby9d117022014-01-23 21:52:41 +0000646 return true;
647 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000648 IndexReg = TmpReg;
649 Scale = TmpInt;
650 if (checkScale(Scale, ErrMsg))
651 return true;
Chad Rosier31246272013-04-17 21:01:45 +0000652 // Get the scale and replace the 'Register * Scale' with '0'.
653 IC.popOperator();
Chad Rosier31246272013-04-17 21:01:45 +0000654 } else {
655 IC.pushOperand(IC_IMM, TmpInt);
656 }
Chad Rosier5362af92013-04-16 18:15:40 +0000657 break;
658 }
Chad Rosier31246272013-04-17 21:01:45 +0000659 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000660 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000661 }
662 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000663 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000664 switch (State) {
665 default:
666 State = IES_ERROR;
667 break;
668 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000669 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000670 case IES_RPAREN:
671 State = IES_MULTIPLY;
672 IC.pushOperator(IC_MULTIPLY);
673 break;
674 }
675 }
676 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000677 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000678 switch (State) {
679 default:
680 State = IES_ERROR;
681 break;
682 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000683 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000684 State = IES_DIVIDE;
685 IC.pushOperator(IC_DIVIDE);
686 break;
687 }
688 }
Coby Tayree41a5b552017-06-27 16:58:27 +0000689 void onMod() {
690 PrevState = State;
691 switch (State) {
692 default:
693 State = IES_ERROR;
694 break;
695 case IES_INTEGER:
696 case IES_RPAREN:
697 State = IES_MOD;
698 IC.pushOperator(IC_MOD);
699 break;
700 }
701 }
Coby Tayreed8912892017-08-24 08:46:25 +0000702 bool onLBrac() {
703 if (BracCount)
704 return true;
Chad Rosierdb003992013-04-18 16:28:19 +0000705 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000706 switch (State) {
707 default:
708 State = IES_ERROR;
709 break;
710 case IES_RBRAC:
Coby Tayreed8912892017-08-24 08:46:25 +0000711 case IES_INTEGER:
712 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000713 State = IES_PLUS;
714 IC.pushOperator(IC_PLUS);
715 break;
Coby Tayreed8912892017-08-24 08:46:25 +0000716 case IES_INIT:
717 assert(!BracCount && "BracCount should be zero on parsing's start");
718 State = IES_LBRAC;
719 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000720 }
Coby Tayreed8912892017-08-24 08:46:25 +0000721 MemExpr = true;
722 BracCount++;
723 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000724 }
Coby Tayreed8912892017-08-24 08:46:25 +0000725 bool onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000726 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000727 switch (State) {
728 default:
729 State = IES_ERROR;
730 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000731 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000732 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000733 case IES_RPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000734 if (BracCount-- != 1)
735 return true;
Chad Rosier5362af92013-04-16 18:15:40 +0000736 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000737 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
738 // If we already have a BaseReg, then assume this is the IndexReg with
739 // a scale of 1.
740 if (!BaseReg) {
741 BaseReg = TmpReg;
742 } else {
743 assert (!IndexReg && "BaseReg/IndexReg already set!");
744 IndexReg = TmpReg;
745 Scale = 1;
746 }
Chad Rosier5362af92013-04-16 18:15:40 +0000747 }
748 break;
749 }
Chad Rosier31246272013-04-17 21:01:45 +0000750 PrevState = CurrState;
Coby Tayreed8912892017-08-24 08:46:25 +0000751 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000752 }
753 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000754 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000755 switch (State) {
756 default:
757 State = IES_ERROR;
758 break;
759 case IES_PLUS:
760 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000761 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000762 case IES_OR:
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000763 case IES_XOR:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000764 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000765 case IES_LSHIFT:
766 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000767 case IES_MULTIPLY:
768 case IES_DIVIDE:
Coby Tayree41a5b552017-06-27 16:58:27 +0000769 case IES_MOD:
Chad Rosier5362af92013-04-16 18:15:40 +0000770 case IES_LPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000771 case IES_INIT:
772 case IES_LBRAC:
Chad Rosier5362af92013-04-16 18:15:40 +0000773 State = IES_LPAREN;
774 IC.pushOperator(IC_LPAREN);
775 break;
776 }
Chad Rosier31246272013-04-17 21:01:45 +0000777 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000778 }
779 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000780 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000781 switch (State) {
782 default:
783 State = IES_ERROR;
784 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000785 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000786 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000787 case IES_RPAREN:
788 State = IES_RPAREN;
789 IC.pushOperator(IC_RPAREN);
790 break;
791 }
792 }
793 };
794
Nirav Dave2364748a2016-09-16 18:30:20 +0000795 bool Error(SMLoc L, const Twine &Msg, SMRange Range = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000796 bool MatchingInlineAsm = false) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000797 MCAsmParser &Parser = getParser();
Nirav Dave2364748a2016-09-16 18:30:20 +0000798 if (MatchingInlineAsm) {
799 if (!getLexer().isAtStartOfStatement())
800 Parser.eatToEndOfStatement();
801 return false;
802 }
803 return Parser.Error(L, Msg, Range);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000804 }
805
David Blaikie960ea3f2014-06-08 16:18:35 +0000806 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000807 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000808 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000809 }
810
David Blaikie960ea3f2014-06-08 16:18:35 +0000811 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
812 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
Marina Yatsinab9f4f622016-01-19 15:37:56 +0000813 bool IsSIReg(unsigned Reg);
814 unsigned GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, bool IsSIReg);
815 void
816 AddDefaultSrcDestOperands(OperandVector &Operands,
817 std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
818 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst);
819 bool VerifyAndAdjustOperands(OperandVector &OrigOperands,
820 OperandVector &FinalOperands);
David Blaikie960ea3f2014-06-08 16:18:35 +0000821 std::unique_ptr<X86Operand> ParseOperand();
822 std::unique_ptr<X86Operand> ParseATTOperand();
823 std::unique_ptr<X86Operand> ParseIntelOperand();
824 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Coby Tayreed8912892017-08-24 08:46:25 +0000825 bool ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End);
826 unsigned IdentifyIntelInlineAsmOperator(StringRef Name);
827 unsigned ParseIntelInlineAsmOperator(unsigned OpKind);
Craig Topper36d8da32018-01-06 06:41:07 +0000828 std::unique_ptr<X86Operand> ParseRoundingModeOp(SMLoc Start);
Coby Tayree2cb497a2017-04-04 14:43:23 +0000829 bool ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM);
Coby Tayreed8912892017-08-24 08:46:25 +0000830 void RewriteIntelExpression(IntelExprStateMachine &SM, SMLoc Start,
831 SMLoc End);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000832 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Coby Tayreed8912892017-08-24 08:46:25 +0000833 bool ParseIntelInlineAsmIdentifier(const MCExpr *&Val, StringRef &Identifier,
834 InlineAsmIdentifierInfo &Info,
835 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000836
David Blaikie960ea3f2014-06-08 16:18:35 +0000837 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000838
Coby Tayreed8912892017-08-24 08:46:25 +0000839 bool ParseIntelMemoryOperandSize(unsigned &Size);
David Blaikie960ea3f2014-06-08 16:18:35 +0000840 std::unique_ptr<X86Operand>
841 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
842 unsigned IndexReg, unsigned Scale, SMLoc Start,
843 SMLoc End, unsigned Size, StringRef Identifier,
Coby Tayreeef66b3b2017-09-10 12:21:24 +0000844 const InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000845
Michael Zuckerman02ecd432015-12-13 17:07:23 +0000846 bool parseDirectiveEven(SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000847 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000848 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000849
Reid Kleckner9cdd4df2017-10-11 21:24:33 +0000850 /// CodeView FPO data directives.
851 bool parseDirectiveFPOProc(SMLoc L);
852 bool parseDirectiveFPOSetFrame(SMLoc L);
853 bool parseDirectiveFPOPushReg(SMLoc L);
854 bool parseDirectiveFPOStackAlloc(SMLoc L);
855 bool parseDirectiveFPOEndPrologue(SMLoc L);
856 bool parseDirectiveFPOEndProc(SMLoc L);
857 bool parseDirectiveFPOData(SMLoc L);
858
Craig Topper8a2a1042017-10-26 21:03:54 +0000859 bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
David Blaikie960ea3f2014-06-08 16:18:35 +0000860 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000861
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000862 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
863 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000864 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000865
Chad Rosier49963552012-10-13 00:26:04 +0000866 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000867 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000868 uint64_t &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000869 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000870
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000871 void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands,
872 MCStreamer &Out, bool MatchingInlineAsm);
873
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000874 bool ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000875 bool MatchingInlineAsm);
876
877 bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
878 OperandVector &Operands, MCStreamer &Out,
879 uint64_t &ErrorInfo,
880 bool MatchingInlineAsm);
881
882 bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
883 OperandVector &Operands, MCStreamer &Out,
884 uint64_t &ErrorInfo,
885 bool MatchingInlineAsm);
886
Craig Topperfd38cbe2014-08-30 16:48:34 +0000887 bool OmitRegisterFromClobberLists(unsigned RegNo) override;
Nico Weber42f79db2014-07-17 20:24:55 +0000888
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000889 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
890 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
Michael Zuckerman1bee6342016-10-18 13:52:39 +0000891 /// return false if no parsing errors occurred, true otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000892 bool HandleAVX512Operand(OperandVector &Operands,
893 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000894
Michael Zuckerman1bee6342016-10-18 13:52:39 +0000895 bool ParseZ(std::unique_ptr<X86Operand> &Z, const SMLoc &StartLoc);
896
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000897 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000898 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000899 return getSTI().getFeatureBits()[X86::Mode64Bit];
Evan Cheng4d1ca962011-07-08 01:53:10 +0000900 }
Craig Topper3c80d622014-01-06 04:55:54 +0000901 bool is32BitMode() const {
902 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000903 return getSTI().getFeatureBits()[X86::Mode32Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000904 }
905 bool is16BitMode() const {
906 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000907 return getSTI().getFeatureBits()[X86::Mode16Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000908 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000909 void SwitchMode(unsigned mode) {
Akira Hatanakab11ef082015-11-14 06:35:56 +0000910 MCSubtargetInfo &STI = copySTI();
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000911 FeatureBitset AllModes({X86::Mode64Bit, X86::Mode32Bit, X86::Mode16Bit});
912 FeatureBitset OldMode = STI.getFeatureBits() & AllModes;
Craig Topper619b1522017-10-26 06:46:38 +0000913 uint64_t FB = ComputeAvailableFeatures(
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000914 STI.ToggleFeature(OldMode.flip(mode)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000915 setAvailableFeatures(FB);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000916
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000917 assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes));
Evan Cheng481ebb02011-07-27 00:38:12 +0000918 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000919
Reid Kleckner5b37c182014-08-01 20:21:24 +0000920 unsigned getPointerWidth() {
921 if (is16BitMode()) return 16;
922 if (is32BitMode()) return 32;
923 if (is64BitMode()) return 64;
924 llvm_unreachable("invalid mode");
925 }
926
Chad Rosierc2f055d2013-04-18 16:13:18 +0000927 bool isParsingIntelSyntax() {
928 return getParser().getAssemblerDialect();
929 }
930
Daniel Dunbareefe8612010-07-19 05:44:09 +0000931 /// @name Auto-generated Matcher Functions
932 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000933
Chris Lattner3e4582a2010-09-06 19:11:01 +0000934#define GET_ASSEMBLER_HEADER
935#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000936
Daniel Dunbar00331992009-07-29 00:02:19 +0000937 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000938
939public:
Coby Tayree07a89742017-03-21 19:31:55 +0000940
Akira Hatanakab11ef082015-11-14 06:35:56 +0000941 X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser,
Rafael Espindola961d4692014-11-11 05:18:41 +0000942 const MCInstrInfo &mii, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000943 : MCTargetAsmParser(Options, sti, mii), InstInfo(nullptr),
Nirav Dave6477ce22016-09-26 19:33:36 +0000944 Code16GCC(false) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000945
Daniel Dunbareefe8612010-07-19 05:44:09 +0000946 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000947 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000948 Instrumentation.reset(
949 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000950 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000951
Craig Topper39012cc2014-03-09 18:03:14 +0000952 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000953
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000954 void SetFrameRegister(unsigned RegNo) override;
955
David Blaikie960ea3f2014-06-08 16:18:35 +0000956 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
957 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000958
Craig Topper39012cc2014-03-09 18:03:14 +0000959 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000960};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000961} // end anonymous namespace
962
Sean Callanan86c11812010-01-23 00:40:33 +0000963/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000964/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000965
Chris Lattner60db0a62010-02-09 00:34:28 +0000966static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000967
968/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000969
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000970static bool CheckBaseRegAndIndexRegAndScale(unsigned BaseReg, unsigned IndexReg,
971 unsigned Scale, StringRef &ErrMsg) {
Kevin Enderbybc570f22014-01-23 22:34:42 +0000972 // If we have both a base register and an index register make sure they are
973 // both 64-bit or 32-bit registers.
974 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Douglas Katzman0411e862016-10-05 15:23:35 +0000975
976 if ((BaseReg == X86::RIP && IndexReg != 0) || (IndexReg == X86::RIP)) {
977 ErrMsg = "invalid base+index expression";
978 return true;
979 }
Kevin Enderbybc570f22014-01-23 22:34:42 +0000980 if (BaseReg != 0 && IndexReg != 0) {
981 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
982 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
983 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
984 IndexReg != X86::RIZ) {
985 ErrMsg = "base register is 64-bit, but index register is not";
986 return true;
987 }
988 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
989 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
990 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
991 IndexReg != X86::EIZ){
992 ErrMsg = "base register is 32-bit, but index register is not";
993 return true;
994 }
995 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
996 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
997 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
998 ErrMsg = "base register is 16-bit, but index register is not";
999 return true;
1000 }
1001 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
1002 IndexReg != X86::SI && IndexReg != X86::DI) ||
1003 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
1004 IndexReg != X86::BX && IndexReg != X86::BP)) {
1005 ErrMsg = "invalid 16-bit base/index register combination";
1006 return true;
1007 }
1008 }
1009 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001010 return checkScale(Scale, ErrMsg);
Kevin Enderbybc570f22014-01-23 22:34:42 +00001011}
1012
Devang Patel4a6e7782012-01-12 18:03:40 +00001013bool X86AsmParser::ParseRegister(unsigned &RegNo,
1014 SMLoc &StartLoc, SMLoc &EndLoc) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001015 MCAsmParser &Parser = getParser();
Chris Lattnercc2ad082010-01-15 18:27:19 +00001016 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001017 const AsmToken &PercentTok = Parser.getTok();
1018 StartLoc = PercentTok.getLoc();
1019
1020 // If we encounter a %, ignore it. This code handles registers with and
1021 // without the prefix, unprefixed registers can occur in cfi directives.
1022 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001023 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001024
Sean Callanan936b0d32010-01-19 21:44:56 +00001025 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001026 EndLoc = Tok.getEndLoc();
1027
Devang Patelce6a2ca2012-01-20 22:32:05 +00001028 if (Tok.isNot(AsmToken::Identifier)) {
Reid Klecknerc990b5d2017-07-24 20:48:15 +00001029 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001030 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001031 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001032 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001033
Kevin Enderby7d912182009-09-03 17:15:07 +00001034 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001035
Chris Lattner1261b812010-09-22 04:11:10 +00001036 // If the match failed, try the register name as lowercase.
1037 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001038 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001039
Michael Kupersteincdb076b2015-07-30 10:10:25 +00001040 // The "flags" register cannot be referenced directly.
1041 // Treat it as an identifier instead.
1042 if (isParsingInlineAsm() && isParsingIntelSyntax() && RegNo == X86::EFLAGS)
1043 RegNo = 0;
1044
Evan Chengeda1d4f2011-07-27 23:22:03 +00001045 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +00001046 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +00001047 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1048 // checked.
1049 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1050 // REX prefix.
1051 if (RegNo == X86::RIZ ||
1052 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1053 X86II::isX86_64NonExtLowByteReg(RegNo) ||
Craig Topper6acca802016-08-27 17:13:37 +00001054 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001055 return Error(StartLoc, "register %"
1056 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001057 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001058 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001059
Chris Lattner1261b812010-09-22 04:11:10 +00001060 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1061 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001062 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001063 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001064
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001065 // Check to see if we have '(4)' after %st.
1066 if (getLexer().isNot(AsmToken::LParen))
1067 return false;
1068 // Lex the paren.
1069 getParser().Lex();
1070
1071 const AsmToken &IntTok = Parser.getTok();
1072 if (IntTok.isNot(AsmToken::Integer))
1073 return Error(IntTok.getLoc(), "expected stack index");
1074 switch (IntTok.getIntVal()) {
1075 case 0: RegNo = X86::ST0; break;
1076 case 1: RegNo = X86::ST1; break;
1077 case 2: RegNo = X86::ST2; break;
1078 case 3: RegNo = X86::ST3; break;
1079 case 4: RegNo = X86::ST4; break;
1080 case 5: RegNo = X86::ST5; break;
1081 case 6: RegNo = X86::ST6; break;
1082 case 7: RegNo = X86::ST7; break;
1083 default: return Error(IntTok.getLoc(), "invalid stack index");
1084 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001085
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001086 if (getParser().Lex().isNot(AsmToken::RParen))
1087 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001088
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001089 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001090 Parser.Lex(); // Eat ')'
1091 return false;
1092 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001093
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001094 EndLoc = Parser.getTok().getEndLoc();
1095
Craig Topper7d9a3b82017-12-02 08:27:46 +00001096 // If this is "db[0-15]", match it as an alias
1097 // for dr[0-15].
1098 if (RegNo == 0 && Tok.getString().startswith("db")) {
1099 if (Tok.getString().size() == 3) {
1100 switch (Tok.getString()[2]) {
1101 case '0': RegNo = X86::DR0; break;
1102 case '1': RegNo = X86::DR1; break;
1103 case '2': RegNo = X86::DR2; break;
1104 case '3': RegNo = X86::DR3; break;
1105 case '4': RegNo = X86::DR4; break;
1106 case '5': RegNo = X86::DR5; break;
1107 case '6': RegNo = X86::DR6; break;
1108 case '7': RegNo = X86::DR7; break;
1109 case '8': RegNo = X86::DR8; break;
1110 case '9': RegNo = X86::DR9; break;
1111 }
1112 } else if (Tok.getString().size() == 4 && Tok.getString()[2] == '1') {
1113 switch (Tok.getString()[3]) {
1114 case '0': RegNo = X86::DR10; break;
1115 case '1': RegNo = X86::DR11; break;
1116 case '2': RegNo = X86::DR12; break;
1117 case '3': RegNo = X86::DR13; break;
1118 case '4': RegNo = X86::DR14; break;
1119 case '5': RegNo = X86::DR15; break;
1120 }
Chris Lattner80486622010-06-24 07:29:18 +00001121 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001122
Chris Lattner80486622010-06-24 07:29:18 +00001123 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001124 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001125 Parser.Lex(); // Eat it.
1126 return false;
1127 }
1128 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001129
Devang Patelce6a2ca2012-01-20 22:32:05 +00001130 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001131 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001132 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001133 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001134 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001135
Sean Callanana83fd7d2010-01-19 20:27:46 +00001136 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001137 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001138}
1139
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001140void X86AsmParser::SetFrameRegister(unsigned RegNo) {
Yuri Gorshenine8c81fd2014-10-07 11:03:09 +00001141 Instrumentation->SetInitialFrameRegister(RegNo);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001142}
1143
David Blaikie960ea3f2014-06-08 16:18:35 +00001144std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001145 bool Parse32 = is32BitMode() || Code16GCC;
1146 unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001147 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001148 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001149 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001150 Loc, Loc, 0);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001151}
1152
David Blaikie960ea3f2014-06-08 16:18:35 +00001153std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001154 bool Parse32 = is32BitMode() || Code16GCC;
1155 unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001156 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001157 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001158 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001159 Loc, Loc, 0);
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001160}
1161
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001162bool X86AsmParser::IsSIReg(unsigned Reg) {
1163 switch (Reg) {
Craig Topper4d187632016-02-26 05:29:39 +00001164 default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001165 case X86::RSI:
1166 case X86::ESI:
1167 case X86::SI:
1168 return true;
1169 case X86::RDI:
1170 case X86::EDI:
1171 case X86::DI:
1172 return false;
1173 }
1174}
1175
1176unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg,
1177 bool IsSIReg) {
1178 switch (RegClassID) {
Craig Topper4d187632016-02-26 05:29:39 +00001179 default: llvm_unreachable("Unexpected register class");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001180 case X86::GR64RegClassID:
1181 return IsSIReg ? X86::RSI : X86::RDI;
1182 case X86::GR32RegClassID:
1183 return IsSIReg ? X86::ESI : X86::EDI;
1184 case X86::GR16RegClassID:
1185 return IsSIReg ? X86::SI : X86::DI;
1186 }
1187}
1188
Michael Kupersteinffcc7662015-07-23 10:23:48 +00001189void X86AsmParser::AddDefaultSrcDestOperands(
1190 OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
1191 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) {
1192 if (isParsingIntelSyntax()) {
1193 Operands.push_back(std::move(Dst));
1194 Operands.push_back(std::move(Src));
1195 }
1196 else {
1197 Operands.push_back(std::move(Src));
1198 Operands.push_back(std::move(Dst));
1199 }
1200}
1201
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001202bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands,
1203 OperandVector &FinalOperands) {
1204
1205 if (OrigOperands.size() > 1) {
Craig Topperd55f4bc2016-02-16 07:45:07 +00001206 // Check if sizes match, OrigOperands also contains the instruction name
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001207 assert(OrigOperands.size() == FinalOperands.size() + 1 &&
Craig Topperd55f4bc2016-02-16 07:45:07 +00001208 "Operand size mismatch");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001209
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001210 SmallVector<std::pair<SMLoc, std::string>, 2> Warnings;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001211 // Verify types match
1212 int RegClassID = -1;
1213 for (unsigned int i = 0; i < FinalOperands.size(); ++i) {
1214 X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]);
1215 X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]);
1216
1217 if (FinalOp.isReg() &&
1218 (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg()))
1219 // Return false and let a normal complaint about bogus operands happen
1220 return false;
1221
1222 if (FinalOp.isMem()) {
1223
1224 if (!OrigOp.isMem())
1225 // Return false and let a normal complaint about bogus operands happen
1226 return false;
1227
1228 unsigned OrigReg = OrigOp.Mem.BaseReg;
1229 unsigned FinalReg = FinalOp.Mem.BaseReg;
1230
1231 // If we've already encounterd a register class, make sure all register
1232 // bases are of the same register class
1233 if (RegClassID != -1 &&
1234 !X86MCRegisterClasses[RegClassID].contains(OrigReg)) {
1235 return Error(OrigOp.getStartLoc(),
1236 "mismatching source and destination index registers");
1237 }
1238
1239 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg))
1240 RegClassID = X86::GR64RegClassID;
1241 else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg))
1242 RegClassID = X86::GR32RegClassID;
1243 else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg))
1244 RegClassID = X86::GR16RegClassID;
Marina Yatsina701938d2016-01-20 14:03:47 +00001245 else
Craig Topper5a62f7e2016-02-16 07:28:03 +00001246 // Unexpected register class type
Marina Yatsina701938d2016-01-20 14:03:47 +00001247 // Return false and let a normal complaint about bogus operands happen
1248 return false;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001249
1250 bool IsSI = IsSIReg(FinalReg);
1251 FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI);
1252
1253 if (FinalReg != OrigReg) {
1254 std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI";
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001255 Warnings.push_back(std::make_pair(
1256 OrigOp.getStartLoc(),
1257 "memory operand is only for determining the size, " + RegName +
1258 " will be used for the location"));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001259 }
1260
1261 FinalOp.Mem.Size = OrigOp.Mem.Size;
1262 FinalOp.Mem.SegReg = OrigOp.Mem.SegReg;
1263 FinalOp.Mem.BaseReg = FinalReg;
1264 }
1265 }
1266
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001267 // Produce warnings only if all the operands passed the adjustment - prevent
1268 // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings
Craig Topper16d7eb22016-02-16 07:45:04 +00001269 for (auto &WarningMsg : Warnings) {
1270 Warning(WarningMsg.first, WarningMsg.second);
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001271 }
1272
1273 // Remove old operands
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001274 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1275 OrigOperands.pop_back();
1276 }
1277 // OrigOperands.append(FinalOperands.begin(), FinalOperands.end());
1278 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1279 OrigOperands.push_back(std::move(FinalOperands[i]));
1280
1281 return false;
1282}
1283
David Blaikie960ea3f2014-06-08 16:18:35 +00001284std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001285 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001286 return ParseIntelOperand();
1287 return ParseATTOperand();
1288}
1289
David Blaikie960ea3f2014-06-08 16:18:35 +00001290std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
1291 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
1292 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
Coby Tayreeef66b3b2017-09-10 12:21:24 +00001293 const InlineAsmIdentifierInfo &Info) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001294 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
1295 // some other label reference.
Coby Tayreec3d24112017-09-29 07:02:46 +00001296 if (Info.isKind(InlineAsmIdentifierInfo::IK_Label)) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001297 // Insert an explicit size if the user didn't have one.
1298 if (!Size) {
1299 Size = getPointerWidth();
Craig Topper7d5b2312015-10-10 05:25:02 +00001300 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
1301 /*Len=*/0, Size);
Reid Kleckner5b37c182014-08-01 20:21:24 +00001302 }
Reid Kleckner5b37c182014-08-01 20:21:24 +00001303 // Create an absolute memory reference in order to match against
1304 // instructions taking a PC relative operand.
Craig Topper055845f2015-01-02 07:02:25 +00001305 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size,
Coby Tayreec3d24112017-09-29 07:02:46 +00001306 Identifier, Info.Label.Decl);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001307 }
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001308 // We either have a direct symbol reference, or an offset from a symbol. The
1309 // parser always puts the symbol on the LHS, so look there for size
1310 // calculation purposes.
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00001311 unsigned FrontendSize = 0;
Coby Tayreec3d24112017-09-29 07:02:46 +00001312 void *Decl = nullptr;
1313 bool IsGlobalLV = false;
1314 if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
1315 // Size is in terms of bits in this context.
1316 FrontendSize = Info.Var.Type * 8;
1317 Decl = Info.Var.Decl;
1318 IsGlobalLV = Info.Var.IsGlobalLV;
1319 }
1320 // It is widely common for MS InlineAsm to use a global variable and one/two
1321 // registers in a mmory expression, and though unaccessible via rip/eip.
1322 if (IsGlobalLV && (BaseReg || IndexReg)) {
1323 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End);
1324 // Otherwise, we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001325 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001326 // get the matching correct in some cases.
Coby Tayreec3d24112017-09-29 07:02:46 +00001327 } else {
1328 BaseReg = BaseReg ? BaseReg : 1;
1329 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1330 IndexReg, Scale, Start, End, Size, Identifier,
1331 Decl, FrontendSize);
1332 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001333}
1334
Coby Tayree2cb497a2017-04-04 14:43:23 +00001335// Some binary bitwise operators have a named synonymous
1336// Query a candidate string for being such a named operator
1337// and if so - invoke the appropriate handler
1338bool X86AsmParser::ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM) {
1339 // A named operator should be either lower or upper case, but not a mix
1340 if (Name.compare(Name.lower()) && Name.compare(Name.upper()))
1341 return false;
1342 if (Name.equals_lower("not"))
1343 SM.onNot();
1344 else if (Name.equals_lower("or"))
1345 SM.onOr();
1346 else if (Name.equals_lower("shl"))
1347 SM.onLShift();
1348 else if (Name.equals_lower("shr"))
1349 SM.onRShift();
1350 else if (Name.equals_lower("xor"))
1351 SM.onXor();
1352 else if (Name.equals_lower("and"))
1353 SM.onAnd();
Coby Tayree41a5b552017-06-27 16:58:27 +00001354 else if (Name.equals_lower("mod"))
1355 SM.onMod();
Coby Tayree2cb497a2017-04-04 14:43:23 +00001356 else
1357 return false;
1358 return true;
1359}
1360
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001361bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001362 MCAsmParser &Parser = getParser();
Chad Rosier6844ea02012-10-24 22:13:37 +00001363 const AsmToken &Tok = Parser.getTok();
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001364 StringRef ErrMsg;
Chad Rosier51afe632012-06-27 22:34:28 +00001365
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001366 AsmToken::TokenKind PrevTK = AsmToken::Error;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001367 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001368 while (!Done) {
1369 bool UpdateLocLex = true;
Andrew V. Tischenkofdb264e2017-05-26 13:23:34 +00001370 AsmToken::TokenKind TK = getLexer().getKind();
Chad Rosier5c118fd2013-01-14 22:31:35 +00001371
David Majnemer6a5b8122014-06-19 01:25:43 +00001372 switch (TK) {
Coby Tayreed8912892017-08-24 08:46:25 +00001373 default:
1374 if ((Done = SM.isValidEndState()))
Chad Rosier5c118fd2013-01-14 22:31:35 +00001375 break;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001376 return Error(Tok.getLoc(), "unknown token in expression");
Coby Tayreed8912892017-08-24 08:46:25 +00001377 case AsmToken::EndOfStatement:
Chad Rosierbfb70992013-04-17 00:11:46 +00001378 Done = true;
1379 break;
Coby Tayreed8912892017-08-24 08:46:25 +00001380 case AsmToken::Real:
1381 // DotOperator: [ebx].0
1382 UpdateLocLex = false;
1383 if (ParseIntelDotOperator(SM, End))
1384 return true;
1385 break;
Nico Weber73a699e2018-03-12 12:47:27 +00001386 case AsmToken::At:
David Majnemer6a5b8122014-06-19 01:25:43 +00001387 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001388 case AsmToken::Identifier: {
Chad Rosier152749c2013-04-12 18:54:20 +00001389 SMLoc IdentLoc = Tok.getLoc();
1390 StringRef Identifier = Tok.getString();
Coby Tayree07a89742017-03-21 19:31:55 +00001391 UpdateLocLex = false;
Coby Tayreec3d24112017-09-29 07:02:46 +00001392 // Register
1393 unsigned Reg;
Nico Weber73a699e2018-03-12 12:47:27 +00001394 if (Tok.is(AsmToken::Identifier) && !ParseRegister(Reg, IdentLoc, End)) {
Coby Tayreec3d24112017-09-29 07:02:46 +00001395 if (SM.onRegister(Reg, ErrMsg))
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001396 return Error(Tok.getLoc(), ErrMsg);
Coby Tayreec3d24112017-09-29 07:02:46 +00001397 break;
1398 }
1399 // Operator synonymous ("not", "or" etc.)
1400 if ((UpdateLocLex = ParseIntelNamedOperator(Identifier, SM)))
1401 break;
1402 // Symbol reference, when parsing assembly content
1403 InlineAsmIdentifierInfo Info;
1404 const MCExpr *Val;
1405 if (!isParsingInlineAsm()) {
1406 if (getParser().parsePrimaryExpr(Val, End)) {
Coby Tayree07a89742017-03-21 19:31:55 +00001407 return Error(Tok.getLoc(), "Unexpected identifier!");
Coby Tayreec3d24112017-09-29 07:02:46 +00001408 } else if (SM.onIdentifierExpr(Val, Identifier, Info, false, ErrMsg)) {
Coby Tayreed8912892017-08-24 08:46:25 +00001409 return Error(IdentLoc, ErrMsg);
Coby Tayreec3d24112017-09-29 07:02:46 +00001410 } else
1411 break;
1412 }
1413 // MS InlineAsm operators (TYPE/LENGTH/SIZE)
1414 if (unsigned OpKind = IdentifyIntelInlineAsmOperator(Identifier)) {
Coby Tayreed8912892017-08-24 08:46:25 +00001415 if (OpKind == IOK_OFFSET)
Coby Tayree07a89742017-03-21 19:31:55 +00001416 return Error(IdentLoc, "Dealing OFFSET operator as part of"
1417 "a compound immediate expression is yet to be supported");
Coby Tayreec3d24112017-09-29 07:02:46 +00001418 if (int64_t Val = ParseIntelInlineAsmOperator(OpKind)) {
1419 if (SM.onInteger(Val, ErrMsg))
1420 return Error(IdentLoc, ErrMsg);
1421 } else
Coby Tayree07a89742017-03-21 19:31:55 +00001422 return true;
Coby Tayreec3d24112017-09-29 07:02:46 +00001423 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001424 }
Coby Tayreec3d24112017-09-29 07:02:46 +00001425 // MS Dot Operator expression
1426 if (Identifier.count('.') && PrevTK == AsmToken::RBrac) {
1427 if (ParseIntelDotOperator(SM, End))
1428 return true;
1429 break;
1430 }
1431 // MS InlineAsm identifier
Nico Weber73a699e2018-03-12 12:47:27 +00001432 // Call parseIdentifier() to combine @ with the identifier behind it.
1433 if (TK == AsmToken::At && Parser.parseIdentifier(Identifier))
1434 return Error(IdentLoc, "expected identifier");
Coby Tayreec3d24112017-09-29 07:02:46 +00001435 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, false, End))
1436 return true;
1437 else if (SM.onIdentifierExpr(Val, Identifier, Info, true, ErrMsg))
1438 return Error(IdentLoc, ErrMsg);
Coby Tayree07a89742017-03-21 19:31:55 +00001439 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001440 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001441 case AsmToken::Integer: {
Kevin Enderby36eba252013-12-19 23:16:14 +00001442 // Look for 'b' or 'f' following an Integer as a directional label
1443 SMLoc Loc = getTok().getLoc();
1444 int64_t IntVal = getTok().getIntVal();
1445 End = consumeToken();
1446 UpdateLocLex = false;
1447 if (getLexer().getKind() == AsmToken::Identifier) {
1448 StringRef IDVal = getTok().getString();
1449 if (IDVal == "f" || IDVal == "b") {
1450 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +00001451 getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001452 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Michael Liao5bf95782014-12-04 05:20:33 +00001453 const MCExpr *Val =
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001454 MCSymbolRefExpr::create(Sym, Variant, getContext());
Kevin Enderby36eba252013-12-19 23:16:14 +00001455 if (IDVal == "b" && Sym->isUndefined())
1456 return Error(Loc, "invalid reference to undefined symbol");
1457 StringRef Identifier = Sym->getName();
Coby Tayreec3d24112017-09-29 07:02:46 +00001458 InlineAsmIdentifierInfo Info;
1459 if (SM.onIdentifierExpr(Val, Identifier, Info,
1460 isParsingInlineAsm(), ErrMsg))
Coby Tayreed8912892017-08-24 08:46:25 +00001461 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001462 End = consumeToken();
1463 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001464 if (SM.onInteger(IntVal, ErrMsg))
1465 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001466 }
1467 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001468 if (SM.onInteger(IntVal, ErrMsg))
1469 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001470 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001471 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001472 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001473 case AsmToken::Plus:
1474 if (SM.onPlus(ErrMsg))
1475 return Error(getTok().getLoc(), ErrMsg);
1476 break;
1477 case AsmToken::Minus:
1478 if (SM.onMinus(ErrMsg))
1479 return Error(getTok().getLoc(), ErrMsg);
1480 break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001481 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001482 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001483 case AsmToken::Slash: SM.onDivide(); break;
Reid Kleckner39970062017-10-31 16:47:38 +00001484 case AsmToken::Percent: SM.onMod(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001485 case AsmToken::Pipe: SM.onOr(); break;
Michael Kupersteine3de07a2015-06-14 12:59:45 +00001486 case AsmToken::Caret: SM.onXor(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001487 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001488 case AsmToken::LessLess:
1489 SM.onLShift(); break;
1490 case AsmToken::GreaterGreater:
1491 SM.onRShift(); break;
Coby Tayreed8912892017-08-24 08:46:25 +00001492 case AsmToken::LBrac:
1493 if (SM.onLBrac())
1494 return Error(Tok.getLoc(), "unexpected bracket encountered");
1495 break;
1496 case AsmToken::RBrac:
1497 if (SM.onRBrac())
1498 return Error(Tok.getLoc(), "unexpected bracket encountered");
1499 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001500 case AsmToken::LParen: SM.onLParen(); break;
1501 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001502 }
Chad Rosier31246272013-04-17 21:01:45 +00001503 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001504 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001505
Alp Tokera5b88a52013-12-02 16:06:06 +00001506 if (!Done && UpdateLocLex)
1507 End = consumeToken();
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001508
1509 PrevTK = TK;
Devang Patel41b9dde2012-01-17 18:00:18 +00001510 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001511 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001512}
1513
Coby Tayreed8912892017-08-24 08:46:25 +00001514void X86AsmParser::RewriteIntelExpression(IntelExprStateMachine &SM,
1515 SMLoc Start, SMLoc End) {
1516 SMLoc Loc = Start;
1517 unsigned ExprLen = End.getPointer() - Start.getPointer();
1518 // Skip everything before a symbol displacement (if we have one)
1519 if (SM.getSym()) {
1520 StringRef SymName = SM.getSymName();
1521 if (unsigned Len = SymName.data() - Start.getPointer())
1522 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Start, Len);
1523 Loc = SMLoc::getFromPointer(SymName.data() + SymName.size());
1524 ExprLen = End.getPointer() - (SymName.data() + SymName.size());
1525 // If we have only a symbol than there's no need for complex rewrite,
1526 // simply skip everything after it
1527 if (!(SM.getBaseReg() || SM.getIndexReg() || SM.getImm())) {
1528 if (ExprLen)
1529 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Loc, ExprLen);
1530 return;
Nirav Dave8601ac12016-08-02 17:56:03 +00001531 }
1532 }
Coby Tayreed8912892017-08-24 08:46:25 +00001533 // Build an Intel Expression rewrite
1534 StringRef BaseRegStr;
1535 StringRef IndexRegStr;
1536 if (SM.getBaseReg())
1537 BaseRegStr = X86IntelInstPrinter::getRegisterName(SM.getBaseReg());
1538 if (SM.getIndexReg())
1539 IndexRegStr = X86IntelInstPrinter::getRegisterName(SM.getIndexReg());
1540 // Emit it
1541 IntelExpr Expr(BaseRegStr, IndexRegStr, SM.getScale(), SM.getImm(), SM.isMemExpr());
1542 InstInfo->AsmRewrites->emplace_back(Loc, ExprLen, Expr);
Devang Patel41b9dde2012-01-17 18:00:18 +00001543}
1544
Chad Rosier8a244662013-04-02 20:02:33 +00001545// Inline assembly may use variable names with namespace alias qualifiers.
Coby Tayreed8912892017-08-24 08:46:25 +00001546bool X86AsmParser::ParseIntelInlineAsmIdentifier(const MCExpr *&Val,
1547 StringRef &Identifier,
1548 InlineAsmIdentifierInfo &Info,
1549 bool IsUnevaluatedOperand,
1550 SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001551 MCAsmParser &Parser = getParser();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001552 assert(isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001553 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001554
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001555 StringRef LineBuf(Identifier.data());
Coby Tayreec3d24112017-09-29 07:02:46 +00001556 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001557
Chad Rosier8a244662013-04-02 20:02:33 +00001558 const AsmToken &Tok = Parser.getTok();
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001559 SMLoc Loc = Tok.getLoc();
John McCallf73981b2013-05-03 00:15:41 +00001560
1561 // Advance the token stream until the end of the current token is
1562 // after the end of what the frontend claimed.
1563 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001564 do {
John McCallf73981b2013-05-03 00:15:41 +00001565 End = Tok.getEndLoc();
1566 getLexer().Lex();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001567 } while (End.getPointer() < EndPtr);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001568 Identifier = LineBuf;
1569
Reid Klecknerc2b92542015-08-26 21:57:25 +00001570 // The frontend should end parsing on an assembler token boundary, unless it
1571 // failed parsing.
Coby Tayreec3d24112017-09-29 07:02:46 +00001572 assert((End.getPointer() == EndPtr ||
1573 Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) &&
1574 "frontend claimed part of a token?");
Reid Klecknerc2b92542015-08-26 21:57:25 +00001575
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001576 // If the identifier lookup was unsuccessful, assume that we are dealing with
1577 // a label.
Coby Tayreec3d24112017-09-29 07:02:46 +00001578 if (Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) {
Ehsan Akhgaribb6bb072014-09-22 20:40:36 +00001579 StringRef InternalName =
1580 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(),
1581 Loc, false);
1582 assert(InternalName.size() && "We should have an internal name here.");
1583 // Push a rewrite for replacing the identifier name with the internal name.
Craig Topper7d5b2312015-10-10 05:25:02 +00001584 InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(),
1585 InternalName);
Coby Tayreec3d24112017-09-29 07:02:46 +00001586 } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
1587 return false;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001588 // Create the symbol reference.
Jim Grosbach6f482002015-05-18 18:43:14 +00001589 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Chad Rosier8a244662013-04-02 20:02:33 +00001590 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001591 Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001592 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001593}
1594
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001595//ParseRoundingModeOp - Parse AVX-512 rounding mode operand
1596std::unique_ptr<X86Operand>
Craig Topper36d8da32018-01-06 06:41:07 +00001597X86AsmParser::ParseRoundingModeOp(SMLoc Start) {
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001598 MCAsmParser &Parser = getParser();
1599 const AsmToken &Tok = Parser.getTok();
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001600 // Eat "{" and mark the current place.
1601 const SMLoc consumedToken = consumeToken();
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001602 if (Tok.getIdentifier().startswith("r")){
1603 int rndMode = StringSwitch<int>(Tok.getIdentifier())
1604 .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT)
1605 .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF)
1606 .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF)
1607 .Case("rz", X86::STATIC_ROUNDING::TO_ZERO)
1608 .Default(-1);
1609 if (-1 == rndMode)
1610 return ErrorOperand(Tok.getLoc(), "Invalid rounding mode.");
1611 Parser.Lex(); // Eat "r*" of r*-sae
1612 if (!getLexer().is(AsmToken::Minus))
1613 return ErrorOperand(Tok.getLoc(), "Expected - at this point");
1614 Parser.Lex(); // Eat "-"
1615 Parser.Lex(); // Eat the sae
1616 if (!getLexer().is(AsmToken::RCurly))
1617 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
Craig Topper36d8da32018-01-06 06:41:07 +00001618 SMLoc End = Tok.getEndLoc();
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001619 Parser.Lex(); // Eat "}"
1620 const MCExpr *RndModeOp =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001621 MCConstantExpr::create(rndMode, Parser.getContext());
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001622 return X86Operand::CreateImm(RndModeOp, Start, End);
1623 }
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001624 if(Tok.getIdentifier().equals("sae")){
1625 Parser.Lex(); // Eat the sae
1626 if (!getLexer().is(AsmToken::RCurly))
1627 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1628 Parser.Lex(); // Eat "}"
1629 return X86Operand::CreateToken("{sae}", consumedToken);
1630 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001631 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
1632}
Chad Rosier91c82662012-10-24 17:22:29 +00001633
Chad Rosier5dcb4662012-10-24 22:21:50 +00001634/// Parse the '.' operator.
Coby Tayreed8912892017-08-24 08:46:25 +00001635bool X86AsmParser::ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End) {
1636 const AsmToken &Tok = getTok();
1637 unsigned Offset;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001638
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001639 // Drop the optional '.'.
1640 StringRef DotDispStr = Tok.getString();
1641 if (DotDispStr.startswith("."))
1642 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001643
Chad Rosier5dcb4662012-10-24 22:21:50 +00001644 // .Imm gets lexed as a real.
1645 if (Tok.is(AsmToken::Real)) {
1646 APInt DotDisp;
1647 DotDispStr.getAsInteger(10, DotDisp);
Coby Tayreed8912892017-08-24 08:46:25 +00001648 Offset = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001649 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001650 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1651 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Coby Tayreed8912892017-08-24 08:46:25 +00001652 Offset))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001653 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosiercc541e82013-04-19 15:57:00 +00001654 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001655 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001656
Coby Tayreed8912892017-08-24 08:46:25 +00001657 // Eat the DotExpression and update End
1658 End = SMLoc::getFromPointer(DotDispStr.data());
1659 const char *DotExprEndLoc = DotDispStr.data() + DotDispStr.size();
1660 while (Tok.getLoc().getPointer() < DotExprEndLoc)
1661 Lex();
1662 SM.addImm(Offset);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001663 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001664}
1665
Chad Rosier91c82662012-10-24 17:22:29 +00001666/// Parse the 'offset' operator. This operator is used to specify the
1667/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001668std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001669 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001670 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001671 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001672 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001673
Chad Rosier91c82662012-10-24 17:22:29 +00001674 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001675 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001676 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001677 StringRef Identifier = Tok.getString();
Coby Tayreed8912892017-08-24 08:46:25 +00001678 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info,
1679 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001680 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001681
Coby Tayreec3d24112017-09-29 07:02:46 +00001682 void *Decl = nullptr;
1683 // FIXME: MS evaluates "offset <Constant>" to the underlying integral
1684 if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
1685 return ErrorOperand(Start, "offset operator cannot yet handle constants");
1686 else if (Info.isKind(InlineAsmIdentifierInfo::IK_Var))
1687 Decl = Info.Var.Decl;
Chad Rosiere2f03772012-10-26 16:09:20 +00001688 // Don't emit the offset operator.
Craig Topper7d5b2312015-10-10 05:25:02 +00001689 InstInfo->AsmRewrites->emplace_back(AOK_Skip, OffsetOfLoc, 7);
Chad Rosiere2f03772012-10-26 16:09:20 +00001690
Chad Rosier91c82662012-10-24 17:22:29 +00001691 // The offset operator will have an 'r' constraint, thus we need to create
1692 // register operand to ensure proper matching. Just pick a GPR based on
1693 // the size of a pointer.
Nirav Dave6477ce22016-09-26 19:33:36 +00001694 bool Parse32 = is32BitMode() || Code16GCC;
1695 unsigned RegNo = is64BitMode() ? X86::RBX : (Parse32 ? X86::EBX : X86::BX);
1696
Chad Rosiera4bc9432013-01-10 22:10:27 +00001697 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Coby Tayreec3d24112017-09-29 07:02:46 +00001698 OffsetOfLoc, Identifier, Decl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001699}
1700
Coby Tayree07a89742017-03-21 19:31:55 +00001701// Query a candidate string for being an Intel assembly operator
1702// Report back its kind, or IOK_INVALID if does not evaluated as a known one
Coby Tayreed8912892017-08-24 08:46:25 +00001703unsigned X86AsmParser::IdentifyIntelInlineAsmOperator(StringRef Name) {
Coby Tayree07a89742017-03-21 19:31:55 +00001704 return StringSwitch<unsigned>(Name)
1705 .Cases("TYPE","type",IOK_TYPE)
1706 .Cases("SIZE","size",IOK_SIZE)
1707 .Cases("LENGTH","length",IOK_LENGTH)
1708 .Cases("OFFSET","offset",IOK_OFFSET)
1709 .Default(IOK_INVALID);
1710}
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001711
1712/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1713/// returns the number of elements in an array. It returns the value 1 for
1714/// non-array variables. The SIZE operator returns the size of a C or C++
1715/// variable. A variable's size is the product of its LENGTH and TYPE. The
1716/// TYPE operator returns the size of a C or C++ type or variable. If the
1717/// variable is an array, TYPE returns the size of a single element.
Coby Tayreed8912892017-08-24 08:46:25 +00001718unsigned X86AsmParser::ParseIntelInlineAsmOperator(unsigned OpKind) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001719 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001720 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001721 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001722
Craig Topper062a2ba2014-04-25 05:30:21 +00001723 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001724 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001725 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001726 StringRef Identifier = Tok.getString();
Coby Tayreed8912892017-08-24 08:46:25 +00001727 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info,
1728 /*Unevaluated=*/true, End))
Coby Tayree07a89742017-03-21 19:31:55 +00001729 return 0;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001730
Coby Tayreec3d24112017-09-29 07:02:46 +00001731 if (!Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
Coby Tayree07a89742017-03-21 19:31:55 +00001732 Error(Start, "unable to lookup expression");
1733 return 0;
1734 }
Coby Tayreed8912892017-08-24 08:46:25 +00001735
Chad Rosierf6675c32013-04-22 17:01:46 +00001736 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001737 switch(OpKind) {
1738 default: llvm_unreachable("Unexpected operand kind!");
Coby Tayreec3d24112017-09-29 07:02:46 +00001739 case IOK_LENGTH: CVal = Info.Var.Length; break;
1740 case IOK_SIZE: CVal = Info.Var.Size; break;
1741 case IOK_TYPE: CVal = Info.Var.Type; break;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001742 }
Eric Christopheradfe5362017-07-25 19:22:09 +00001743
Coby Tayree07a89742017-03-21 19:31:55 +00001744 return CVal;
Chad Rosier11c42f22012-10-26 18:04:20 +00001745}
1746
Coby Tayreed8912892017-08-24 08:46:25 +00001747bool X86AsmParser::ParseIntelMemoryOperandSize(unsigned &Size) {
1748 Size = StringSwitch<unsigned>(getTok().getString())
1749 .Cases("BYTE", "byte", 8)
1750 .Cases("WORD", "word", 16)
1751 .Cases("DWORD", "dword", 32)
Coby Tayree566348f2017-09-28 11:04:08 +00001752 .Cases("FLOAT", "float", 32)
1753 .Cases("LONG", "long", 32)
Coby Tayreed8912892017-08-24 08:46:25 +00001754 .Cases("FWORD", "fword", 48)
Coby Tayree566348f2017-09-28 11:04:08 +00001755 .Cases("DOUBLE", "double", 64)
Coby Tayreed8912892017-08-24 08:46:25 +00001756 .Cases("QWORD", "qword", 64)
1757 .Cases("MMWORD","mmword", 64)
1758 .Cases("XWORD", "xword", 80)
1759 .Cases("TBYTE", "tbyte", 80)
1760 .Cases("XMMWORD", "xmmword", 128)
1761 .Cases("YMMWORD", "ymmword", 256)
1762 .Cases("ZMMWORD", "zmmword", 512)
1763 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
1764 .Default(0);
1765 if (Size) {
1766 const AsmToken &Tok = Lex(); // Eat operand size (e.g., byte, word).
1767 if (!(Tok.getString().equals("PTR") || Tok.getString().equals("ptr")))
1768 return Error(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
1769 Lex(); // Eat ptr.
1770 }
1771 return false;
1772}
1773
David Blaikie960ea3f2014-06-08 16:18:35 +00001774std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001775 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001776 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001777 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001778
Coby Tayree07a89742017-03-21 19:31:55 +00001779 // FIXME: Offset operator
1780 // Should be handled as part of immediate expression, as other operators
1781 // Currently, only supported as a stand-alone operand
1782 if (isParsingInlineAsm())
Coby Tayreed8912892017-08-24 08:46:25 +00001783 if (IdentifyIntelInlineAsmOperator(Tok.getString()) == IOK_OFFSET)
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001784 return ParseIntelOffsetOfOperator();
Chad Rosier11c42f22012-10-26 18:04:20 +00001785
Coby Tayreed8912892017-08-24 08:46:25 +00001786 // Parse optional Size directive.
1787 unsigned Size;
1788 if (ParseIntelMemoryOperandSize(Size))
1789 return nullptr;
1790 bool PtrInOperand = bool(Size);
Nirav Dave8601ac12016-08-02 17:56:03 +00001791
David Majnemeraa34d792013-08-27 21:56:17 +00001792 Start = Tok.getLoc();
1793
Coby Tayreed8912892017-08-24 08:46:25 +00001794 // Rounding mode operand.
Craig Toppere538fc72018-02-02 17:02:58 +00001795 if (getLexer().is(AsmToken::LCurly))
Craig Topper36d8da32018-01-06 06:41:07 +00001796 return ParseRoundingModeOp(Start);
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001797
Coby Tayreed8912892017-08-24 08:46:25 +00001798 // Register operand.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001799 unsigned RegNo = 0;
Coby Tayreed8912892017-08-24 08:46:25 +00001800 if (Tok.is(AsmToken::Identifier) && !ParseRegister(RegNo, Start, End)) {
Douglas Katzman0411e862016-10-05 15:23:35 +00001801 if (RegNo == X86::RIP)
1802 return ErrorOperand(Start, "rip can only be used as a base register");
Coby Tayreed8912892017-08-24 08:46:25 +00001803 // A Register followed by ':' is considered a segment override
1804 if (Tok.isNot(AsmToken::Colon))
1805 return !PtrInOperand ? X86Operand::CreateReg(RegNo, Start, End) :
1806 ErrorOperand(Start, "expected memory operand after 'ptr', "
1807 "found register operand instead");
1808 // An alleged segment override. check if we have a valid segment register
1809 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1810 return ErrorOperand(Start, "invalid segment register");
1811 // Eat ':' and update Start location
1812 Start = Lex().getLoc();
Devang Patel46831de2012-01-12 01:36:43 +00001813 }
1814
Nirav Dave8601ac12016-08-02 17:56:03 +00001815 // Immediates and Memory
Coby Tayreed8912892017-08-24 08:46:25 +00001816 IntelExprStateMachine SM;
Nirav Dave8601ac12016-08-02 17:56:03 +00001817 if (ParseIntelExpression(SM, End))
1818 return nullptr;
1819
Coby Tayreed8912892017-08-24 08:46:25 +00001820 if (isParsingInlineAsm())
1821 RewriteIntelExpression(SM, Start, Tok.getLoc());
1822
Nirav Dave8601ac12016-08-02 17:56:03 +00001823 int64_t Imm = SM.getImm();
Coby Tayreed8912892017-08-24 08:46:25 +00001824 const MCExpr *Disp = SM.getSym();
1825 const MCExpr *ImmDisp = MCConstantExpr::create(Imm, getContext());
1826 if (Disp && Imm)
1827 Disp = MCBinaryExpr::createAdd(Disp, ImmDisp, getContext());
1828 if (!Disp)
1829 Disp = ImmDisp;
Nirav Dave8601ac12016-08-02 17:56:03 +00001830
Coby Tayreed8912892017-08-24 08:46:25 +00001831 // RegNo != 0 specifies a valid segment register,
1832 // and we are parsing a segment override
1833 if (!SM.isMemExpr() && !RegNo)
1834 return X86Operand::CreateImm(Disp, Start, End);
Nirav Dave8601ac12016-08-02 17:56:03 +00001835
Coby Tayreed8912892017-08-24 08:46:25 +00001836 StringRef ErrMsg;
1837 unsigned BaseReg = SM.getBaseReg();
1838 unsigned IndexReg = SM.getIndexReg();
1839 unsigned Scale = SM.getScale();
Nirav Dave8601ac12016-08-02 17:56:03 +00001840
Coby Tayreed8912892017-08-24 08:46:25 +00001841 if ((BaseReg || IndexReg) &&
1842 CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, ErrMsg))
1843 return ErrorOperand(Start, ErrMsg);
1844 if (isParsingInlineAsm())
1845 return CreateMemForInlineAsm(RegNo, Disp, BaseReg, IndexReg,
1846 Scale, Start, End, Size, SM.getSymName(),
1847 SM.getIdentifierInfo());
1848 if (!(BaseReg || IndexReg || RegNo))
1849 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size);
1850 return X86Operand::CreateMem(getPointerWidth(), RegNo, Disp,
1851 BaseReg, IndexReg, Scale, Start, End, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001852}
1853
David Blaikie960ea3f2014-06-08 16:18:35 +00001854std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001855 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001856 switch (getLexer().getKind()) {
1857 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001858 // Parse a memory operand with no segment register.
1859 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001860 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001861 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001862 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001863 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001864 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001865 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001866 Error(Start, "%eiz and %riz can only be used as index registers",
1867 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001868 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001869 }
Douglas Katzman0411e862016-10-05 15:23:35 +00001870 if (RegNo == X86::RIP) {
1871 Error(Start, "%rip can only be used as a base register",
1872 SMRange(Start, End));
1873 return nullptr;
1874 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001875
Chris Lattnerb9270732010-04-17 18:56:34 +00001876 // If this is a segment register followed by a ':', then this is the start
1877 // of a memory reference, otherwise this is a normal register reference.
1878 if (getLexer().isNot(AsmToken::Colon))
1879 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001880
Reid Kleckner0c5da972014-07-31 23:03:22 +00001881 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1882 return ErrorOperand(Start, "invalid segment register");
1883
Chris Lattnerb9270732010-04-17 18:56:34 +00001884 getParser().Lex(); // Eat the colon.
1885 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001886 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001887 case AsmToken::Dollar: {
1888 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001889 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001890 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001891 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001892 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001893 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001894 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001895 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001896 case AsmToken::LCurly:{
Craig Topper36d8da32018-01-06 06:41:07 +00001897 SMLoc Start = Parser.getTok().getLoc();
Craig Toppere538fc72018-02-02 17:02:58 +00001898 return ParseRoundingModeOp(Start);
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001899 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001900 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001901}
1902
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001903// true on failure, false otherwise
1904// If no {z} mark was found - Parser doesn't advance
1905bool X86AsmParser::ParseZ(std::unique_ptr<X86Operand> &Z,
1906 const SMLoc &StartLoc) {
1907 MCAsmParser &Parser = getParser();
1908 // Assuming we are just pass the '{' mark, quering the next token
Coby Tayree179ff0e2016-11-20 09:31:11 +00001909 // Searched for {z}, but none was found. Return false, as no parsing error was
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001910 // encountered
1911 if (!(getLexer().is(AsmToken::Identifier) &&
1912 (getLexer().getTok().getIdentifier() == "z")))
1913 return false;
1914 Parser.Lex(); // Eat z
1915 // Query and eat the '}' mark
1916 if (!getLexer().is(AsmToken::RCurly))
1917 return Error(getLexer().getLoc(), "Expected } at this point");
1918 Parser.Lex(); // Eat '}'
1919 // Assign Z with the {z} mark opernad
Benjamin Kramerfc54e352016-11-24 15:17:39 +00001920 Z = X86Operand::CreateToken("{z}", StartLoc);
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001921 return false;
1922}
1923
1924// true on failure, false otherwise
David Blaikie960ea3f2014-06-08 16:18:35 +00001925bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1926 const MCParsedAsmOperand &Op) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001927 MCAsmParser &Parser = getParser();
Craig Toppere538fc72018-02-02 17:02:58 +00001928 if (getLexer().is(AsmToken::LCurly)) {
1929 // Eat "{" and mark the current place.
1930 const SMLoc consumedToken = consumeToken();
1931 // Distinguish {1to<NUM>} from {%k<NUM>}.
1932 if(getLexer().is(AsmToken::Integer)) {
1933 // Parse memory broadcasting ({1to<NUM>}).
1934 if (getLexer().getTok().getIntVal() != 1)
1935 return TokError("Expected 1to<NUM> at this point");
1936 Parser.Lex(); // Eat "1" of 1to8
1937 if (!getLexer().is(AsmToken::Identifier) ||
1938 !getLexer().getTok().getIdentifier().startswith("to"))
1939 return TokError("Expected 1to<NUM> at this point");
1940 // Recognize only reasonable suffixes.
1941 const char *BroadcastPrimitive =
1942 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
1943 .Case("to2", "{1to2}")
1944 .Case("to4", "{1to4}")
1945 .Case("to8", "{1to8}")
1946 .Case("to16", "{1to16}")
1947 .Default(nullptr);
1948 if (!BroadcastPrimitive)
1949 return TokError("Invalid memory broadcast primitive.");
1950 Parser.Lex(); // Eat "toN" of 1toN
1951 if (!getLexer().is(AsmToken::RCurly))
1952 return TokError("Expected } at this point");
1953 Parser.Lex(); // Eat "}"
1954 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1955 consumedToken));
1956 // No AVX512 specific primitives can pass
1957 // after memory broadcasting, so return.
1958 return false;
1959 } else {
1960 // Parse either {k}{z}, {z}{k}, {k} or {z}
1961 // last one have no meaning, but GCC accepts it
1962 // Currently, we're just pass a '{' mark
1963 std::unique_ptr<X86Operand> Z;
1964 if (ParseZ(Z, consumedToken))
1965 return true;
1966 // Reaching here means that parsing of the allegadly '{z}' mark yielded
1967 // no errors.
1968 // Query for the need of further parsing for a {%k<NUM>} mark
1969 if (!Z || getLexer().is(AsmToken::LCurly)) {
1970 SMLoc StartLoc = Z ? consumeToken() : consumedToken;
1971 // Parse an op-mask register mark ({%k<NUM>}), which is now to be
1972 // expected
1973 unsigned RegNo;
1974 SMLoc RegLoc;
1975 if (!ParseRegister(RegNo, RegLoc, StartLoc) &&
1976 X86MCRegisterClasses[X86::VK1RegClassID].contains(RegNo)) {
1977 if (RegNo == X86::K0)
1978 return Error(RegLoc, "Register k0 can't be used as write mask");
1979 if (!getLexer().is(AsmToken::RCurly))
1980 return Error(getLexer().getLoc(), "Expected } at this point");
1981 Operands.push_back(X86Operand::CreateToken("{", StartLoc));
1982 Operands.push_back(
1983 X86Operand::CreateReg(RegNo, StartLoc, StartLoc));
1984 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1985 } else
1986 return Error(getLexer().getLoc(),
1987 "Expected an op-mask register at this point");
1988 // {%k<NUM>} mark is found, inquire for {z}
1989 if (getLexer().is(AsmToken::LCurly) && !Z) {
1990 // Have we've found a parsing error, or found no (expected) {z} mark
1991 // - report an error
1992 if (ParseZ(Z, consumeToken()) || !Z)
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001993 return Error(getLexer().getLoc(),
Craig Toppere538fc72018-02-02 17:02:58 +00001994 "Expected a {z} mark at this point");
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001995
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001996 }
Craig Toppere538fc72018-02-02 17:02:58 +00001997 // '{z}' on its own is meaningless, hence should be ignored.
1998 // on the contrary - have it been accompanied by a K register,
1999 // allow it.
2000 if (Z)
2001 Operands.push_back(std::move(Z));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002002 }
2003 }
2004 }
Michael Zuckerman1bee6342016-10-18 13:52:39 +00002005 return false;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002006}
2007
Chris Lattnerb9270732010-04-17 18:56:34 +00002008/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
2009/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00002010std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
2011 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002012
Rafael Espindola961d4692014-11-11 05:18:41 +00002013 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002014 // We have to disambiguate a parenthesized expression "(4+5)" from the start
2015 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00002016 // only way to do this without lookahead is to eat the '(' and see what is
2017 // after it.
Jim Grosbach13760bd2015-05-30 01:25:56 +00002018 const MCExpr *Disp = MCConstantExpr::create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002019 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00002020 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00002021 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002022
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002023 // After parsing the base expression we could either have a parenthesized
2024 // memory address or not. If not, return now. If so, eat the (.
2025 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002026 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002027 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002028 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, ExprEnd);
2029 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2030 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002031 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002032
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002033 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002034 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002035 } else {
2036 // Okay, we have a '('. We don't know if this is an expression or not, but
2037 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00002038 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002039 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002040
Kevin Enderby7d912182009-09-03 17:15:07 +00002041 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002042 // Nothing to do here, fall into the code below with the '(' part of the
2043 // memory operand consumed.
2044 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00002045 SMLoc ExprEnd;
Konstantin Belochapka34777112017-09-22 23:37:48 +00002046 getLexer().UnLex(AsmToken(AsmToken::LParen, "("));
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002047
Konstantin Belochapka34777112017-09-22 23:37:48 +00002048 // It must be either an parenthesized expression, or an expression that
2049 // begins from a parenthesized expression, parse it now. Example: (1+2) or
2050 // (1+2)+3
2051 if (getParser().parseExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00002052 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002053
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002054 // After parsing the base expression we could either have a parenthesized
2055 // memory address or not. If not, return now. If so, eat the (.
2056 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002057 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002058 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002059 return X86Operand::CreateMem(getPointerWidth(), Disp, LParenLoc,
2060 ExprEnd);
2061 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2062 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002063 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002064
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002065 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002066 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002067 }
2068 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002069
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002070 // If we reached here, then we just ate the ( of the memory operand. Process
2071 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00002072 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00002073 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002074
Chris Lattner0c2538f2010-01-15 18:51:29 +00002075 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002076 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00002077 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00002078 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002079 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002080 Error(StartLoc, "eiz and riz can only be used as index registers",
2081 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00002082 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002083 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00002084 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002085
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002086 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002087 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002088 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002089
2090 // Following the comma we should have either an index register, or a scale
2091 // value. We don't support the later form, but we want to parse it
2092 // correctly.
2093 //
2094 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002095 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00002096 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00002097 SMLoc L;
Douglas Katzman0411e862016-10-05 15:23:35 +00002098 if (ParseRegister(IndexReg, L, L))
2099 return nullptr;
2100 if (BaseReg == X86::RIP) {
2101 Error(IndexLoc, "%rip as base register can not have an index register");
2102 return nullptr;
2103 }
2104 if (IndexReg == X86::RIP) {
2105 Error(IndexLoc, "%rip is not allowed as an index register");
2106 return nullptr;
2107 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002108
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002109 if (getLexer().isNot(AsmToken::RParen)) {
2110 // Parse the scale amount:
2111 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002112 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002113 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002114 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002115 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002116 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00002117 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002118
2119 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002120 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002121
2122 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002123 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00002124 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002125 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00002126 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002127
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002128 // Validate the scale amount.
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002129 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
David Woodhouse6dbda442014-01-08 12:58:28 +00002130 ScaleVal != 1) {
2131 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00002132 return nullptr;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002133 }
2134 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 &&
2135 ScaleVal != 8) {
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002136 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00002137 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002138 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002139 Scale = (unsigned)ScaleVal;
2140 }
2141 }
2142 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002143 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002144 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00002145 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002146
2147 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002148 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00002149 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002150
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002151 if (Value != 1)
2152 Warning(Loc, "scale factor without index register is ignored");
2153 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002154 }
2155 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002156
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002157 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002158 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002159 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00002160 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002161 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002162 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002163 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002164
David Woodhouse6dbda442014-01-08 12:58:28 +00002165 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
2166 // and then only in non-64-bit modes. Except for DX, which is a special case
2167 // because an unofficial form of in/out instructions uses it.
2168 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
2169 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
2170 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2171 BaseReg != X86::DX) {
2172 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002173 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002174 }
2175 if (BaseReg == 0 &&
2176 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2177 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002178 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002179 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00002180
2181 StringRef ErrMsg;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00002182 if (CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, ErrMsg)) {
Kevin Enderbybc570f22014-01-23 22:34:42 +00002183 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00002184 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002185 }
2186
Reid Klecknerb7e2f602014-07-31 23:26:35 +00002187 if (SegReg || BaseReg || IndexReg)
Craig Topper055845f2015-01-02 07:02:25 +00002188 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
2189 IndexReg, Scale, MemStart, MemEnd);
2190 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002191}
2192
David Blaikie960ea3f2014-06-08 16:18:35 +00002193bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
2194 SMLoc NameLoc, OperandVector &Operands) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002195 MCAsmParser &Parser = getParser();
Chad Rosierf0e87202012-10-25 20:41:34 +00002196 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002197 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002198
Coby Tayree48d67cd2017-07-30 11:12:47 +00002199 if ((Name.equals("jmp") || Name.equals("jc") || Name.equals("jz")) &&
2200 isParsingIntelSyntax() && isParsingInlineAsm()) {
Michael Zuckerman174d2e72016-10-14 08:09:40 +00002201 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 Dunbare1fdb0e2009-07-28 22:40:46 +00002342
Chris Lattner086a83a2010-09-08 05:17:37 +00002343 // Determine whether this is an instruction prefix.
Coby Tayreec54c5cb2017-08-21 07:50:15 +00002344 // FIXME:
Craig Topper0768bce2017-09-26 21:35:04 +00002345 // Enhance prefixes integrity robustness. for example, following forms
Coby Tayreec54c5cb2017-08-21 07:50:15 +00002346 // are currently tolerated:
2347 // repz repnz <insn> ; GAS errors for the use of two similar prefixes
2348 // lock addq %rax, %rbx ; Destination operand must be of memory type
2349 // xacquire <insn> ; xacquire must be accompanied by 'lock'
2350 bool isPrefix = StringSwitch<bool>(Name)
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002351 .Cases("rex64", "data32", "data16", true)
2352 .Cases("xacquire", "xrelease", true)
2353 .Cases("acquire", "release", isParsingIntelSyntax())
2354 .Default(false);
2355
2356 auto isLockRepeatPrefix = [](StringRef N) {
2357 return StringSwitch<bool>(N)
2358 .Cases("lock", "rep", "repe", "repz", "repne", "repnz", true)
2359 .Default(false);
2360 };
Michael J. Spencer530ce852010-10-09 11:00:50 +00002361
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002362 bool CurlyAsEndOfStatement = false;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002363
2364 unsigned Flags = X86::IP_NO_PREFIX;
2365 while (isLockRepeatPrefix(Name.lower())) {
2366 unsigned Prefix =
2367 StringSwitch<unsigned>(Name)
2368 .Cases("lock", "lock", X86::IP_HAS_LOCK)
2369 .Cases("rep", "repe", "repz", X86::IP_HAS_REPEAT)
2370 .Cases("repne", "repnz", X86::IP_HAS_REPEAT_NE)
2371 .Default(X86::IP_NO_PREFIX); // Invalid prefix (impossible)
2372 Flags |= Prefix;
Andrew V. Tischenkof7706992018-01-17 10:12:06 +00002373 if (getLexer().is(AsmToken::EndOfStatement)) {
2374 // We don't have real instr with the given prefix
2375 // let's use the prefix as the instr.
2376 // TODO: there could be several prefixes one after another
2377 Flags = X86::IP_NO_PREFIX;
2378 break;
2379 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002380 Name = Parser.getTok().getString();
2381 Parser.Lex(); // eat the prefix
Andrew V. Tischenko1dd78562017-12-26 18:29:52 +00002382 // Hack: we could have something like "rep # some comment" or
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002383 // "lock; cmpxchg16b $1" or "lock\0A\09incl" or "lock/incl"
2384 while (Name.startswith(";") || Name.startswith("\n") ||
Andrew V. Tischenko1dd78562017-12-26 18:29:52 +00002385 Name.startswith("#") || Name.startswith("\t") ||
2386 Name.startswith("/")) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002387 Name = Parser.getTok().getString();
2388 Parser.Lex(); // go to next prefix or instr
2389 }
2390 }
2391
2392 if (Flags)
2393 PatchedName = Name;
2394 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
2395
Chris Lattner086a83a2010-09-08 05:17:37 +00002396 // This does the actual operand parsing. Don't parse any more if we have a
2397 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2398 // just want to parse the "lock" as the first instruction and the "incl" as
2399 // the next one.
2400 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002401 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002402 if (getLexer().is(AsmToken::Star))
2403 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002404
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002405 // Read the operands.
Kirill Bobyrev6afbaf02017-01-18 16:34:25 +00002406 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002407 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
2408 Operands.push_back(std::move(Op));
Michael Zuckerman1bee6342016-10-18 13:52:39 +00002409 if (HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00002410 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002411 } else {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002412 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00002413 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002414 // check for comma and eat it
2415 if (getLexer().is(AsmToken::Comma))
2416 Parser.Lex();
2417 else
2418 break;
2419 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00002420
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +00002421 // In MS inline asm curly braces mark the beginning/end of a block,
2422 // therefore they should be interepreted as end of statement
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002423 CurlyAsEndOfStatement =
2424 isParsingIntelSyntax() && isParsingInlineAsm() &&
2425 (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly));
2426 if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement)
Nirav Dave2364748a2016-09-16 18:30:20 +00002427 return TokError("unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002428 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002429
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002430 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002431 if (getLexer().is(AsmToken::EndOfStatement) ||
2432 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002433 Parser.Lex();
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002434 else if (CurlyAsEndOfStatement)
2435 // Add an actual EndOfStatement before the curly brace
2436 Info.AsmRewrites->emplace_back(AOK_EndOfStatement,
2437 getLexer().getTok().getLoc(), 0);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002438
Michael Zuckermanfd3fe9e2015-11-12 16:58:51 +00002439 // This is for gas compatibility and cannot be done in td.
2440 // Adding "p" for some floating point with no argument.
2441 // For example: fsub --> fsubp
2442 bool IsFp =
2443 Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr";
2444 if (IsFp && Operands.size() == 1) {
2445 const char *Repl = StringSwitch<const char *>(Name)
2446 .Case("fsub", "fsubp")
2447 .Case("fdiv", "fdivp")
2448 .Case("fsubr", "fsubrp")
2449 .Case("fdivr", "fdivrp");
2450 static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl);
2451 }
2452
Nirav Davef45fd2b2016-08-08 18:01:04 +00002453 // Moving a 32 or 16 bit value into a segment register has the same
2454 // behavior. Modify such instructions to always take shorter form.
2455 if ((Name == "mov" || Name == "movw" || Name == "movl") &&
2456 (Operands.size() == 3)) {
2457 X86Operand &Op1 = (X86Operand &)*Operands[1];
2458 X86Operand &Op2 = (X86Operand &)*Operands[2];
2459 SMLoc Loc = Op1.getEndLoc();
2460 if (Op1.isReg() && Op2.isReg() &&
2461 X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(
2462 Op2.getReg()) &&
2463 (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) ||
2464 X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) {
2465 // Change instruction name to match new instruction.
2466 if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) {
2467 Name = is16BitMode() ? "movw" : "movl";
2468 Operands[0] = X86Operand::CreateToken(Name, NameLoc);
2469 }
2470 // Select the correct equivalent 16-/32-bit source register.
2471 unsigned Reg =
2472 getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32);
2473 Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc);
2474 }
2475 }
2476
Nirav Dave8e103802016-06-29 19:54:27 +00002477 // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" ->
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002478 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2479 // documented form in various unofficial manuals, so a lot of code uses it.
Nirav Dave8e103802016-06-29 19:54:27 +00002480 if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" ||
2481 Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") &&
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002482 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002483 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002484 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2485 isa<MCConstantExpr>(Op.Mem.Disp) &&
2486 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2487 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2488 SMLoc Loc = Op.getEndLoc();
2489 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002490 }
2491 }
Nirav Dave8e103802016-06-29 19:54:27 +00002492 // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al".
2493 if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" ||
2494 Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") &&
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002495 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002496 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002497 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2498 isa<MCConstantExpr>(Op.Mem.Disp) &&
2499 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2500 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2501 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002502 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002503 }
2504 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002505
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002506 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands;
2507 bool HadVerifyError = false;
2508
David Woodhouse4ce66062014-01-22 15:08:55 +00002509 // Append default arguments to "ins[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002510 if (Name.startswith("ins") &&
2511 (Operands.size() == 1 || Operands.size() == 3) &&
2512 (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" ||
2513 Name == "ins")) {
2514
2515 AddDefaultSrcDestOperands(TmpOperands,
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002516 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc),
2517 DefaultMemDIOperand(NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002518 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002519 }
2520
David Woodhousec472b812014-01-22 15:08:49 +00002521 // Append default arguments to "outs[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002522 if (Name.startswith("outs") &&
2523 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhousec472b812014-01-22 15:08:49 +00002524 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002525 Name == "outsd" || Name == "outs")) {
2526 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002527 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002528 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002529 }
2530
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002531 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2532 // values of $SIREG according to the mode. It would be nice if this
2533 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002534 if (Name.startswith("lods") &&
2535 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002536 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002537 Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) {
2538 TmpOperands.push_back(DefaultMemSIOperand(NameLoc));
2539 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2540 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002541
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002542 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2543 // values of $DIREG according to the mode. It would be nice if this
2544 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002545 if (Name.startswith("stos") &&
2546 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002547 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002548 Name == "stosl" || Name == "stosd" || Name == "stosq")) {
2549 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2550 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2551 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002552
David Woodhouse20fe4802014-01-22 15:08:27 +00002553 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2554 // values of $DIREG according to the mode. It would be nice if this
2555 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002556 if (Name.startswith("scas") &&
2557 (Operands.size() == 1 || Operands.size() == 2) &&
David Woodhouse20fe4802014-01-22 15:08:27 +00002558 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002559 Name == "scasl" || Name == "scasd" || Name == "scasq")) {
2560 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2561 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2562 }
David Woodhouse20fe4802014-01-22 15:08:27 +00002563
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002564 // Add default SI and DI operands to "cmps[bwlq]".
2565 if (Name.startswith("cmps") &&
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002566 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002567 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2568 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002569 AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc),
2570 DefaultMemSIOperand(NameLoc));
2571 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002572 }
2573
David Woodhouse6f417de2014-01-22 15:08:42 +00002574 // Add default SI and DI operands to "movs[bwlq]".
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002575 if (((Name.startswith("movs") &&
2576 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2577 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2578 (Name.startswith("smov") &&
2579 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2580 Name == "smovl" || Name == "smovd" || Name == "smovq"))) &&
2581 (Operands.size() == 1 || Operands.size() == 3)) {
Coby Tayree94ddbb42016-11-21 15:50:56 +00002582 if (Name == "movsd" && Operands.size() == 1 && !isParsingIntelSyntax())
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002583 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2584 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
2585 DefaultMemDIOperand(NameLoc));
2586 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2587 }
2588
2589 // Check if we encountered an error for one the string insturctions
2590 if (HadVerifyError) {
2591 return HadVerifyError;
David Woodhouse6f417de2014-01-22 15:08:42 +00002592 }
2593
Chris Lattner4bd21712010-09-15 04:33:27 +00002594 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002595 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002596 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002597 Name.startswith("shl") || Name.startswith("sal") ||
2598 Name.startswith("rcl") || Name.startswith("rcr") ||
2599 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002600 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002601 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002602 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002603 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2604 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2605 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002606 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002607 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002608 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2609 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2610 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002611 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002612 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002613 }
Chad Rosier51afe632012-06-27 22:34:28 +00002614
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002615 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2616 // instalias with an immediate operand yet.
2617 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002618 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
Duncan P. N. Exon Smithd5313222015-07-23 19:27:07 +00002619 if (Op1.isImm())
2620 if (auto *CE = dyn_cast<MCConstantExpr>(Op1.getImm()))
2621 if (CE->getValue() == 3) {
2622 Operands.erase(Operands.begin() + 1);
2623 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
2624 }
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002625 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002626
Marina Yatsinad9658d12016-01-19 16:35:38 +00002627 // Transforms "xlat mem8" into "xlatb"
2628 if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) {
2629 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2630 if (Op1.isMem8()) {
2631 Warning(Op1.getStartLoc(), "memory operand is only for determining the "
2632 "size, (R|E)BX will be used for the location");
2633 Operands.pop_back();
2634 static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb");
2635 }
2636 }
2637
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002638 if (Flags)
2639 Operands.push_back(X86Operand::CreatePrefix(Flags, NameLoc, NameLoc));
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002640 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002641}
2642
David Blaikie960ea3f2014-06-08 16:18:35 +00002643bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Aaron Ballmana81264b2016-05-23 15:52:59 +00002644 return false;
Devang Patelde47cce2012-01-18 22:42:29 +00002645}
2646
Craig Topper8a2a1042017-10-26 21:03:54 +00002647bool X86AsmParser::validateInstruction(MCInst &Inst, const OperandVector &Ops) {
2648 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
2649
2650 switch (Inst.getOpcode()) {
2651 case X86::VGATHERDPDYrm:
2652 case X86::VGATHERDPDrm:
2653 case X86::VGATHERDPSYrm:
2654 case X86::VGATHERDPSrm:
2655 case X86::VGATHERQPDYrm:
2656 case X86::VGATHERQPDrm:
2657 case X86::VGATHERQPSYrm:
2658 case X86::VGATHERQPSrm:
2659 case X86::VPGATHERDDYrm:
2660 case X86::VPGATHERDDrm:
2661 case X86::VPGATHERDQYrm:
2662 case X86::VPGATHERDQrm:
2663 case X86::VPGATHERQDYrm:
2664 case X86::VPGATHERQDrm:
2665 case X86::VPGATHERQQYrm:
2666 case X86::VPGATHERQQrm: {
2667 unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg());
2668 unsigned Mask = MRI->getEncodingValue(Inst.getOperand(1).getReg());
2669 unsigned Index =
2670 MRI->getEncodingValue(Inst.getOperand(3 + X86::AddrIndexReg).getReg());
2671 if (Dest == Mask || Dest == Index || Mask == Index)
2672 return Warning(Ops[0]->getStartLoc(), "mask, index, and destination "
2673 "registers should be distinct");
2674 break;
2675 }
2676 case X86::VGATHERDPDZ128rm:
2677 case X86::VGATHERDPDZ256rm:
2678 case X86::VGATHERDPDZrm:
2679 case X86::VGATHERDPSZ128rm:
2680 case X86::VGATHERDPSZ256rm:
2681 case X86::VGATHERDPSZrm:
2682 case X86::VGATHERQPDZ128rm:
2683 case X86::VGATHERQPDZ256rm:
2684 case X86::VGATHERQPDZrm:
2685 case X86::VGATHERQPSZ128rm:
2686 case X86::VGATHERQPSZ256rm:
2687 case X86::VGATHERQPSZrm:
2688 case X86::VPGATHERDDZ128rm:
2689 case X86::VPGATHERDDZ256rm:
2690 case X86::VPGATHERDDZrm:
2691 case X86::VPGATHERDQZ128rm:
2692 case X86::VPGATHERDQZ256rm:
2693 case X86::VPGATHERDQZrm:
2694 case X86::VPGATHERQDZ128rm:
2695 case X86::VPGATHERQDZ256rm:
2696 case X86::VPGATHERQDZrm:
2697 case X86::VPGATHERQQZ128rm:
2698 case X86::VPGATHERQQZ256rm:
2699 case X86::VPGATHERQQZrm: {
2700 unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg());
2701 unsigned Index =
2702 MRI->getEncodingValue(Inst.getOperand(4 + X86::AddrIndexReg).getReg());
2703 if (Dest == Index)
2704 return Warning(Ops[0]->getStartLoc(), "index and destination registers "
2705 "should be distinct");
2706 break;
2707 }
2708 }
2709
2710 return false;
2711}
2712
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002713static const char *getSubtargetFeatureName(uint64_t Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002714
David Blaikie960ea3f2014-06-08 16:18:35 +00002715void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2716 MCStreamer &Out) {
Andrew V. Tischenko3543f0a2017-11-09 12:45:40 +00002717 Instrumentation->InstrumentAndEmitInstruction(
2718 Inst, Operands, getContext(), MII, Out,
2719 getParser().shouldPrintSchedInfo());
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002720}
2721
David Blaikie960ea3f2014-06-08 16:18:35 +00002722bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2723 OperandVector &Operands,
Tim Northover26bb14e2014-08-18 11:49:42 +00002724 MCStreamer &Out, uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00002725 bool MatchingInlineAsm) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002726 if (isParsingIntelSyntax())
2727 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002728 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002729 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002730 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002731}
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002732
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002733void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
2734 OperandVector &Operands, MCStreamer &Out,
2735 bool MatchingInlineAsm) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002736 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002737 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002738 // call.
Reid Klecknerb1f2d2f2014-07-31 00:07:33 +00002739 const char *Repl = StringSwitch<const char *>(Op.getToken())
2740 .Case("finit", "fninit")
2741 .Case("fsave", "fnsave")
2742 .Case("fstcw", "fnstcw")
2743 .Case("fstcww", "fnstcw")
2744 .Case("fstenv", "fnstenv")
2745 .Case("fstsw", "fnstsw")
2746 .Case("fstsww", "fnstsw")
2747 .Case("fclex", "fnclex")
2748 .Default(nullptr);
2749 if (Repl) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002750 MCInst Inst;
2751 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002752 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002753 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002754 EmitInstruction(Inst, Operands, Out);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002755 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002756 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002757}
2758
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002759bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002760 bool MatchingInlineAsm) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002761 assert(ErrorInfo && "Unknown missing feature!");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002762 SmallString<126> Msg;
2763 raw_svector_ostream OS(Msg);
2764 OS << "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002765 uint64_t Mask = 1;
2766 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2767 if (ErrorInfo & Mask)
2768 OS << ' ' << getSubtargetFeatureName(ErrorInfo & Mask);
2769 Mask <<= 1;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002770 }
Nirav Dave2364748a2016-09-16 18:30:20 +00002771 return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002772}
2773
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002774static unsigned getPrefixes(OperandVector &Operands) {
2775 unsigned Result = 0;
2776 X86Operand &Prefix = static_cast<X86Operand &>(*Operands.back());
2777 if (Prefix.isPrefix()) {
2778 Result = Prefix.getPrefix();
2779 Operands.pop_back();
2780 }
2781 return Result;
2782}
2783
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002784bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
2785 OperandVector &Operands,
2786 MCStreamer &Out,
2787 uint64_t &ErrorInfo,
2788 bool MatchingInlineAsm) {
2789 assert(!Operands.empty() && "Unexpect empty operand list!");
2790 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2791 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Nirav Dave2364748a2016-09-16 18:30:20 +00002792 SMRange EmptyRange = None;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002793
2794 // First, handle aliases that expand to multiple instructions.
2795 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002796
Chris Lattner628fbec2010-09-06 21:54:15 +00002797 bool WasOriginallyInvalidOperand = false;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002798 unsigned Prefixes = getPrefixes(Operands);
2799
Chris Lattnerb44fd242010-09-29 01:42:58 +00002800 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002801
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002802 if (Prefixes)
2803 Inst.setFlags(Prefixes);
2804
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002805 // First, try a direct match.
Nirav Dave6477ce22016-09-26 19:33:36 +00002806 switch (MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
2807 isParsingIntelSyntax())) {
Craig Topper589ceee2015-01-03 08:16:34 +00002808 default: llvm_unreachable("Unexpected match result!");
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002809 case Match_Success:
Craig Toppere6094f92017-11-08 19:38:48 +00002810 if (!MatchingInlineAsm && validateInstruction(Inst, Operands))
Craig Topper8a2a1042017-10-26 21:03:54 +00002811 return true;
Devang Patelde47cce2012-01-18 22:42:29 +00002812 // Some instructions need post-processing to, for example, tweak which
2813 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002814 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002815 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002816 while (processInstruction(Inst, Operands))
2817 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002818
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002819 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002820 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002821 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002822 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002823 return false;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002824 case Match_MissingFeature:
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002825 return ErrorMissingFeature(IDLoc, ErrorInfo, MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002826 case Match_InvalidOperand:
2827 WasOriginallyInvalidOperand = true;
2828 break;
2829 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002830 break;
2831 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002832
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002833 // FIXME: Ideally, we would only attempt suffix matches for things which are
2834 // valid prefixes, and we could just infer the right unambiguous
2835 // type. However, that requires substantially more matcher support than the
2836 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002837
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002838 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002839 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002840 SmallString<16> Tmp;
2841 Tmp += Base;
2842 Tmp += ' ';
Yaron Keren075759a2015-03-30 15:42:36 +00002843 Op.setTokenValue(Tmp);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002844
Chris Lattnerfab94132010-11-06 18:28:02 +00002845 // If this instruction starts with an 'f', then it is a floating point stack
2846 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2847 // 80-bit floating point, which use the suffixes s,l,t respectively.
2848 //
2849 // Otherwise, we assume that this may be an integer instruction, which comes
2850 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2851 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002852
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002853 // Check for the various suffix matches.
Tim Northover26bb14e2014-08-18 11:49:42 +00002854 uint64_t ErrorInfoIgnore;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002855 uint64_t ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002856 unsigned Match[4];
Chad Rosier51afe632012-06-27 22:34:28 +00002857
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002858 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
2859 Tmp.back() = Suffixes[I];
Nirav Dave6477ce22016-09-26 19:33:36 +00002860 Match[I] = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2861 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002862 // If this returned as a missing feature failure, remember that.
2863 if (Match[I] == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002864 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002865 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002866
2867 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002868 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002869
2870 // If exactly one matched, then we treat that as a successful match (and the
2871 // instruction will already have been filled in correctly, since the failing
2872 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002873 unsigned NumSuccessfulMatches =
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002874 std::count(std::begin(Match), std::end(Match), Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002875 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002876 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002877 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002878 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002879 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002880 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002881 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002882
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002883 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002884
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002885 // If we had multiple suffix matches, then identify this as an ambiguous
2886 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002887 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002888 char MatchChars[4];
2889 unsigned NumMatches = 0;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002890 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
2891 if (Match[I] == Match_Success)
2892 MatchChars[NumMatches++] = Suffixes[I];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002893
Alp Tokere69170a2014-06-26 22:52:05 +00002894 SmallString<126> Msg;
2895 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002896 OS << "ambiguous instructions require an explicit suffix (could be ";
2897 for (unsigned i = 0; i != NumMatches; ++i) {
2898 if (i != 0)
2899 OS << ", ";
2900 if (i + 1 == NumMatches)
2901 OS << "or ";
2902 OS << "'" << Base << MatchChars[i] << "'";
2903 }
2904 OS << ")";
Nirav Dave2364748a2016-09-16 18:30:20 +00002905 Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002906 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002907 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002908
Chris Lattner628fbec2010-09-06 21:54:15 +00002909 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002910
Chris Lattner628fbec2010-09-06 21:54:15 +00002911 // If all of the instructions reported an invalid mnemonic, then the original
2912 // mnemonic was invalid.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002913 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002914 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002915 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002916 Op.getLocRange(), MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002917 }
2918
2919 // Recover location info for the operand if we know which was the problem.
Tim Northover26bb14e2014-08-18 11:49:42 +00002920 if (ErrorInfo != ~0ULL) {
Chad Rosier49963552012-10-13 00:26:04 +00002921 if (ErrorInfo >= Operands.size())
Nirav Dave2364748a2016-09-16 18:30:20 +00002922 return Error(IDLoc, "too few operands for instruction", EmptyRange,
2923 MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002924
David Blaikie960ea3f2014-06-08 16:18:35 +00002925 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2926 if (Operand.getStartLoc().isValid()) {
2927 SMRange OperandRange = Operand.getLocRange();
2928 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002929 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002930 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002931 }
2932
Nirav Dave2364748a2016-09-16 18:30:20 +00002933 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Chad Rosier4453e842012-10-12 23:09:25 +00002934 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002935 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002936
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002937 // If one instruction matched with a missing feature, report this as a
2938 // missing feature.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002939 if (std::count(std::begin(Match), std::end(Match),
2940 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002941 ErrorInfo = ErrorInfoMissingFeature;
2942 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002943 MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002944 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002945
Chris Lattner628fbec2010-09-06 21:54:15 +00002946 // If one instruction matched with an invalid operand, report this as an
2947 // operand failure.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002948 if (std::count(std::begin(Match), std::end(Match),
2949 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002950 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002951 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002952 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002953
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002954 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002955 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Nirav Dave2364748a2016-09-16 18:30:20 +00002956 EmptyRange, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002957 return true;
2958}
2959
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002960bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
2961 OperandVector &Operands,
2962 MCStreamer &Out,
2963 uint64_t &ErrorInfo,
2964 bool MatchingInlineAsm) {
2965 assert(!Operands.empty() && "Unexpect empty operand list!");
2966 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2967 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
2968 StringRef Mnemonic = Op.getToken();
Nirav Dave2364748a2016-09-16 18:30:20 +00002969 SMRange EmptyRange = None;
Nirav Daveee554e62016-10-06 15:28:08 +00002970 StringRef Base = Op.getToken();
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002971 unsigned Prefixes = getPrefixes(Operands);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002972
2973 // First, handle aliases that expand to multiple instructions.
2974 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
2975
2976 MCInst Inst;
2977
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00002978 if (Prefixes)
2979 Inst.setFlags(Prefixes);
2980
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002981 // Find one unsized memory operand, if present.
2982 X86Operand *UnsizedMemOp = nullptr;
2983 for (const auto &Op : Operands) {
2984 X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00002985 if (X86Op->isMemUnsized()) {
2986 UnsizedMemOp = X86Op;
Coby Tayree49b37332016-11-22 09:30:29 +00002987 // Have we found an unqualified memory operand,
2988 // break. IA allows only one memory operand.
2989 break;
2990 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002991 }
2992
2993 // Allow some instructions to have implicitly pointer-sized operands. This is
2994 // compatible with gas.
2995 if (UnsizedMemOp) {
2996 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"};
2997 for (const char *Instr : PtrSizedInstrs) {
2998 if (Mnemonic == Instr) {
Craig Topper055845f2015-01-02 07:02:25 +00002999 UnsizedMemOp->Mem.Size = getPointerWidth();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003000 break;
3001 }
3002 }
3003 }
3004
Nirav Daveee554e62016-10-06 15:28:08 +00003005 SmallVector<unsigned, 8> Match;
3006 uint64_t ErrorInfoMissingFeature = 0;
3007
3008 // If unsized push has immediate operand we should default the default pointer
3009 // size for the size.
3010 if (Mnemonic == "push" && Operands.size() == 2) {
3011 auto *X86Op = static_cast<X86Operand *>(Operands[1].get());
3012 if (X86Op->isImm()) {
3013 // If it's not a constant fall through and let remainder take care of it.
3014 const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm());
3015 unsigned Size = getPointerWidth();
3016 if (CE &&
3017 (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) {
3018 SmallString<16> Tmp;
3019 Tmp += Base;
3020 Tmp += (is64BitMode())
3021 ? "q"
3022 : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " ";
3023 Op.setTokenValue(Tmp);
3024 // Do match in ATT mode to allow explicit suffix usage.
3025 Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo,
3026 MatchingInlineAsm,
3027 false /*isParsingIntelSyntax()*/));
3028 Op.setTokenValue(Base);
3029 }
3030 }
3031 }
3032
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003033 // If an unsized memory operand is present, try to match with each memory
3034 // operand size. In Intel assembly, the size is not part of the instruction
3035 // mnemonic.
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003036 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) {
Ahmed Bougachad65f7872014-12-03 02:03:26 +00003037 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512};
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003038 for (unsigned Size : MopSizes) {
3039 UnsizedMemOp->Mem.Size = Size;
3040 uint64_t ErrorInfoIgnore;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00003041 unsigned LastOpcode = Inst.getOpcode();
Nirav Dave6477ce22016-09-26 19:33:36 +00003042 unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
3043 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00003044 if (Match.empty() || LastOpcode != Inst.getOpcode())
3045 Match.push_back(M);
3046
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003047 // If this returned as a missing feature failure, remember that.
3048 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00003049 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003050 }
Reid Kleckner7b7a5992014-08-27 20:10:38 +00003051
3052 // Restore the size of the unsized memory operand if we modified it.
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00003053 UnsizedMemOp->Mem.Size = 0;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00003054 }
3055
3056 // If we haven't matched anything yet, this is not a basic integer or FPU
Saleem Abdulrasoolc3f8ad32015-01-16 20:16:06 +00003057 // operation. There shouldn't be any ambiguity in our mnemonic table, so try
Reid Kleckner7b7a5992014-08-27 20:10:38 +00003058 // matching with the unsized operand.
3059 if (Match.empty()) {
Nirav Dave6477ce22016-09-26 19:33:36 +00003060 Match.push_back(MatchInstruction(
3061 Operands, Inst, ErrorInfo, MatchingInlineAsm, isParsingIntelSyntax()));
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003062 // If this returned as a missing feature failure, remember that.
3063 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00003064 ErrorInfoMissingFeature = ErrorInfo;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003065 }
3066
3067 // Restore the size of the unsized memory operand if we modified it.
3068 if (UnsizedMemOp)
3069 UnsizedMemOp->Mem.Size = 0;
3070
3071 // If it's a bad mnemonic, all results will be the same.
3072 if (Match.back() == Match_MnemonicFail) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003073 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00003074 Op.getLocRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003075 }
3076
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00003077 unsigned NumSuccessfulMatches =
3078 std::count(std::begin(Match), std::end(Match), Match_Success);
3079
3080 // If matching was ambiguous and we had size information from the frontend,
3081 // try again with that. This handles cases like "movxz eax, m8/m16".
3082 if (UnsizedMemOp && NumSuccessfulMatches > 1 &&
3083 UnsizedMemOp->getMemFrontendSize()) {
3084 UnsizedMemOp->Mem.Size = UnsizedMemOp->getMemFrontendSize();
3085 unsigned M = MatchInstruction(
3086 Operands, Inst, ErrorInfo, MatchingInlineAsm, isParsingIntelSyntax());
3087 if (M == Match_Success)
3088 NumSuccessfulMatches = 1;
3089
3090 // Add a rewrite that encodes the size information we used from the
3091 // frontend.
3092 InstInfo->AsmRewrites->emplace_back(
3093 AOK_SizeDirective, UnsizedMemOp->getStartLoc(),
3094 /*Len=*/0, UnsizedMemOp->getMemFrontendSize());
3095 }
3096
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003097 // If exactly one matched, then we treat that as a successful match (and the
3098 // instruction will already have been filled in correctly, since the failing
3099 // matches won't have modified it).
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003100 if (NumSuccessfulMatches == 1) {
Craig Toppere6094f92017-11-08 19:38:48 +00003101 if (!MatchingInlineAsm && validateInstruction(Inst, Operands))
Craig Topper8a2a1042017-10-26 21:03:54 +00003102 return true;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003103 // Some instructions need post-processing to, for example, tweak which
3104 // encoding is selected. Loop on it while changes happen so the individual
3105 // transformations can chain off each other.
3106 if (!MatchingInlineAsm)
3107 while (processInstruction(Inst, Operands))
3108 ;
3109 Inst.setLoc(IDLoc);
3110 if (!MatchingInlineAsm)
3111 EmitInstruction(Inst, Operands, Out);
3112 Opcode = Inst.getOpcode();
3113 return false;
3114 } else if (NumSuccessfulMatches > 1) {
3115 assert(UnsizedMemOp &&
3116 "multiple matches only possible with unsized memory operands");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003117 return Error(UnsizedMemOp->getStartLoc(),
3118 "ambiguous operand size for instruction '" + Mnemonic + "\'",
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00003119 UnsizedMemOp->getLocRange());
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003120 }
3121
3122 // If one instruction matched with a missing feature, report this as a
3123 // missing feature.
3124 if (std::count(std::begin(Match), std::end(Match),
3125 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00003126 ErrorInfo = ErrorInfoMissingFeature;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003127 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
3128 MatchingInlineAsm);
3129 }
3130
3131 // If one instruction matched with an invalid operand, report this as an
3132 // operand failure.
3133 if (std::count(std::begin(Match), std::end(Match),
3134 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00003135 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003136 MatchingInlineAsm);
3137 }
3138
3139 // If all of these were an outright failure, report it in a useless way.
Nirav Dave2364748a2016-09-16 18:30:20 +00003140 return Error(IDLoc, "unknown instruction mnemonic", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00003141 MatchingInlineAsm);
3142}
3143
Nico Weber42f79db2014-07-17 20:24:55 +00003144bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
3145 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
3146}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00003147
Devang Patel4a6e7782012-01-12 18:03:40 +00003148bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003149 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00003150 StringRef IDVal = DirectiveID.getIdentifier();
3151 if (IDVal == ".word")
3152 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00003153 else if (IDVal.startswith(".code"))
3154 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00003155 else if (IDVal.startswith(".att_syntax")) {
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +00003156 getParser().setParsingInlineAsm(false);
Reid Klecknerce63b792014-08-06 23:21:13 +00003157 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3158 if (Parser.getTok().getString() == "prefix")
3159 Parser.Lex();
3160 else if (Parser.getTok().getString() == "noprefix")
3161 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
3162 "supported: registers must have a "
3163 "'%' prefix in .att_syntax");
3164 }
Chad Rosier6f8d8b22012-09-10 20:54:39 +00003165 getParser().setAssemblerDialect(0);
3166 return false;
3167 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00003168 getParser().setAssemblerDialect(1);
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +00003169 getParser().setParsingInlineAsm(true);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00003170 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003171 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00003172 Parser.Lex();
Reid Klecknerce63b792014-08-06 23:21:13 +00003173 else if (Parser.getTok().getString() == "prefix")
3174 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
3175 "supported: registers must not have "
3176 "a '%' prefix in .intel_syntax");
Devang Patel9a9bb5c2012-01-30 20:02:42 +00003177 }
3178 return false;
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003179 } else if (IDVal == ".even")
3180 return parseDirectiveEven(DirectiveID.getLoc());
Reid Kleckner9cdd4df2017-10-11 21:24:33 +00003181 else if (IDVal == ".cv_fpo_proc")
3182 return parseDirectiveFPOProc(DirectiveID.getLoc());
3183 else if (IDVal == ".cv_fpo_setframe")
3184 return parseDirectiveFPOSetFrame(DirectiveID.getLoc());
3185 else if (IDVal == ".cv_fpo_pushreg")
3186 return parseDirectiveFPOPushReg(DirectiveID.getLoc());
3187 else if (IDVal == ".cv_fpo_stackalloc")
3188 return parseDirectiveFPOStackAlloc(DirectiveID.getLoc());
3189 else if (IDVal == ".cv_fpo_endprologue")
3190 return parseDirectiveFPOEndPrologue(DirectiveID.getLoc());
3191 else if (IDVal == ".cv_fpo_endproc")
3192 return parseDirectiveFPOEndProc(DirectiveID.getLoc());
3193
Chris Lattner72c0b592010-10-30 17:38:55 +00003194 return true;
3195}
3196
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003197/// parseDirectiveEven
3198/// ::= .even
3199bool X86AsmParser::parseDirectiveEven(SMLoc L) {
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003200 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3201 TokError("unexpected token in directive");
3202 return false;
3203 }
Eric Christopher445c9522016-10-14 05:47:37 +00003204 const MCSection *Section = getStreamer().getCurrentSectionOnly();
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003205 if (!Section) {
3206 getStreamer().InitSections(false);
Eric Christopher445c9522016-10-14 05:47:37 +00003207 Section = getStreamer().getCurrentSectionOnly();
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003208 }
3209 if (Section->UseCodeAlign())
3210 getStreamer().EmitCodeAlignment(2, 0);
3211 else
3212 getStreamer().EmitValueToAlignment(2, 0, 1, 0);
3213 return false;
3214}
Chris Lattner72c0b592010-10-30 17:38:55 +00003215/// ParseDirectiveWord
3216/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00003217bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003218 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00003219 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3220 for (;;) {
3221 const MCExpr *Value;
David Majnemera375b262015-10-26 02:45:50 +00003222 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003223 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003224 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00003225
David Majnemera375b262015-10-26 02:45:50 +00003226 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
3227 assert(Size <= 8 && "Invalid size");
3228 uint64_t IntValue = MCE->getValue();
3229 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
3230 return Error(ExprLoc, "literal value out of range for directive");
3231 getStreamer().EmitIntValue(IntValue, Size);
3232 } else {
3233 getStreamer().EmitValue(Value, Size, ExprLoc);
3234 }
Chad Rosier51afe632012-06-27 22:34:28 +00003235
Chris Lattner72c0b592010-10-30 17:38:55 +00003236 if (getLexer().is(AsmToken::EndOfStatement))
3237 break;
Chad Rosier51afe632012-06-27 22:34:28 +00003238
Chris Lattner72c0b592010-10-30 17:38:55 +00003239 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003240 if (getLexer().isNot(AsmToken::Comma)) {
3241 Error(L, "unexpected token in directive");
3242 return false;
3243 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003244 Parser.Lex();
3245 }
3246 }
Chad Rosier51afe632012-06-27 22:34:28 +00003247
Chris Lattner72c0b592010-10-30 17:38:55 +00003248 Parser.Lex();
3249 return false;
3250}
3251
Evan Cheng481ebb02011-07-27 00:38:12 +00003252/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00003253/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00003254bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003255 MCAsmParser &Parser = getParser();
Nirav Dave6477ce22016-09-26 19:33:36 +00003256 Code16GCC = false;
Craig Topper3c80d622014-01-06 04:55:54 +00003257 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00003258 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00003259 if (!is16BitMode()) {
3260 SwitchMode(X86::Mode16Bit);
3261 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3262 }
Nirav Dave6477ce22016-09-26 19:33:36 +00003263 } else if (IDVal == ".code16gcc") {
3264 // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode.
3265 Parser.Lex();
3266 Code16GCC = true;
3267 if (!is16BitMode()) {
3268 SwitchMode(X86::Mode16Bit);
3269 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3270 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003271 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00003272 Parser.Lex();
3273 if (!is32BitMode()) {
3274 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003275 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
3276 }
3277 } else if (IDVal == ".code64") {
3278 Parser.Lex();
3279 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00003280 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003281 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
3282 }
3283 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003284 Error(L, "unknown directive " + IDVal);
3285 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00003286 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003287
Evan Cheng481ebb02011-07-27 00:38:12 +00003288 return false;
3289}
Chris Lattner72c0b592010-10-30 17:38:55 +00003290
Reid Kleckner9cdd4df2017-10-11 21:24:33 +00003291// .cv_fpo_proc foo
3292bool X86AsmParser::parseDirectiveFPOProc(SMLoc L) {
3293 MCAsmParser &Parser = getParser();
3294 StringRef ProcName;
3295 int64_t ParamsSize;
3296 if (Parser.parseIdentifier(ProcName))
3297 return Parser.TokError("expected symbol name");
3298 if (Parser.parseIntToken(ParamsSize, "expected parameter byte count"))
3299 return true;
3300 if (!isUIntN(32, ParamsSize))
3301 return Parser.TokError("parameters size out of range");
3302 if (Parser.parseEOL("unexpected tokens"))
3303 return addErrorSuffix(" in '.cv_fpo_proc' directive");
3304 MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
3305 return getTargetStreamer().emitFPOProc(ProcSym, ParamsSize, L);
3306}
3307
3308// .cv_fpo_setframe ebp
3309bool X86AsmParser::parseDirectiveFPOSetFrame(SMLoc L) {
3310 MCAsmParser &Parser = getParser();
3311 unsigned Reg;
3312 SMLoc DummyLoc;
3313 if (ParseRegister(Reg, DummyLoc, DummyLoc) ||
3314 Parser.parseEOL("unexpected tokens"))
3315 return addErrorSuffix(" in '.cv_fpo_setframe' directive");
3316 return getTargetStreamer().emitFPOSetFrame(Reg, L);
3317}
3318
3319// .cv_fpo_pushreg ebx
3320bool X86AsmParser::parseDirectiveFPOPushReg(SMLoc L) {
3321 MCAsmParser &Parser = getParser();
3322 unsigned Reg;
3323 SMLoc DummyLoc;
3324 if (ParseRegister(Reg, DummyLoc, DummyLoc) ||
3325 Parser.parseEOL("unexpected tokens"))
3326 return addErrorSuffix(" in '.cv_fpo_pushreg' directive");
3327 return getTargetStreamer().emitFPOPushReg(Reg, L);
3328}
3329
3330// .cv_fpo_stackalloc 20
3331bool X86AsmParser::parseDirectiveFPOStackAlloc(SMLoc L) {
3332 MCAsmParser &Parser = getParser();
3333 int64_t Offset;
3334 if (Parser.parseIntToken(Offset, "expected offset") ||
3335 Parser.parseEOL("unexpected tokens"))
3336 return addErrorSuffix(" in '.cv_fpo_stackalloc' directive");
3337 return getTargetStreamer().emitFPOStackAlloc(Offset, L);
3338}
3339
3340// .cv_fpo_endprologue
3341bool X86AsmParser::parseDirectiveFPOEndPrologue(SMLoc L) {
3342 MCAsmParser &Parser = getParser();
3343 if (Parser.parseEOL("unexpected tokens"))
3344 return addErrorSuffix(" in '.cv_fpo_endprologue' directive");
3345 return getTargetStreamer().emitFPOEndPrologue(L);
3346}
3347
3348// .cv_fpo_endproc
3349bool X86AsmParser::parseDirectiveFPOEndProc(SMLoc L) {
3350 MCAsmParser &Parser = getParser();
3351 if (Parser.parseEOL("unexpected tokens"))
3352 return addErrorSuffix(" in '.cv_fpo_endproc' directive");
3353 return getTargetStreamer().emitFPOEndProc(L);
3354}
3355
Daniel Dunbar71475772009-07-17 20:42:00 +00003356// Force static initialization.
3357extern "C" void LLVMInitializeX86AsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00003358 RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target());
3359 RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target());
Daniel Dunbar71475772009-07-17 20:42:00 +00003360}
Daniel Dunbar00331992009-07-29 00:02:19 +00003361
Chris Lattner3e4582a2010-09-06 19:11:01 +00003362#define GET_REGISTER_MATCHER
3363#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00003364#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00003365#include "X86GenAsmMatcher.inc"