blob: 112af623b4a906c18853d936a09f13c03b48103c [file] [log] [blame]
Daniel Dunbar71475772009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng11424442011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000011#include "X86AsmInstrumentation.h"
Evgeniy Stepanove3804d42014-02-28 12:28:07 +000012#include "X86AsmParserCommon.h"
13#include "X86Operand.h"
Coby Tayreed8912892017-08-24 08:46:25 +000014#include "InstPrinter/X86IntelInstPrinter.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000015#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000018#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000020#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000023#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCParser/MCAsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000027#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/MC/MCRegisterInfo.h"
Michael Zuckerman02ecd432015-12-13 17:07:23 +000029#include "llvm/MC/MCSection.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSubtargetInfo.h"
32#include "llvm/MC/MCSymbol.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000033#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000035#include "llvm/Support/raw_ostream.h"
Reid Kleckner7b1e1a02014-07-30 22:23:11 +000036#include <algorithm>
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000037#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000038
Daniel Dunbar71475772009-07-17 20:42:00 +000039using namespace llvm;
40
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +000041static bool checkScale(unsigned Scale, StringRef &ErrMsg) {
42 if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
43 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
44 return true;
45 }
46 return false;
47}
48
Daniel Dunbar71475772009-07-17 20:42:00 +000049namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000050
Chad Rosier5362af92013-04-16 18:15:40 +000051static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000052 0, // IC_OR
Michael Kupersteine3de07a2015-06-14 12:59:45 +000053 1, // IC_XOR
54 2, // IC_AND
55 3, // IC_LSHIFT
56 3, // IC_RSHIFT
57 4, // IC_PLUS
58 4, // IC_MINUS
59 5, // IC_MULTIPLY
60 5, // IC_DIVIDE
Coby Tayree41a5b552017-06-27 16:58:27 +000061 5, // IC_MOD
62 6, // IC_NOT
63 7, // IC_NEG
64 8, // IC_RPAREN
65 9, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000066 0, // IC_IMM
67 0 // IC_REGISTER
68};
69
Devang Patel4a6e7782012-01-12 18:03:40 +000070class X86AsmParser : public MCTargetAsmParser {
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000071 const MCInstrInfo &MII;
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
Nirav Dave6477ce22016-09-26 19:33:36 +000084 unsigned MatchInstruction(const OperandVector &Operands, MCInst &Inst,
85 uint64_t &ErrorInfo, bool matchingInlineAsm,
86 unsigned VariantID = 0) {
87 // In Code16GCC mode, match as 32-bit.
88 if (Code16GCC)
89 SwitchMode(X86::Mode32Bit);
90 unsigned rv = MatchInstructionImpl(Operands, Inst, ErrorInfo,
91 matchingInlineAsm, VariantID);
92 if (Code16GCC)
93 SwitchMode(X86::Mode16Bit);
94 return rv;
95 }
96
Chad Rosier5362af92013-04-16 18:15:40 +000097 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000098 IC_OR = 0,
Michael Kupersteine3de07a2015-06-14 12:59:45 +000099 IC_XOR,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000100 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000101 IC_LSHIFT,
102 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000103 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +0000104 IC_MINUS,
105 IC_MULTIPLY,
106 IC_DIVIDE,
Coby Tayree41a5b552017-06-27 16:58:27 +0000107 IC_MOD,
108 IC_NOT,
109 IC_NEG,
Chad Rosier5362af92013-04-16 18:15:40 +0000110 IC_RPAREN,
111 IC_LPAREN,
112 IC_IMM,
113 IC_REGISTER
114 };
115
Coby Tayree07a89742017-03-21 19:31:55 +0000116 enum IntelOperatorKind {
117 IOK_INVALID = 0,
118 IOK_LENGTH,
119 IOK_SIZE,
120 IOK_TYPE,
121 IOK_OFFSET
122 };
123
Chad Rosier5362af92013-04-16 18:15:40 +0000124 class InfixCalculator {
125 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
126 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
127 SmallVector<ICToken, 4> PostfixStack;
Michael Liao5bf95782014-12-04 05:20:33 +0000128
Coby Tayree41a5b552017-06-27 16:58:27 +0000129 bool isUnaryOperator(const InfixCalculatorTok Op) {
130 return Op == IC_NEG || Op == IC_NOT;
131 }
132
Chad Rosier5362af92013-04-16 18:15:40 +0000133 public:
134 int64_t popOperand() {
135 assert (!PostfixStack.empty() && "Poped an empty stack!");
136 ICToken Op = PostfixStack.pop_back_val();
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000137 if (!(Op.first == IC_IMM || Op.first == IC_REGISTER))
138 return -1; // The invalid Scale value will be caught later by checkScale
Chad Rosier5362af92013-04-16 18:15:40 +0000139 return Op.second;
140 }
141 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
142 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
143 "Unexpected operand!");
144 PostfixStack.push_back(std::make_pair(Op, Val));
145 }
Michael Liao5bf95782014-12-04 05:20:33 +0000146
Jakub Staszak9c349222013-08-08 15:48:46 +0000147 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000148 void pushOperator(InfixCalculatorTok Op) {
149 // Push the new operator if the stack is empty.
150 if (InfixOperatorStack.empty()) {
151 InfixOperatorStack.push_back(Op);
152 return;
153 }
Michael Liao5bf95782014-12-04 05:20:33 +0000154
Chad Rosier5362af92013-04-16 18:15:40 +0000155 // Push the new operator if it has a higher precedence than the operator
156 // on the top of the stack or the operator on the top of the stack is a
157 // left parentheses.
158 unsigned Idx = InfixOperatorStack.size() - 1;
159 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
160 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
161 InfixOperatorStack.push_back(Op);
162 return;
163 }
Michael Liao5bf95782014-12-04 05:20:33 +0000164
Chad Rosier5362af92013-04-16 18:15:40 +0000165 // The operator on the top of the stack has higher precedence than the
166 // new operator.
167 unsigned ParenCount = 0;
Kirill Bobyrev6afbaf02017-01-18 16:34:25 +0000168 while (1) {
Chad Rosier5362af92013-04-16 18:15:40 +0000169 // Nothing to process.
170 if (InfixOperatorStack.empty())
171 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000172
Chad Rosier5362af92013-04-16 18:15:40 +0000173 Idx = InfixOperatorStack.size() - 1;
174 StackOp = InfixOperatorStack[Idx];
175 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
176 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000177
Chad Rosier5362af92013-04-16 18:15:40 +0000178 // If we have an even parentheses count and we see a left parentheses,
179 // then stop processing.
180 if (!ParenCount && StackOp == IC_LPAREN)
181 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000182
Chad Rosier5362af92013-04-16 18:15:40 +0000183 if (StackOp == IC_RPAREN) {
184 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000185 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000186 } else if (StackOp == IC_LPAREN) {
187 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000188 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000189 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000190 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000191 PostfixStack.push_back(std::make_pair(StackOp, 0));
192 }
193 }
194 // Push the new operator.
195 InfixOperatorStack.push_back(Op);
196 }
Marina Yatsinaa0e02412015-08-10 11:33:10 +0000197
Chad Rosier5362af92013-04-16 18:15:40 +0000198 int64_t execute() {
199 // Push any remaining operators onto the postfix stack.
200 while (!InfixOperatorStack.empty()) {
201 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
202 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
203 PostfixStack.push_back(std::make_pair(StackOp, 0));
204 }
Michael Liao5bf95782014-12-04 05:20:33 +0000205
Chad Rosier5362af92013-04-16 18:15:40 +0000206 if (PostfixStack.empty())
207 return 0;
Michael Liao5bf95782014-12-04 05:20:33 +0000208
Chad Rosier5362af92013-04-16 18:15:40 +0000209 SmallVector<ICToken, 16> OperandStack;
210 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
211 ICToken Op = PostfixStack[i];
212 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
213 OperandStack.push_back(Op);
Coby Tayree41a5b552017-06-27 16:58:27 +0000214 } else if (isUnaryOperator(Op.first)) {
215 assert (OperandStack.size() > 0 && "Too few operands.");
216 ICToken Operand = OperandStack.pop_back_val();
217 assert (Operand.first == IC_IMM &&
218 "Unary operation with a register!");
219 switch (Op.first) {
220 default:
221 report_fatal_error("Unexpected operator!");
222 break;
223 case IC_NEG:
224 OperandStack.push_back(std::make_pair(IC_IMM, -Operand.second));
225 break;
226 case IC_NOT:
227 OperandStack.push_back(std::make_pair(IC_IMM, ~Operand.second));
228 break;
229 }
Chad Rosier5362af92013-04-16 18:15:40 +0000230 } else {
231 assert (OperandStack.size() > 1 && "Too few operands.");
232 int64_t Val;
233 ICToken Op2 = OperandStack.pop_back_val();
234 ICToken Op1 = OperandStack.pop_back_val();
235 switch (Op.first) {
236 default:
237 report_fatal_error("Unexpected operator!");
238 break;
239 case IC_PLUS:
240 Val = Op1.second + Op2.second;
241 OperandStack.push_back(std::make_pair(IC_IMM, Val));
242 break;
243 case IC_MINUS:
244 Val = Op1.second - Op2.second;
245 OperandStack.push_back(std::make_pair(IC_IMM, Val));
246 break;
247 case IC_MULTIPLY:
248 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
249 "Multiply operation with an immediate and a register!");
250 Val = Op1.second * Op2.second;
251 OperandStack.push_back(std::make_pair(IC_IMM, Val));
252 break;
253 case IC_DIVIDE:
254 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
255 "Divide operation with an immediate and a register!");
256 assert (Op2.second != 0 && "Division by zero!");
257 Val = Op1.second / Op2.second;
258 OperandStack.push_back(std::make_pair(IC_IMM, Val));
259 break;
Coby Tayree41a5b552017-06-27 16:58:27 +0000260 case IC_MOD:
261 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
262 "Modulo operation with an immediate and a register!");
263 Val = Op1.second % Op2.second;
264 OperandStack.push_back(std::make_pair(IC_IMM, Val));
265 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000266 case IC_OR:
267 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
268 "Or operation with an immediate and a register!");
269 Val = Op1.second | Op2.second;
270 OperandStack.push_back(std::make_pair(IC_IMM, Val));
271 break;
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000272 case IC_XOR:
273 assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
274 "Xor operation with an immediate and a register!");
275 Val = Op1.second ^ Op2.second;
276 OperandStack.push_back(std::make_pair(IC_IMM, Val));
277 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000278 case IC_AND:
279 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
280 "And operation with an immediate and a register!");
281 Val = Op1.second & Op2.second;
282 OperandStack.push_back(std::make_pair(IC_IMM, Val));
283 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000284 case IC_LSHIFT:
285 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
286 "Left shift operation with an immediate and a register!");
287 Val = Op1.second << Op2.second;
288 OperandStack.push_back(std::make_pair(IC_IMM, Val));
289 break;
290 case IC_RSHIFT:
291 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
292 "Right shift operation with an immediate and a register!");
293 Val = Op1.second >> Op2.second;
294 OperandStack.push_back(std::make_pair(IC_IMM, Val));
295 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000296 }
297 }
298 }
299 assert (OperandStack.size() == 1 && "Expected a single result.");
300 return OperandStack.pop_back_val().second;
301 }
302 };
303
304 enum IntelExprState {
Coby Tayreed8912892017-08-24 08:46:25 +0000305 IES_INIT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000306 IES_OR,
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000307 IES_XOR,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000308 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000309 IES_LSHIFT,
310 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000311 IES_PLUS,
312 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000313 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000314 IES_MULTIPLY,
315 IES_DIVIDE,
Coby Tayree41a5b552017-06-27 16:58:27 +0000316 IES_MOD,
Chad Rosier5362af92013-04-16 18:15:40 +0000317 IES_LBRAC,
318 IES_RBRAC,
319 IES_LPAREN,
320 IES_RPAREN,
321 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000322 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000323 IES_IDENTIFIER,
324 IES_ERROR
325 };
326
327 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000328 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000329 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000330 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000331 const MCExpr *Sym;
332 StringRef SymName;
333 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000334 InlineAsmIdentifierInfo Info;
Coby Tayreed8912892017-08-24 08:46:25 +0000335 short BracCount;
336 bool MemExpr;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000337
Chad Rosier5362af92013-04-16 18:15:40 +0000338 public:
Coby Tayreed8912892017-08-24 08:46:25 +0000339 IntelExprStateMachine()
340 : State(IES_INIT), PrevState(IES_ERROR), BaseReg(0), IndexReg(0),
341 TmpReg(0), Scale(1), Imm(0), Sym(nullptr), BracCount(0),
Coby Tayreec3d24112017-09-29 07:02:46 +0000342 MemExpr(false) {}
Michael Liao5bf95782014-12-04 05:20:33 +0000343
Coby Tayreed8912892017-08-24 08:46:25 +0000344 void addImm(int64_t imm) { Imm += imm; }
345 short getBracCount() { return BracCount; }
346 bool isMemExpr() { return MemExpr; }
Chad Rosier5362af92013-04-16 18:15:40 +0000347 unsigned getBaseReg() { return BaseReg; }
348 unsigned getIndexReg() { return IndexReg; }
349 unsigned getScale() { return Scale; }
350 const MCExpr *getSym() { return Sym; }
351 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000352 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000353 bool isValidEndState() {
354 return State == IES_RBRAC || State == IES_INTEGER;
355 }
Chad Rosier31246272013-04-17 21:01:45 +0000356 bool hadError() { return State == IES_ERROR; }
Coby Tayreed8912892017-08-24 08:46:25 +0000357 InlineAsmIdentifierInfo &getIdentifierInfo() { return Info; }
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000358
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000359 void onOr() {
360 IntelExprState CurrState = State;
361 switch (State) {
362 default:
363 State = IES_ERROR;
364 break;
365 case IES_INTEGER:
366 case IES_RPAREN:
367 case IES_REGISTER:
368 State = IES_OR;
369 IC.pushOperator(IC_OR);
370 break;
371 }
372 PrevState = CurrState;
373 }
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000374 void onXor() {
375 IntelExprState CurrState = State;
376 switch (State) {
377 default:
378 State = IES_ERROR;
379 break;
380 case IES_INTEGER:
381 case IES_RPAREN:
382 case IES_REGISTER:
383 State = IES_XOR;
384 IC.pushOperator(IC_XOR);
385 break;
386 }
387 PrevState = CurrState;
388 }
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000389 void onAnd() {
390 IntelExprState CurrState = State;
391 switch (State) {
392 default:
393 State = IES_ERROR;
394 break;
395 case IES_INTEGER:
396 case IES_RPAREN:
397 case IES_REGISTER:
398 State = IES_AND;
399 IC.pushOperator(IC_AND);
400 break;
401 }
402 PrevState = CurrState;
403 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000404 void onLShift() {
405 IntelExprState CurrState = State;
406 switch (State) {
407 default:
408 State = IES_ERROR;
409 break;
410 case IES_INTEGER:
411 case IES_RPAREN:
412 case IES_REGISTER:
413 State = IES_LSHIFT;
414 IC.pushOperator(IC_LSHIFT);
415 break;
416 }
417 PrevState = CurrState;
418 }
419 void onRShift() {
420 IntelExprState CurrState = State;
421 switch (State) {
422 default:
423 State = IES_ERROR;
424 break;
425 case IES_INTEGER:
426 case IES_RPAREN:
427 case IES_REGISTER:
428 State = IES_RSHIFT;
429 IC.pushOperator(IC_RSHIFT);
430 break;
431 }
432 PrevState = CurrState;
433 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000434 bool onPlus(StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000435 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000436 switch (State) {
437 default:
438 State = IES_ERROR;
439 break;
440 case IES_INTEGER:
441 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000442 case IES_REGISTER:
443 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000444 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000445 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
446 // If we already have a BaseReg, then assume this is the IndexReg with
447 // a scale of 1.
448 if (!BaseReg) {
449 BaseReg = TmpReg;
450 } else {
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000451 if (IndexReg) {
452 ErrMsg = "BaseReg/IndexReg already set!";
453 return true;
454 }
Chad Rosier31246272013-04-17 21:01:45 +0000455 IndexReg = TmpReg;
456 Scale = 1;
457 }
458 }
Chad Rosier5362af92013-04-16 18:15:40 +0000459 break;
460 }
Chad Rosier31246272013-04-17 21:01:45 +0000461 PrevState = CurrState;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000462 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000463 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000464 bool onMinus(StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000465 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000466 switch (State) {
467 default:
468 State = IES_ERROR;
469 break;
Coby Tayree41a5b552017-06-27 16:58:27 +0000470 case IES_OR:
471 case IES_XOR:
472 case IES_AND:
473 case IES_LSHIFT:
474 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000475 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000476 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000477 case IES_MULTIPLY:
478 case IES_DIVIDE:
Coby Tayree41a5b552017-06-27 16:58:27 +0000479 case IES_MOD:
Chad Rosier5362af92013-04-16 18:15:40 +0000480 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000481 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000482 case IES_LBRAC:
483 case IES_RBRAC:
484 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000485 case IES_REGISTER:
Coby Tayreed8912892017-08-24 08:46:25 +0000486 case IES_INIT:
Chad Rosier5362af92013-04-16 18:15:40 +0000487 State = IES_MINUS;
Coby Tayree41a5b552017-06-27 16:58:27 +0000488 // push minus operator if it is not a negate operator
489 if (CurrState == IES_REGISTER || CurrState == IES_RPAREN ||
490 CurrState == IES_INTEGER || CurrState == IES_RBRAC)
Chad Rosier31246272013-04-17 21:01:45 +0000491 IC.pushOperator(IC_MINUS);
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000492 else if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
493 // We have negate operator for Scale: it's illegal
494 ErrMsg = "Scale can't be negative";
495 return true;
496 } else
Coby Tayree41a5b552017-06-27 16:58:27 +0000497 IC.pushOperator(IC_NEG);
Chad Rosier31246272013-04-17 21:01:45 +0000498 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
499 // If we already have a BaseReg, then assume this is the IndexReg with
500 // a scale of 1.
501 if (!BaseReg) {
502 BaseReg = TmpReg;
503 } else {
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000504 if (IndexReg) {
505 ErrMsg = "BaseReg/IndexReg already set!";
506 return true;
507 }
Chad Rosier31246272013-04-17 21:01:45 +0000508 IndexReg = TmpReg;
509 Scale = 1;
510 }
Chad Rosier5362af92013-04-16 18:15:40 +0000511 }
Chad Rosier5362af92013-04-16 18:15:40 +0000512 break;
513 }
Chad Rosier31246272013-04-17 21:01:45 +0000514 PrevState = CurrState;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000515 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000516 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000517 void onNot() {
518 IntelExprState CurrState = State;
519 switch (State) {
520 default:
521 State = IES_ERROR;
522 break;
Coby Tayree41a5b552017-06-27 16:58:27 +0000523 case IES_OR:
524 case IES_XOR:
525 case IES_AND:
526 case IES_LSHIFT:
527 case IES_RSHIFT:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000528 case IES_PLUS:
Coby Tayree41a5b552017-06-27 16:58:27 +0000529 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000530 case IES_NOT:
Coby Tayree41a5b552017-06-27 16:58:27 +0000531 case IES_MULTIPLY:
532 case IES_DIVIDE:
533 case IES_MOD:
534 case IES_LPAREN:
535 case IES_LBRAC:
Coby Tayreed8912892017-08-24 08:46:25 +0000536 case IES_INIT:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000537 State = IES_NOT;
Coby Tayree41a5b552017-06-27 16:58:27 +0000538 IC.pushOperator(IC_NOT);
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000539 break;
540 }
541 PrevState = CurrState;
542 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000543
544 bool onRegister(unsigned Reg, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000545 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000546 switch (State) {
547 default:
548 State = IES_ERROR;
549 break;
550 case IES_PLUS:
551 case IES_LPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000552 case IES_LBRAC:
Chad Rosier5362af92013-04-16 18:15:40 +0000553 State = IES_REGISTER;
554 TmpReg = Reg;
555 IC.pushOperand(IC_REGISTER);
556 break;
Chad Rosier31246272013-04-17 21:01:45 +0000557 case IES_MULTIPLY:
558 // Index Register - Scale * Register
559 if (PrevState == IES_INTEGER) {
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000560 if (IndexReg) {
561 ErrMsg = "BaseReg/IndexReg already set!";
562 return true;
563 }
Chad Rosier31246272013-04-17 21:01:45 +0000564 State = IES_REGISTER;
565 IndexReg = Reg;
566 // Get the scale and replace the 'Scale * Register' with '0'.
567 Scale = IC.popOperand();
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000568 if (checkScale(Scale, ErrMsg))
569 return true;
Chad Rosier31246272013-04-17 21:01:45 +0000570 IC.pushOperand(IC_IMM);
571 IC.popOperator();
572 } else {
573 State = IES_ERROR;
574 }
Chad Rosier5362af92013-04-16 18:15:40 +0000575 break;
576 }
Chad Rosier31246272013-04-17 21:01:45 +0000577 PrevState = CurrState;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000578 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000579 }
Coby Tayreed8912892017-08-24 08:46:25 +0000580 bool onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName,
Coby Tayreec3d24112017-09-29 07:02:46 +0000581 const InlineAsmIdentifierInfo &IDInfo,
582 bool ParsingInlineAsm, StringRef &ErrMsg) {
583 // InlineAsm: Treat an enum value as an integer
584 if (ParsingInlineAsm)
585 if (IDInfo.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
586 return onInteger(IDInfo.Enum.EnumVal, ErrMsg);
587 // Treat a symbolic constant like an integer
588 if (auto *CE = dyn_cast<MCConstantExpr>(SymRef))
589 return onInteger(CE->getValue(), ErrMsg);
Chad Rosierdb003992013-04-18 16:28:19 +0000590 PrevState = State;
Coby Tayreed8912892017-08-24 08:46:25 +0000591 bool HasSymbol = Sym != nullptr;
Chad Rosier5362af92013-04-16 18:15:40 +0000592 switch (State) {
593 default:
594 State = IES_ERROR;
595 break;
596 case IES_PLUS:
597 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000598 case IES_NOT:
Coby Tayreed8912892017-08-24 08:46:25 +0000599 case IES_INIT:
600 case IES_LBRAC:
Coby Tayreec3d24112017-09-29 07:02:46 +0000601 MemExpr = true;
Chad Rosier5362af92013-04-16 18:15:40 +0000602 State = IES_INTEGER;
603 Sym = SymRef;
604 SymName = SymRefName;
605 IC.pushOperand(IC_IMM);
Coby Tayreec3d24112017-09-29 07:02:46 +0000606 if (ParsingInlineAsm)
607 Info = IDInfo;
Chad Rosier5362af92013-04-16 18:15:40 +0000608 break;
609 }
Coby Tayreed8912892017-08-24 08:46:25 +0000610 if (HasSymbol)
611 ErrMsg = "cannot use more than one symbol in memory operand";
612 return HasSymbol;
Chad Rosier5362af92013-04-16 18:15:40 +0000613 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000614 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000615 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000616 switch (State) {
617 default:
618 State = IES_ERROR;
619 break;
620 case IES_PLUS:
621 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000622 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000623 case IES_OR:
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000624 case IES_XOR:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000625 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000626 case IES_LSHIFT:
627 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000628 case IES_DIVIDE:
Coby Tayree41a5b552017-06-27 16:58:27 +0000629 case IES_MOD:
Chad Rosier31246272013-04-17 21:01:45 +0000630 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000631 case IES_LPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000632 case IES_INIT:
633 case IES_LBRAC:
Chad Rosier5362af92013-04-16 18:15:40 +0000634 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000635 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
636 // Index Register - Register * Scale
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000637 if (IndexReg) {
638 ErrMsg = "BaseReg/IndexReg already set!";
Kevin Enderby9d117022014-01-23 21:52:41 +0000639 return true;
640 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000641 IndexReg = TmpReg;
642 Scale = TmpInt;
643 if (checkScale(Scale, ErrMsg))
644 return true;
Chad Rosier31246272013-04-17 21:01:45 +0000645 // Get the scale and replace the 'Register * Scale' with '0'.
646 IC.popOperator();
Chad Rosier31246272013-04-17 21:01:45 +0000647 } else {
648 IC.pushOperand(IC_IMM, TmpInt);
649 }
Chad Rosier5362af92013-04-16 18:15:40 +0000650 break;
651 }
Chad Rosier31246272013-04-17 21:01:45 +0000652 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000653 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000654 }
655 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000656 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000657 switch (State) {
658 default:
659 State = IES_ERROR;
660 break;
661 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000662 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000663 case IES_RPAREN:
664 State = IES_MULTIPLY;
665 IC.pushOperator(IC_MULTIPLY);
666 break;
667 }
668 }
669 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000670 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000671 switch (State) {
672 default:
673 State = IES_ERROR;
674 break;
675 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000676 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000677 State = IES_DIVIDE;
678 IC.pushOperator(IC_DIVIDE);
679 break;
680 }
681 }
Coby Tayree41a5b552017-06-27 16:58:27 +0000682 void onMod() {
683 PrevState = State;
684 switch (State) {
685 default:
686 State = IES_ERROR;
687 break;
688 case IES_INTEGER:
689 case IES_RPAREN:
690 State = IES_MOD;
691 IC.pushOperator(IC_MOD);
692 break;
693 }
694 }
Coby Tayreed8912892017-08-24 08:46:25 +0000695 bool onLBrac() {
696 if (BracCount)
697 return true;
Chad Rosierdb003992013-04-18 16:28:19 +0000698 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000699 switch (State) {
700 default:
701 State = IES_ERROR;
702 break;
703 case IES_RBRAC:
Coby Tayreed8912892017-08-24 08:46:25 +0000704 case IES_INTEGER:
705 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000706 State = IES_PLUS;
707 IC.pushOperator(IC_PLUS);
708 break;
Coby Tayreed8912892017-08-24 08:46:25 +0000709 case IES_INIT:
710 assert(!BracCount && "BracCount should be zero on parsing's start");
711 State = IES_LBRAC;
712 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000713 }
Coby Tayreed8912892017-08-24 08:46:25 +0000714 MemExpr = true;
715 BracCount++;
716 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000717 }
Coby Tayreed8912892017-08-24 08:46:25 +0000718 bool onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000719 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000720 switch (State) {
721 default:
722 State = IES_ERROR;
723 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000724 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000725 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000726 case IES_RPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000727 if (BracCount-- != 1)
728 return true;
Chad Rosier5362af92013-04-16 18:15:40 +0000729 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000730 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
731 // If we already have a BaseReg, then assume this is the IndexReg with
732 // a scale of 1.
733 if (!BaseReg) {
734 BaseReg = TmpReg;
735 } else {
736 assert (!IndexReg && "BaseReg/IndexReg already set!");
737 IndexReg = TmpReg;
738 Scale = 1;
739 }
Chad Rosier5362af92013-04-16 18:15:40 +0000740 }
741 break;
742 }
Chad Rosier31246272013-04-17 21:01:45 +0000743 PrevState = CurrState;
Coby Tayreed8912892017-08-24 08:46:25 +0000744 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000745 }
746 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000747 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000748 switch (State) {
749 default:
750 State = IES_ERROR;
751 break;
752 case IES_PLUS:
753 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000754 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000755 case IES_OR:
Michael Kupersteine3de07a2015-06-14 12:59:45 +0000756 case IES_XOR:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000757 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000758 case IES_LSHIFT:
759 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000760 case IES_MULTIPLY:
761 case IES_DIVIDE:
Coby Tayree41a5b552017-06-27 16:58:27 +0000762 case IES_MOD:
Chad Rosier5362af92013-04-16 18:15:40 +0000763 case IES_LPAREN:
Coby Tayreed8912892017-08-24 08:46:25 +0000764 case IES_INIT:
765 case IES_LBRAC:
Chad Rosier5362af92013-04-16 18:15:40 +0000766 State = IES_LPAREN;
767 IC.pushOperator(IC_LPAREN);
768 break;
769 }
Chad Rosier31246272013-04-17 21:01:45 +0000770 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000771 }
772 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000773 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000774 switch (State) {
775 default:
776 State = IES_ERROR;
777 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000778 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000779 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000780 case IES_RPAREN:
781 State = IES_RPAREN;
782 IC.pushOperator(IC_RPAREN);
783 break;
784 }
785 }
786 };
787
Nirav Dave2364748a2016-09-16 18:30:20 +0000788 bool Error(SMLoc L, const Twine &Msg, SMRange Range = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000789 bool MatchingInlineAsm = false) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000790 MCAsmParser &Parser = getParser();
Nirav Dave2364748a2016-09-16 18:30:20 +0000791 if (MatchingInlineAsm) {
792 if (!getLexer().isAtStartOfStatement())
793 Parser.eatToEndOfStatement();
794 return false;
795 }
796 return Parser.Error(L, Msg, Range);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000797 }
798
David Blaikie960ea3f2014-06-08 16:18:35 +0000799 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000800 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000801 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000802 }
803
David Blaikie960ea3f2014-06-08 16:18:35 +0000804 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
805 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
Marina Yatsinab9f4f622016-01-19 15:37:56 +0000806 bool IsSIReg(unsigned Reg);
807 unsigned GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, bool IsSIReg);
808 void
809 AddDefaultSrcDestOperands(OperandVector &Operands,
810 std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
811 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst);
812 bool VerifyAndAdjustOperands(OperandVector &OrigOperands,
813 OperandVector &FinalOperands);
David Blaikie960ea3f2014-06-08 16:18:35 +0000814 std::unique_ptr<X86Operand> ParseOperand();
815 std::unique_ptr<X86Operand> ParseATTOperand();
816 std::unique_ptr<X86Operand> ParseIntelOperand();
817 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Coby Tayreed8912892017-08-24 08:46:25 +0000818 bool ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End);
819 unsigned IdentifyIntelInlineAsmOperator(StringRef Name);
820 unsigned ParseIntelInlineAsmOperator(unsigned OpKind);
Elena Demikhovsky18fd4962015-03-02 15:00:34 +0000821 std::unique_ptr<X86Operand> ParseRoundingModeOp(SMLoc Start, SMLoc End);
Coby Tayree2cb497a2017-04-04 14:43:23 +0000822 bool ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM);
Coby Tayreed8912892017-08-24 08:46:25 +0000823 void RewriteIntelExpression(IntelExprStateMachine &SM, SMLoc Start,
824 SMLoc End);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000825 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Coby Tayreed8912892017-08-24 08:46:25 +0000826 bool ParseIntelInlineAsmIdentifier(const MCExpr *&Val, StringRef &Identifier,
827 InlineAsmIdentifierInfo &Info,
828 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000829
David Blaikie960ea3f2014-06-08 16:18:35 +0000830 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000831
Coby Tayreed8912892017-08-24 08:46:25 +0000832 bool ParseIntelMemoryOperandSize(unsigned &Size);
David Blaikie960ea3f2014-06-08 16:18:35 +0000833 std::unique_ptr<X86Operand>
834 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
835 unsigned IndexReg, unsigned Scale, SMLoc Start,
836 SMLoc End, unsigned Size, StringRef Identifier,
Coby Tayreeef66b3b2017-09-10 12:21:24 +0000837 const InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000838
Michael Zuckerman02ecd432015-12-13 17:07:23 +0000839 bool parseDirectiveEven(SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000840 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000841 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000842
David Blaikie960ea3f2014-06-08 16:18:35 +0000843 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000844
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000845 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
846 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000847 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000848
Chad Rosier49963552012-10-13 00:26:04 +0000849 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000850 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000851 uint64_t &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000852 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000853
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000854 void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands,
855 MCStreamer &Out, bool MatchingInlineAsm);
856
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000857 bool ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000858 bool MatchingInlineAsm);
859
860 bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
861 OperandVector &Operands, MCStreamer &Out,
862 uint64_t &ErrorInfo,
863 bool MatchingInlineAsm);
864
865 bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
866 OperandVector &Operands, MCStreamer &Out,
867 uint64_t &ErrorInfo,
868 bool MatchingInlineAsm);
869
Craig Topperfd38cbe2014-08-30 16:48:34 +0000870 bool OmitRegisterFromClobberLists(unsigned RegNo) override;
Nico Weber42f79db2014-07-17 20:24:55 +0000871
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000872 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
873 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
Michael Zuckerman1bee6342016-10-18 13:52:39 +0000874 /// return false if no parsing errors occurred, true otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000875 bool HandleAVX512Operand(OperandVector &Operands,
876 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000877
Michael Zuckerman1bee6342016-10-18 13:52:39 +0000878 bool ParseZ(std::unique_ptr<X86Operand> &Z, const SMLoc &StartLoc);
879
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000880 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000881 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000882 return getSTI().getFeatureBits()[X86::Mode64Bit];
Evan Cheng4d1ca962011-07-08 01:53:10 +0000883 }
Craig Topper3c80d622014-01-06 04:55:54 +0000884 bool is32BitMode() const {
885 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000886 return getSTI().getFeatureBits()[X86::Mode32Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000887 }
888 bool is16BitMode() const {
889 // FIXME: Can tablegen auto-generate this?
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000890 return getSTI().getFeatureBits()[X86::Mode16Bit];
Craig Topper3c80d622014-01-06 04:55:54 +0000891 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000892 void SwitchMode(unsigned mode) {
Akira Hatanakab11ef082015-11-14 06:35:56 +0000893 MCSubtargetInfo &STI = copySTI();
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000894 FeatureBitset AllModes({X86::Mode64Bit, X86::Mode32Bit, X86::Mode16Bit});
895 FeatureBitset OldMode = STI.getFeatureBits() & AllModes;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000896 unsigned FB = ComputeAvailableFeatures(
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000897 STI.ToggleFeature(OldMode.flip(mode)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000898 setAvailableFeatures(FB);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000899
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000900 assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes));
Evan Cheng481ebb02011-07-27 00:38:12 +0000901 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000902
Reid Kleckner5b37c182014-08-01 20:21:24 +0000903 unsigned getPointerWidth() {
904 if (is16BitMode()) return 16;
905 if (is32BitMode()) return 32;
906 if (is64BitMode()) return 64;
907 llvm_unreachable("invalid mode");
908 }
909
Chad Rosierc2f055d2013-04-18 16:13:18 +0000910 bool isParsingIntelSyntax() {
911 return getParser().getAssemblerDialect();
912 }
913
Daniel Dunbareefe8612010-07-19 05:44:09 +0000914 /// @name Auto-generated Matcher Functions
915 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000916
Chris Lattner3e4582a2010-09-06 19:11:01 +0000917#define GET_ASSEMBLER_HEADER
918#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000919
Daniel Dunbar00331992009-07-29 00:02:19 +0000920 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000921
922public:
Coby Tayree07a89742017-03-21 19:31:55 +0000923
Akira Hatanakab11ef082015-11-14 06:35:56 +0000924 X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser,
Rafael Espindola961d4692014-11-11 05:18:41 +0000925 const MCInstrInfo &mii, const MCTargetOptions &Options)
Nirav Dave6477ce22016-09-26 19:33:36 +0000926 : MCTargetAsmParser(Options, sti), MII(mii), InstInfo(nullptr),
927 Code16GCC(false) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000928
Daniel Dunbareefe8612010-07-19 05:44:09 +0000929 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000930 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000931 Instrumentation.reset(
932 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000933 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000934
Craig Topper39012cc2014-03-09 18:03:14 +0000935 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000936
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000937 void SetFrameRegister(unsigned RegNo) override;
938
David Blaikie960ea3f2014-06-08 16:18:35 +0000939 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
940 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000941
Craig Topper39012cc2014-03-09 18:03:14 +0000942 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000943};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000944} // end anonymous namespace
945
Sean Callanan86c11812010-01-23 00:40:33 +0000946/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000947/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000948
Chris Lattner60db0a62010-02-09 00:34:28 +0000949static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000950
951/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000952
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000953static bool CheckBaseRegAndIndexRegAndScale(unsigned BaseReg, unsigned IndexReg,
954 unsigned Scale, StringRef &ErrMsg) {
Kevin Enderbybc570f22014-01-23 22:34:42 +0000955 // If we have both a base register and an index register make sure they are
956 // both 64-bit or 32-bit registers.
957 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Douglas Katzman0411e862016-10-05 15:23:35 +0000958
959 if ((BaseReg == X86::RIP && IndexReg != 0) || (IndexReg == X86::RIP)) {
960 ErrMsg = "invalid base+index expression";
961 return true;
962 }
Kevin Enderbybc570f22014-01-23 22:34:42 +0000963 if (BaseReg != 0 && IndexReg != 0) {
964 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
965 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
966 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
967 IndexReg != X86::RIZ) {
968 ErrMsg = "base register is 64-bit, but index register is not";
969 return true;
970 }
971 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
972 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
973 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
974 IndexReg != X86::EIZ){
975 ErrMsg = "base register is 32-bit, but index register is not";
976 return true;
977 }
978 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
979 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
980 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
981 ErrMsg = "base register is 16-bit, but index register is not";
982 return true;
983 }
984 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
985 IndexReg != X86::SI && IndexReg != X86::DI) ||
986 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
987 IndexReg != X86::BX && IndexReg != X86::BP)) {
988 ErrMsg = "invalid 16-bit base/index register combination";
989 return true;
990 }
991 }
992 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +0000993 return checkScale(Scale, ErrMsg);
Kevin Enderbybc570f22014-01-23 22:34:42 +0000994}
995
Devang Patel4a6e7782012-01-12 18:03:40 +0000996bool X86AsmParser::ParseRegister(unsigned &RegNo,
997 SMLoc &StartLoc, SMLoc &EndLoc) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000998 MCAsmParser &Parser = getParser();
Chris Lattnercc2ad082010-01-15 18:27:19 +0000999 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001000 const AsmToken &PercentTok = Parser.getTok();
1001 StartLoc = PercentTok.getLoc();
1002
1003 // If we encounter a %, ignore it. This code handles registers with and
1004 // without the prefix, unprefixed registers can occur in cfi directives.
1005 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001006 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001007
Sean Callanan936b0d32010-01-19 21:44:56 +00001008 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001009 EndLoc = Tok.getEndLoc();
1010
Devang Patelce6a2ca2012-01-20 22:32:05 +00001011 if (Tok.isNot(AsmToken::Identifier)) {
Reid Klecknerc990b5d2017-07-24 20:48:15 +00001012 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001013 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001014 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001015 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001016
Kevin Enderby7d912182009-09-03 17:15:07 +00001017 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001018
Chris Lattner1261b812010-09-22 04:11:10 +00001019 // If the match failed, try the register name as lowercase.
1020 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001021 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001022
Michael Kupersteincdb076b2015-07-30 10:10:25 +00001023 // The "flags" register cannot be referenced directly.
1024 // Treat it as an identifier instead.
1025 if (isParsingInlineAsm() && isParsingIntelSyntax() && RegNo == X86::EFLAGS)
1026 RegNo = 0;
1027
Evan Chengeda1d4f2011-07-27 23:22:03 +00001028 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +00001029 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +00001030 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1031 // checked.
1032 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1033 // REX prefix.
1034 if (RegNo == X86::RIZ ||
1035 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1036 X86II::isX86_64NonExtLowByteReg(RegNo) ||
Craig Topper6acca802016-08-27 17:13:37 +00001037 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001038 return Error(StartLoc, "register %"
1039 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001040 SMRange(StartLoc, EndLoc));
Craig Topper29c22732016-02-26 05:29:32 +00001041 } else if (!getSTI().getFeatureBits()[X86::FeatureAVX512]) {
1042 if (X86II::is32ExtendedReg(RegNo))
1043 return Error(StartLoc, "register %"
Craig Topperd50b5f82016-02-26 06:50:24 +00001044 + Tok.getString() + " is only available with AVX512",
Craig Topper29c22732016-02-26 05:29:32 +00001045 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001046 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001047
Chris Lattner1261b812010-09-22 04:11:10 +00001048 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1049 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001050 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001051 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001052
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001053 // Check to see if we have '(4)' after %st.
1054 if (getLexer().isNot(AsmToken::LParen))
1055 return false;
1056 // Lex the paren.
1057 getParser().Lex();
1058
1059 const AsmToken &IntTok = Parser.getTok();
1060 if (IntTok.isNot(AsmToken::Integer))
1061 return Error(IntTok.getLoc(), "expected stack index");
1062 switch (IntTok.getIntVal()) {
1063 case 0: RegNo = X86::ST0; break;
1064 case 1: RegNo = X86::ST1; break;
1065 case 2: RegNo = X86::ST2; break;
1066 case 3: RegNo = X86::ST3; break;
1067 case 4: RegNo = X86::ST4; break;
1068 case 5: RegNo = X86::ST5; break;
1069 case 6: RegNo = X86::ST6; break;
1070 case 7: RegNo = X86::ST7; break;
1071 default: return Error(IntTok.getLoc(), "invalid stack index");
1072 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001073
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001074 if (getParser().Lex().isNot(AsmToken::RParen))
1075 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001076
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001077 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001078 Parser.Lex(); // Eat ')'
1079 return false;
1080 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001081
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001082 EndLoc = Parser.getTok().getEndLoc();
1083
Chris Lattner80486622010-06-24 07:29:18 +00001084 // If this is "db[0-7]", match it as an alias
1085 // for dr[0-7].
1086 if (RegNo == 0 && Tok.getString().size() == 3 &&
1087 Tok.getString().startswith("db")) {
1088 switch (Tok.getString()[2]) {
1089 case '0': RegNo = X86::DR0; break;
1090 case '1': RegNo = X86::DR1; break;
1091 case '2': RegNo = X86::DR2; break;
1092 case '3': RegNo = X86::DR3; break;
1093 case '4': RegNo = X86::DR4; break;
1094 case '5': RegNo = X86::DR5; break;
1095 case '6': RegNo = X86::DR6; break;
1096 case '7': RegNo = X86::DR7; break;
1097 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001098
Chris Lattner80486622010-06-24 07:29:18 +00001099 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001100 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001101 Parser.Lex(); // Eat it.
1102 return false;
1103 }
1104 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001105
Devang Patelce6a2ca2012-01-20 22:32:05 +00001106 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001107 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001108 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001109 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001110 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001111
Sean Callanana83fd7d2010-01-19 20:27:46 +00001112 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001113 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001114}
1115
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001116void X86AsmParser::SetFrameRegister(unsigned RegNo) {
Yuri Gorshenine8c81fd2014-10-07 11:03:09 +00001117 Instrumentation->SetInitialFrameRegister(RegNo);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +00001118}
1119
David Blaikie960ea3f2014-06-08 16:18:35 +00001120std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001121 bool Parse32 = is32BitMode() || Code16GCC;
1122 unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001123 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001124 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001125 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001126 Loc, Loc, 0);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001127}
1128
David Blaikie960ea3f2014-06-08 16:18:35 +00001129std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
Nirav Dave6477ce22016-09-26 19:33:36 +00001130 bool Parse32 = is32BitMode() || Code16GCC;
1131 unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001132 const MCExpr *Disp = MCConstantExpr::create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001133 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
Nirav Dave6477ce22016-09-26 19:33:36 +00001134 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
Craig Topper055845f2015-01-02 07:02:25 +00001135 Loc, Loc, 0);
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001136}
1137
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001138bool X86AsmParser::IsSIReg(unsigned Reg) {
1139 switch (Reg) {
Craig Topper4d187632016-02-26 05:29:39 +00001140 default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001141 case X86::RSI:
1142 case X86::ESI:
1143 case X86::SI:
1144 return true;
1145 case X86::RDI:
1146 case X86::EDI:
1147 case X86::DI:
1148 return false;
1149 }
1150}
1151
1152unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg,
1153 bool IsSIReg) {
1154 switch (RegClassID) {
Craig Topper4d187632016-02-26 05:29:39 +00001155 default: llvm_unreachable("Unexpected register class");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001156 case X86::GR64RegClassID:
1157 return IsSIReg ? X86::RSI : X86::RDI;
1158 case X86::GR32RegClassID:
1159 return IsSIReg ? X86::ESI : X86::EDI;
1160 case X86::GR16RegClassID:
1161 return IsSIReg ? X86::SI : X86::DI;
1162 }
1163}
1164
Michael Kupersteinffcc7662015-07-23 10:23:48 +00001165void X86AsmParser::AddDefaultSrcDestOperands(
1166 OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
1167 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) {
1168 if (isParsingIntelSyntax()) {
1169 Operands.push_back(std::move(Dst));
1170 Operands.push_back(std::move(Src));
1171 }
1172 else {
1173 Operands.push_back(std::move(Src));
1174 Operands.push_back(std::move(Dst));
1175 }
1176}
1177
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001178bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands,
1179 OperandVector &FinalOperands) {
1180
1181 if (OrigOperands.size() > 1) {
Craig Topperd55f4bc2016-02-16 07:45:07 +00001182 // Check if sizes match, OrigOperands also contains the instruction name
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001183 assert(OrigOperands.size() == FinalOperands.size() + 1 &&
Craig Topperd55f4bc2016-02-16 07:45:07 +00001184 "Operand size mismatch");
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001185
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001186 SmallVector<std::pair<SMLoc, std::string>, 2> Warnings;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001187 // Verify types match
1188 int RegClassID = -1;
1189 for (unsigned int i = 0; i < FinalOperands.size(); ++i) {
1190 X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]);
1191 X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]);
1192
1193 if (FinalOp.isReg() &&
1194 (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg()))
1195 // Return false and let a normal complaint about bogus operands happen
1196 return false;
1197
1198 if (FinalOp.isMem()) {
1199
1200 if (!OrigOp.isMem())
1201 // Return false and let a normal complaint about bogus operands happen
1202 return false;
1203
1204 unsigned OrigReg = OrigOp.Mem.BaseReg;
1205 unsigned FinalReg = FinalOp.Mem.BaseReg;
1206
1207 // If we've already encounterd a register class, make sure all register
1208 // bases are of the same register class
1209 if (RegClassID != -1 &&
1210 !X86MCRegisterClasses[RegClassID].contains(OrigReg)) {
1211 return Error(OrigOp.getStartLoc(),
1212 "mismatching source and destination index registers");
1213 }
1214
1215 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg))
1216 RegClassID = X86::GR64RegClassID;
1217 else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg))
1218 RegClassID = X86::GR32RegClassID;
1219 else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg))
1220 RegClassID = X86::GR16RegClassID;
Marina Yatsina701938d2016-01-20 14:03:47 +00001221 else
Craig Topper5a62f7e2016-02-16 07:28:03 +00001222 // Unexpected register class type
Marina Yatsina701938d2016-01-20 14:03:47 +00001223 // Return false and let a normal complaint about bogus operands happen
1224 return false;
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001225
1226 bool IsSI = IsSIReg(FinalReg);
1227 FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI);
1228
1229 if (FinalReg != OrigReg) {
1230 std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI";
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001231 Warnings.push_back(std::make_pair(
1232 OrigOp.getStartLoc(),
1233 "memory operand is only for determining the size, " + RegName +
1234 " will be used for the location"));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001235 }
1236
1237 FinalOp.Mem.Size = OrigOp.Mem.Size;
1238 FinalOp.Mem.SegReg = OrigOp.Mem.SegReg;
1239 FinalOp.Mem.BaseReg = FinalReg;
1240 }
1241 }
1242
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001243 // Produce warnings only if all the operands passed the adjustment - prevent
1244 // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings
Craig Topper16d7eb22016-02-16 07:45:04 +00001245 for (auto &WarningMsg : Warnings) {
1246 Warning(WarningMsg.first, WarningMsg.second);
Marina Yatsinaff262fa2016-01-21 11:37:06 +00001247 }
1248
1249 // Remove old operands
Marina Yatsinab9f4f622016-01-19 15:37:56 +00001250 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1251 OrigOperands.pop_back();
1252 }
1253 // OrigOperands.append(FinalOperands.begin(), FinalOperands.end());
1254 for (unsigned int i = 0; i < FinalOperands.size(); ++i)
1255 OrigOperands.push_back(std::move(FinalOperands[i]));
1256
1257 return false;
1258}
1259
David Blaikie960ea3f2014-06-08 16:18:35 +00001260std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001261 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001262 return ParseIntelOperand();
1263 return ParseATTOperand();
1264}
1265
David Blaikie960ea3f2014-06-08 16:18:35 +00001266std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
1267 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
1268 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
Coby Tayreeef66b3b2017-09-10 12:21:24 +00001269 const InlineAsmIdentifierInfo &Info) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001270 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
1271 // some other label reference.
Coby Tayreec3d24112017-09-29 07:02:46 +00001272 if (Info.isKind(InlineAsmIdentifierInfo::IK_Label)) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001273 // Insert an explicit size if the user didn't have one.
1274 if (!Size) {
1275 Size = getPointerWidth();
Craig Topper7d5b2312015-10-10 05:25:02 +00001276 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
1277 /*Len=*/0, Size);
Reid Kleckner5b37c182014-08-01 20:21:24 +00001278 }
Reid Kleckner5b37c182014-08-01 20:21:24 +00001279 // Create an absolute memory reference in order to match against
1280 // instructions taking a PC relative operand.
Craig Topper055845f2015-01-02 07:02:25 +00001281 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size,
Coby Tayreec3d24112017-09-29 07:02:46 +00001282 Identifier, Info.Label.Decl);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001283 }
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001284 // We either have a direct symbol reference, or an offset from a symbol. The
1285 // parser always puts the symbol on the LHS, so look there for size
1286 // calculation purposes.
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00001287 unsigned FrontendSize = 0;
Coby Tayreec3d24112017-09-29 07:02:46 +00001288 void *Decl = nullptr;
1289 bool IsGlobalLV = false;
1290 if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
1291 // Size is in terms of bits in this context.
1292 FrontendSize = Info.Var.Type * 8;
1293 Decl = Info.Var.Decl;
1294 IsGlobalLV = Info.Var.IsGlobalLV;
1295 }
1296 // It is widely common for MS InlineAsm to use a global variable and one/two
1297 // registers in a mmory expression, and though unaccessible via rip/eip.
1298 if (IsGlobalLV && (BaseReg || IndexReg)) {
1299 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End);
1300 // Otherwise, we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001301 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001302 // get the matching correct in some cases.
Coby Tayreec3d24112017-09-29 07:02:46 +00001303 } else {
1304 BaseReg = BaseReg ? BaseReg : 1;
1305 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1306 IndexReg, Scale, Start, End, Size, Identifier,
1307 Decl, FrontendSize);
1308 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001309}
1310
Coby Tayree2cb497a2017-04-04 14:43:23 +00001311// Some binary bitwise operators have a named synonymous
1312// Query a candidate string for being such a named operator
1313// and if so - invoke the appropriate handler
1314bool X86AsmParser::ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM) {
1315 // A named operator should be either lower or upper case, but not a mix
1316 if (Name.compare(Name.lower()) && Name.compare(Name.upper()))
1317 return false;
1318 if (Name.equals_lower("not"))
1319 SM.onNot();
1320 else if (Name.equals_lower("or"))
1321 SM.onOr();
1322 else if (Name.equals_lower("shl"))
1323 SM.onLShift();
1324 else if (Name.equals_lower("shr"))
1325 SM.onRShift();
1326 else if (Name.equals_lower("xor"))
1327 SM.onXor();
1328 else if (Name.equals_lower("and"))
1329 SM.onAnd();
Coby Tayree41a5b552017-06-27 16:58:27 +00001330 else if (Name.equals_lower("mod"))
1331 SM.onMod();
Coby Tayree2cb497a2017-04-04 14:43:23 +00001332 else
1333 return false;
1334 return true;
1335}
1336
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001337bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001338 MCAsmParser &Parser = getParser();
Chad Rosier6844ea02012-10-24 22:13:37 +00001339 const AsmToken &Tok = Parser.getTok();
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001340 StringRef ErrMsg;
Chad Rosier51afe632012-06-27 22:34:28 +00001341
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001342 AsmToken::TokenKind PrevTK = AsmToken::Error;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001343 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001344 while (!Done) {
1345 bool UpdateLocLex = true;
Andrew V. Tischenkofdb264e2017-05-26 13:23:34 +00001346 AsmToken::TokenKind TK = getLexer().getKind();
Chad Rosier5c118fd2013-01-14 22:31:35 +00001347
David Majnemer6a5b8122014-06-19 01:25:43 +00001348 switch (TK) {
Coby Tayreed8912892017-08-24 08:46:25 +00001349 default:
1350 if ((Done = SM.isValidEndState()))
Chad Rosier5c118fd2013-01-14 22:31:35 +00001351 break;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001352 return Error(Tok.getLoc(), "unknown token in expression");
Coby Tayreed8912892017-08-24 08:46:25 +00001353 case AsmToken::EndOfStatement:
Chad Rosierbfb70992013-04-17 00:11:46 +00001354 Done = true;
1355 break;
Coby Tayreed8912892017-08-24 08:46:25 +00001356 case AsmToken::Real:
1357 // DotOperator: [ebx].0
1358 UpdateLocLex = false;
1359 if (ParseIntelDotOperator(SM, End))
1360 return true;
1361 break;
David Majnemer6a5b8122014-06-19 01:25:43 +00001362 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001363 case AsmToken::Identifier: {
Chad Rosier152749c2013-04-12 18:54:20 +00001364 SMLoc IdentLoc = Tok.getLoc();
1365 StringRef Identifier = Tok.getString();
Coby Tayree07a89742017-03-21 19:31:55 +00001366 UpdateLocLex = false;
Coby Tayreec3d24112017-09-29 07:02:46 +00001367 // Register
1368 unsigned Reg;
1369 if (Tok.isNot(AsmToken::String) && !ParseRegister(Reg, IdentLoc, End)) {
1370 if (SM.onRegister(Reg, ErrMsg))
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001371 return Error(Tok.getLoc(), ErrMsg);
Coby Tayreec3d24112017-09-29 07:02:46 +00001372 break;
1373 }
1374 // Operator synonymous ("not", "or" etc.)
1375 if ((UpdateLocLex = ParseIntelNamedOperator(Identifier, SM)))
1376 break;
1377 // Symbol reference, when parsing assembly content
1378 InlineAsmIdentifierInfo Info;
1379 const MCExpr *Val;
1380 if (!isParsingInlineAsm()) {
1381 if (getParser().parsePrimaryExpr(Val, End)) {
Coby Tayree07a89742017-03-21 19:31:55 +00001382 return Error(Tok.getLoc(), "Unexpected identifier!");
Coby Tayreec3d24112017-09-29 07:02:46 +00001383 } else if (SM.onIdentifierExpr(Val, Identifier, Info, false, ErrMsg)) {
Coby Tayreed8912892017-08-24 08:46:25 +00001384 return Error(IdentLoc, ErrMsg);
Coby Tayreec3d24112017-09-29 07:02:46 +00001385 } else
1386 break;
1387 }
1388 // MS InlineAsm operators (TYPE/LENGTH/SIZE)
1389 if (unsigned OpKind = IdentifyIntelInlineAsmOperator(Identifier)) {
Coby Tayreed8912892017-08-24 08:46:25 +00001390 if (OpKind == IOK_OFFSET)
Coby Tayree07a89742017-03-21 19:31:55 +00001391 return Error(IdentLoc, "Dealing OFFSET operator as part of"
1392 "a compound immediate expression is yet to be supported");
Coby Tayreec3d24112017-09-29 07:02:46 +00001393 if (int64_t Val = ParseIntelInlineAsmOperator(OpKind)) {
1394 if (SM.onInteger(Val, ErrMsg))
1395 return Error(IdentLoc, ErrMsg);
1396 } else
Coby Tayree07a89742017-03-21 19:31:55 +00001397 return true;
Coby Tayreec3d24112017-09-29 07:02:46 +00001398 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001399 }
Coby Tayreec3d24112017-09-29 07:02:46 +00001400 // MS Dot Operator expression
1401 if (Identifier.count('.') && PrevTK == AsmToken::RBrac) {
1402 if (ParseIntelDotOperator(SM, End))
1403 return true;
1404 break;
1405 }
1406 // MS InlineAsm identifier
1407 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, false, End))
1408 return true;
1409 else if (SM.onIdentifierExpr(Val, Identifier, Info, true, ErrMsg))
1410 return Error(IdentLoc, ErrMsg);
Coby Tayree07a89742017-03-21 19:31:55 +00001411 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001412 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001413 case AsmToken::Integer: {
Kevin Enderby36eba252013-12-19 23:16:14 +00001414 // Look for 'b' or 'f' following an Integer as a directional label
1415 SMLoc Loc = getTok().getLoc();
1416 int64_t IntVal = getTok().getIntVal();
1417 End = consumeToken();
1418 UpdateLocLex = false;
1419 if (getLexer().getKind() == AsmToken::Identifier) {
1420 StringRef IDVal = getTok().getString();
1421 if (IDVal == "f" || IDVal == "b") {
1422 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +00001423 getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001424 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Michael Liao5bf95782014-12-04 05:20:33 +00001425 const MCExpr *Val =
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001426 MCSymbolRefExpr::create(Sym, Variant, getContext());
Kevin Enderby36eba252013-12-19 23:16:14 +00001427 if (IDVal == "b" && Sym->isUndefined())
1428 return Error(Loc, "invalid reference to undefined symbol");
1429 StringRef Identifier = Sym->getName();
Coby Tayreec3d24112017-09-29 07:02:46 +00001430 InlineAsmIdentifierInfo Info;
1431 if (SM.onIdentifierExpr(Val, Identifier, Info,
1432 isParsingInlineAsm(), ErrMsg))
Coby Tayreed8912892017-08-24 08:46:25 +00001433 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001434 End = consumeToken();
1435 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001436 if (SM.onInteger(IntVal, ErrMsg))
1437 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001438 }
1439 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001440 if (SM.onInteger(IntVal, ErrMsg))
1441 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001442 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001443 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001444 }
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00001445 case AsmToken::Plus:
1446 if (SM.onPlus(ErrMsg))
1447 return Error(getTok().getLoc(), ErrMsg);
1448 break;
1449 case AsmToken::Minus:
1450 if (SM.onMinus(ErrMsg))
1451 return Error(getTok().getLoc(), ErrMsg);
1452 break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001453 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001454 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001455 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001456 case AsmToken::Pipe: SM.onOr(); break;
Michael Kupersteine3de07a2015-06-14 12:59:45 +00001457 case AsmToken::Caret: SM.onXor(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001458 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001459 case AsmToken::LessLess:
1460 SM.onLShift(); break;
1461 case AsmToken::GreaterGreater:
1462 SM.onRShift(); break;
Coby Tayreed8912892017-08-24 08:46:25 +00001463 case AsmToken::LBrac:
1464 if (SM.onLBrac())
1465 return Error(Tok.getLoc(), "unexpected bracket encountered");
1466 break;
1467 case AsmToken::RBrac:
1468 if (SM.onRBrac())
1469 return Error(Tok.getLoc(), "unexpected bracket encountered");
1470 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001471 case AsmToken::LParen: SM.onLParen(); break;
1472 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001473 }
Chad Rosier31246272013-04-17 21:01:45 +00001474 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001475 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001476
Alp Tokera5b88a52013-12-02 16:06:06 +00001477 if (!Done && UpdateLocLex)
1478 End = consumeToken();
Marina Yatsina8dfd5cb2015-12-24 12:09:51 +00001479
1480 PrevTK = TK;
Devang Patel41b9dde2012-01-17 18:00:18 +00001481 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001482 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001483}
1484
Coby Tayreed8912892017-08-24 08:46:25 +00001485void X86AsmParser::RewriteIntelExpression(IntelExprStateMachine &SM,
1486 SMLoc Start, SMLoc End) {
1487 SMLoc Loc = Start;
1488 unsigned ExprLen = End.getPointer() - Start.getPointer();
1489 // Skip everything before a symbol displacement (if we have one)
1490 if (SM.getSym()) {
1491 StringRef SymName = SM.getSymName();
1492 if (unsigned Len = SymName.data() - Start.getPointer())
1493 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Start, Len);
1494 Loc = SMLoc::getFromPointer(SymName.data() + SymName.size());
1495 ExprLen = End.getPointer() - (SymName.data() + SymName.size());
1496 // If we have only a symbol than there's no need for complex rewrite,
1497 // simply skip everything after it
1498 if (!(SM.getBaseReg() || SM.getIndexReg() || SM.getImm())) {
1499 if (ExprLen)
1500 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Loc, ExprLen);
1501 return;
Nirav Dave8601ac12016-08-02 17:56:03 +00001502 }
1503 }
Coby Tayreed8912892017-08-24 08:46:25 +00001504 // Build an Intel Expression rewrite
1505 StringRef BaseRegStr;
1506 StringRef IndexRegStr;
1507 if (SM.getBaseReg())
1508 BaseRegStr = X86IntelInstPrinter::getRegisterName(SM.getBaseReg());
1509 if (SM.getIndexReg())
1510 IndexRegStr = X86IntelInstPrinter::getRegisterName(SM.getIndexReg());
1511 // Emit it
1512 IntelExpr Expr(BaseRegStr, IndexRegStr, SM.getScale(), SM.getImm(), SM.isMemExpr());
1513 InstInfo->AsmRewrites->emplace_back(Loc, ExprLen, Expr);
Devang Patel41b9dde2012-01-17 18:00:18 +00001514}
1515
Chad Rosier8a244662013-04-02 20:02:33 +00001516// Inline assembly may use variable names with namespace alias qualifiers.
Coby Tayreed8912892017-08-24 08:46:25 +00001517bool X86AsmParser::ParseIntelInlineAsmIdentifier(const MCExpr *&Val,
1518 StringRef &Identifier,
1519 InlineAsmIdentifierInfo &Info,
1520 bool IsUnevaluatedOperand,
1521 SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001522 MCAsmParser &Parser = getParser();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001523 assert(isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001524 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001525
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001526 StringRef LineBuf(Identifier.data());
Coby Tayreec3d24112017-09-29 07:02:46 +00001527 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001528
Chad Rosier8a244662013-04-02 20:02:33 +00001529 const AsmToken &Tok = Parser.getTok();
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001530 SMLoc Loc = Tok.getLoc();
John McCallf73981b2013-05-03 00:15:41 +00001531
1532 // Advance the token stream until the end of the current token is
1533 // after the end of what the frontend claimed.
1534 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001535 do {
John McCallf73981b2013-05-03 00:15:41 +00001536 End = Tok.getEndLoc();
1537 getLexer().Lex();
Reid Klecknerc2b92542015-08-26 21:57:25 +00001538 } while (End.getPointer() < EndPtr);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001539 Identifier = LineBuf;
1540
Reid Klecknerc2b92542015-08-26 21:57:25 +00001541 // The frontend should end parsing on an assembler token boundary, unless it
1542 // failed parsing.
Coby Tayreec3d24112017-09-29 07:02:46 +00001543 assert((End.getPointer() == EndPtr ||
1544 Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) &&
1545 "frontend claimed part of a token?");
Reid Klecknerc2b92542015-08-26 21:57:25 +00001546
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001547 // If the identifier lookup was unsuccessful, assume that we are dealing with
1548 // a label.
Coby Tayreec3d24112017-09-29 07:02:46 +00001549 if (Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) {
Ehsan Akhgaribb6bb072014-09-22 20:40:36 +00001550 StringRef InternalName =
1551 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(),
1552 Loc, false);
1553 assert(InternalName.size() && "We should have an internal name here.");
1554 // Push a rewrite for replacing the identifier name with the internal name.
Craig Topper7d5b2312015-10-10 05:25:02 +00001555 InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(),
1556 InternalName);
Coby Tayreec3d24112017-09-29 07:02:46 +00001557 } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
1558 return false;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001559 // Create the symbol reference.
Jim Grosbach6f482002015-05-18 18:43:14 +00001560 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Chad Rosier8a244662013-04-02 20:02:33 +00001561 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001562 Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001563 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001564}
1565
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001566//ParseRoundingModeOp - Parse AVX-512 rounding mode operand
1567std::unique_ptr<X86Operand>
1568X86AsmParser::ParseRoundingModeOp(SMLoc Start, SMLoc End) {
1569 MCAsmParser &Parser = getParser();
1570 const AsmToken &Tok = Parser.getTok();
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001571 // Eat "{" and mark the current place.
1572 const SMLoc consumedToken = consumeToken();
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001573 if (Tok.getIdentifier().startswith("r")){
1574 int rndMode = StringSwitch<int>(Tok.getIdentifier())
1575 .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT)
1576 .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF)
1577 .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF)
1578 .Case("rz", X86::STATIC_ROUNDING::TO_ZERO)
1579 .Default(-1);
1580 if (-1 == rndMode)
1581 return ErrorOperand(Tok.getLoc(), "Invalid rounding mode.");
1582 Parser.Lex(); // Eat "r*" of r*-sae
1583 if (!getLexer().is(AsmToken::Minus))
1584 return ErrorOperand(Tok.getLoc(), "Expected - at this point");
1585 Parser.Lex(); // Eat "-"
1586 Parser.Lex(); // Eat the sae
1587 if (!getLexer().is(AsmToken::RCurly))
1588 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1589 Parser.Lex(); // Eat "}"
1590 const MCExpr *RndModeOp =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001591 MCConstantExpr::create(rndMode, Parser.getContext());
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001592 return X86Operand::CreateImm(RndModeOp, Start, End);
1593 }
Elena Demikhovsky29792e92015-05-07 11:24:42 +00001594 if(Tok.getIdentifier().equals("sae")){
1595 Parser.Lex(); // Eat the sae
1596 if (!getLexer().is(AsmToken::RCurly))
1597 return ErrorOperand(Tok.getLoc(), "Expected } at this point");
1598 Parser.Lex(); // Eat "}"
1599 return X86Operand::CreateToken("{sae}", consumedToken);
1600 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001601 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
1602}
Chad Rosier91c82662012-10-24 17:22:29 +00001603
Chad Rosier5dcb4662012-10-24 22:21:50 +00001604/// Parse the '.' operator.
Coby Tayreed8912892017-08-24 08:46:25 +00001605bool X86AsmParser::ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End) {
1606 const AsmToken &Tok = getTok();
1607 unsigned Offset;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001608
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001609 // Drop the optional '.'.
1610 StringRef DotDispStr = Tok.getString();
1611 if (DotDispStr.startswith("."))
1612 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001613
Chad Rosier5dcb4662012-10-24 22:21:50 +00001614 // .Imm gets lexed as a real.
1615 if (Tok.is(AsmToken::Real)) {
1616 APInt DotDisp;
1617 DotDispStr.getAsInteger(10, DotDisp);
Coby Tayreed8912892017-08-24 08:46:25 +00001618 Offset = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001619 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001620 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1621 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Coby Tayreed8912892017-08-24 08:46:25 +00001622 Offset))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001623 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosiercc541e82013-04-19 15:57:00 +00001624 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001625 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001626
Coby Tayreed8912892017-08-24 08:46:25 +00001627 // Eat the DotExpression and update End
1628 End = SMLoc::getFromPointer(DotDispStr.data());
1629 const char *DotExprEndLoc = DotDispStr.data() + DotDispStr.size();
1630 while (Tok.getLoc().getPointer() < DotExprEndLoc)
1631 Lex();
1632 SM.addImm(Offset);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001633 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001634}
1635
Chad Rosier91c82662012-10-24 17:22:29 +00001636/// Parse the 'offset' operator. This operator is used to specify the
1637/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001638std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001639 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001640 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001641 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001642 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001643
Chad Rosier91c82662012-10-24 17:22:29 +00001644 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001645 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001646 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001647 StringRef Identifier = Tok.getString();
Coby Tayreed8912892017-08-24 08:46:25 +00001648 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info,
1649 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001650 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001651
Coby Tayreec3d24112017-09-29 07:02:46 +00001652 void *Decl = nullptr;
1653 // FIXME: MS evaluates "offset <Constant>" to the underlying integral
1654 if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
1655 return ErrorOperand(Start, "offset operator cannot yet handle constants");
1656 else if (Info.isKind(InlineAsmIdentifierInfo::IK_Var))
1657 Decl = Info.Var.Decl;
Chad Rosiere2f03772012-10-26 16:09:20 +00001658 // Don't emit the offset operator.
Craig Topper7d5b2312015-10-10 05:25:02 +00001659 InstInfo->AsmRewrites->emplace_back(AOK_Skip, OffsetOfLoc, 7);
Chad Rosiere2f03772012-10-26 16:09:20 +00001660
Chad Rosier91c82662012-10-24 17:22:29 +00001661 // The offset operator will have an 'r' constraint, thus we need to create
1662 // register operand to ensure proper matching. Just pick a GPR based on
1663 // the size of a pointer.
Nirav Dave6477ce22016-09-26 19:33:36 +00001664 bool Parse32 = is32BitMode() || Code16GCC;
1665 unsigned RegNo = is64BitMode() ? X86::RBX : (Parse32 ? X86::EBX : X86::BX);
1666
Chad Rosiera4bc9432013-01-10 22:10:27 +00001667 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Coby Tayreec3d24112017-09-29 07:02:46 +00001668 OffsetOfLoc, Identifier, Decl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001669}
1670
Coby Tayree07a89742017-03-21 19:31:55 +00001671// Query a candidate string for being an Intel assembly operator
1672// Report back its kind, or IOK_INVALID if does not evaluated as a known one
Coby Tayreed8912892017-08-24 08:46:25 +00001673unsigned X86AsmParser::IdentifyIntelInlineAsmOperator(StringRef Name) {
Coby Tayree07a89742017-03-21 19:31:55 +00001674 return StringSwitch<unsigned>(Name)
1675 .Cases("TYPE","type",IOK_TYPE)
1676 .Cases("SIZE","size",IOK_SIZE)
1677 .Cases("LENGTH","length",IOK_LENGTH)
1678 .Cases("OFFSET","offset",IOK_OFFSET)
1679 .Default(IOK_INVALID);
1680}
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001681
1682/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1683/// returns the number of elements in an array. It returns the value 1 for
1684/// non-array variables. The SIZE operator returns the size of a C or C++
1685/// variable. A variable's size is the product of its LENGTH and TYPE. The
1686/// TYPE operator returns the size of a C or C++ type or variable. If the
1687/// variable is an array, TYPE returns the size of a single element.
Coby Tayreed8912892017-08-24 08:46:25 +00001688unsigned X86AsmParser::ParseIntelInlineAsmOperator(unsigned OpKind) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001689 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001690 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001691 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001692
Craig Topper062a2ba2014-04-25 05:30:21 +00001693 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001694 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001695 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001696 StringRef Identifier = Tok.getString();
Coby Tayreed8912892017-08-24 08:46:25 +00001697 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info,
1698 /*Unevaluated=*/true, End))
Coby Tayree07a89742017-03-21 19:31:55 +00001699 return 0;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001700
Coby Tayreec3d24112017-09-29 07:02:46 +00001701 if (!Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
Coby Tayree07a89742017-03-21 19:31:55 +00001702 Error(Start, "unable to lookup expression");
1703 return 0;
1704 }
Coby Tayreed8912892017-08-24 08:46:25 +00001705
Chad Rosierf6675c32013-04-22 17:01:46 +00001706 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001707 switch(OpKind) {
1708 default: llvm_unreachable("Unexpected operand kind!");
Coby Tayreec3d24112017-09-29 07:02:46 +00001709 case IOK_LENGTH: CVal = Info.Var.Length; break;
1710 case IOK_SIZE: CVal = Info.Var.Size; break;
1711 case IOK_TYPE: CVal = Info.Var.Type; break;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001712 }
Eric Christopheradfe5362017-07-25 19:22:09 +00001713
Coby Tayree07a89742017-03-21 19:31:55 +00001714 return CVal;
Chad Rosier11c42f22012-10-26 18:04:20 +00001715}
1716
Coby Tayreed8912892017-08-24 08:46:25 +00001717bool X86AsmParser::ParseIntelMemoryOperandSize(unsigned &Size) {
1718 Size = StringSwitch<unsigned>(getTok().getString())
1719 .Cases("BYTE", "byte", 8)
1720 .Cases("WORD", "word", 16)
1721 .Cases("DWORD", "dword", 32)
Coby Tayree566348f2017-09-28 11:04:08 +00001722 .Cases("FLOAT", "float", 32)
1723 .Cases("LONG", "long", 32)
Coby Tayreed8912892017-08-24 08:46:25 +00001724 .Cases("FWORD", "fword", 48)
Coby Tayree566348f2017-09-28 11:04:08 +00001725 .Cases("DOUBLE", "double", 64)
Coby Tayreed8912892017-08-24 08:46:25 +00001726 .Cases("QWORD", "qword", 64)
1727 .Cases("MMWORD","mmword", 64)
1728 .Cases("XWORD", "xword", 80)
1729 .Cases("TBYTE", "tbyte", 80)
1730 .Cases("XMMWORD", "xmmword", 128)
1731 .Cases("YMMWORD", "ymmword", 256)
1732 .Cases("ZMMWORD", "zmmword", 512)
1733 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
1734 .Default(0);
1735 if (Size) {
1736 const AsmToken &Tok = Lex(); // Eat operand size (e.g., byte, word).
1737 if (!(Tok.getString().equals("PTR") || Tok.getString().equals("ptr")))
1738 return Error(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
1739 Lex(); // Eat ptr.
1740 }
1741 return false;
1742}
1743
David Blaikie960ea3f2014-06-08 16:18:35 +00001744std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001745 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001746 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001747 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001748
Coby Tayree07a89742017-03-21 19:31:55 +00001749 // FIXME: Offset operator
1750 // Should be handled as part of immediate expression, as other operators
1751 // Currently, only supported as a stand-alone operand
1752 if (isParsingInlineAsm())
Coby Tayreed8912892017-08-24 08:46:25 +00001753 if (IdentifyIntelInlineAsmOperator(Tok.getString()) == IOK_OFFSET)
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001754 return ParseIntelOffsetOfOperator();
Chad Rosier11c42f22012-10-26 18:04:20 +00001755
Coby Tayreed8912892017-08-24 08:46:25 +00001756 // Parse optional Size directive.
1757 unsigned Size;
1758 if (ParseIntelMemoryOperandSize(Size))
1759 return nullptr;
1760 bool PtrInOperand = bool(Size);
Nirav Dave8601ac12016-08-02 17:56:03 +00001761
David Majnemeraa34d792013-08-27 21:56:17 +00001762 Start = Tok.getLoc();
1763
Coby Tayreed8912892017-08-24 08:46:25 +00001764 // Rounding mode operand.
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001765 if (getSTI().getFeatureBits()[X86::FeatureAVX512] &&
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001766 getLexer().is(AsmToken::LCurly))
1767 return ParseRoundingModeOp(Start, End);
1768
Coby Tayreed8912892017-08-24 08:46:25 +00001769 // Register operand.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001770 unsigned RegNo = 0;
Coby Tayreed8912892017-08-24 08:46:25 +00001771 if (Tok.is(AsmToken::Identifier) && !ParseRegister(RegNo, Start, End)) {
Douglas Katzman0411e862016-10-05 15:23:35 +00001772 if (RegNo == X86::RIP)
1773 return ErrorOperand(Start, "rip can only be used as a base register");
Coby Tayreed8912892017-08-24 08:46:25 +00001774 // A Register followed by ':' is considered a segment override
1775 if (Tok.isNot(AsmToken::Colon))
1776 return !PtrInOperand ? X86Operand::CreateReg(RegNo, Start, End) :
1777 ErrorOperand(Start, "expected memory operand after 'ptr', "
1778 "found register operand instead");
1779 // An alleged segment override. check if we have a valid segment register
1780 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1781 return ErrorOperand(Start, "invalid segment register");
1782 // Eat ':' and update Start location
1783 Start = Lex().getLoc();
Devang Patel46831de2012-01-12 01:36:43 +00001784 }
1785
Nirav Dave8601ac12016-08-02 17:56:03 +00001786 // Immediates and Memory
Coby Tayreed8912892017-08-24 08:46:25 +00001787 IntelExprStateMachine SM;
Nirav Dave8601ac12016-08-02 17:56:03 +00001788 if (ParseIntelExpression(SM, End))
1789 return nullptr;
1790
Coby Tayreed8912892017-08-24 08:46:25 +00001791 if (isParsingInlineAsm())
1792 RewriteIntelExpression(SM, Start, Tok.getLoc());
1793
Nirav Dave8601ac12016-08-02 17:56:03 +00001794 int64_t Imm = SM.getImm();
Coby Tayreed8912892017-08-24 08:46:25 +00001795 const MCExpr *Disp = SM.getSym();
1796 const MCExpr *ImmDisp = MCConstantExpr::create(Imm, getContext());
1797 if (Disp && Imm)
1798 Disp = MCBinaryExpr::createAdd(Disp, ImmDisp, getContext());
1799 if (!Disp)
1800 Disp = ImmDisp;
Nirav Dave8601ac12016-08-02 17:56:03 +00001801
Coby Tayreed8912892017-08-24 08:46:25 +00001802 // RegNo != 0 specifies a valid segment register,
1803 // and we are parsing a segment override
1804 if (!SM.isMemExpr() && !RegNo)
1805 return X86Operand::CreateImm(Disp, Start, End);
Nirav Dave8601ac12016-08-02 17:56:03 +00001806
Coby Tayreed8912892017-08-24 08:46:25 +00001807 StringRef ErrMsg;
1808 unsigned BaseReg = SM.getBaseReg();
1809 unsigned IndexReg = SM.getIndexReg();
1810 unsigned Scale = SM.getScale();
Nirav Dave8601ac12016-08-02 17:56:03 +00001811
Coby Tayreed8912892017-08-24 08:46:25 +00001812 if ((BaseReg || IndexReg) &&
1813 CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, ErrMsg))
1814 return ErrorOperand(Start, ErrMsg);
1815 if (isParsingInlineAsm())
1816 return CreateMemForInlineAsm(RegNo, Disp, BaseReg, IndexReg,
1817 Scale, Start, End, Size, SM.getSymName(),
1818 SM.getIdentifierInfo());
1819 if (!(BaseReg || IndexReg || RegNo))
1820 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size);
1821 return X86Operand::CreateMem(getPointerWidth(), RegNo, Disp,
1822 BaseReg, IndexReg, Scale, Start, End, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001823}
1824
David Blaikie960ea3f2014-06-08 16:18:35 +00001825std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001826 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001827 switch (getLexer().getKind()) {
1828 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001829 // Parse a memory operand with no segment register.
1830 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001831 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001832 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001833 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001834 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001835 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001836 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001837 Error(Start, "%eiz and %riz can only be used as index registers",
1838 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001839 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001840 }
Douglas Katzman0411e862016-10-05 15:23:35 +00001841 if (RegNo == X86::RIP) {
1842 Error(Start, "%rip can only be used as a base register",
1843 SMRange(Start, End));
1844 return nullptr;
1845 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001846
Chris Lattnerb9270732010-04-17 18:56:34 +00001847 // If this is a segment register followed by a ':', then this is the start
1848 // of a memory reference, otherwise this is a normal register reference.
1849 if (getLexer().isNot(AsmToken::Colon))
1850 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001851
Reid Kleckner0c5da972014-07-31 23:03:22 +00001852 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1853 return ErrorOperand(Start, "invalid segment register");
1854
Chris Lattnerb9270732010-04-17 18:56:34 +00001855 getParser().Lex(); // Eat the colon.
1856 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001857 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001858 case AsmToken::Dollar: {
1859 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001860 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001861 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001862 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001863 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001864 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001865 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001866 }
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001867 case AsmToken::LCurly:{
1868 SMLoc Start = Parser.getTok().getLoc(), End;
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001869 if (getSTI().getFeatureBits()[X86::FeatureAVX512])
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001870 return ParseRoundingModeOp(Start, End);
Nirav Dave8601ac12016-08-02 17:56:03 +00001871 return ErrorOperand(Start, "Unexpected '{' in expression");
Elena Demikhovsky18fd4962015-03-02 15:00:34 +00001872 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001873 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001874}
1875
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001876// true on failure, false otherwise
1877// If no {z} mark was found - Parser doesn't advance
1878bool X86AsmParser::ParseZ(std::unique_ptr<X86Operand> &Z,
1879 const SMLoc &StartLoc) {
1880 MCAsmParser &Parser = getParser();
1881 // Assuming we are just pass the '{' mark, quering the next token
Coby Tayree179ff0e2016-11-20 09:31:11 +00001882 // Searched for {z}, but none was found. Return false, as no parsing error was
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001883 // encountered
1884 if (!(getLexer().is(AsmToken::Identifier) &&
1885 (getLexer().getTok().getIdentifier() == "z")))
1886 return false;
1887 Parser.Lex(); // Eat z
1888 // Query and eat the '}' mark
1889 if (!getLexer().is(AsmToken::RCurly))
1890 return Error(getLexer().getLoc(), "Expected } at this point");
1891 Parser.Lex(); // Eat '}'
1892 // Assign Z with the {z} mark opernad
Benjamin Kramerfc54e352016-11-24 15:17:39 +00001893 Z = X86Operand::CreateToken("{z}", StartLoc);
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001894 return false;
1895}
1896
1897// true on failure, false otherwise
David Blaikie960ea3f2014-06-08 16:18:35 +00001898bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1899 const MCParsedAsmOperand &Op) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001900 MCAsmParser &Parser = getParser();
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001901 if(getSTI().getFeatureBits()[X86::FeatureAVX512]) {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001902 if (getLexer().is(AsmToken::LCurly)) {
1903 // Eat "{" and mark the current place.
1904 const SMLoc consumedToken = consumeToken();
1905 // Distinguish {1to<NUM>} from {%k<NUM>}.
1906 if(getLexer().is(AsmToken::Integer)) {
1907 // Parse memory broadcasting ({1to<NUM>}).
1908 if (getLexer().getTok().getIntVal() != 1)
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001909 return TokError("Expected 1to<NUM> at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001910 Parser.Lex(); // Eat "1" of 1to8
1911 if (!getLexer().is(AsmToken::Identifier) ||
1912 !getLexer().getTok().getIdentifier().startswith("to"))
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001913 return TokError("Expected 1to<NUM> at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001914 // Recognize only reasonable suffixes.
1915 const char *BroadcastPrimitive =
1916 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
Robert Khasanovbfa01312014-07-21 14:54:21 +00001917 .Case("to2", "{1to2}")
1918 .Case("to4", "{1to4}")
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001919 .Case("to8", "{1to8}")
1920 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001921 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001922 if (!BroadcastPrimitive)
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001923 return TokError("Invalid memory broadcast primitive.");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001924 Parser.Lex(); // Eat "toN" of 1toN
1925 if (!getLexer().is(AsmToken::RCurly))
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001926 return TokError("Expected } at this point");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001927 Parser.Lex(); // Eat "}"
1928 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1929 consumedToken));
1930 // No AVX512 specific primitives can pass
1931 // after memory broadcasting, so return.
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001932 return false;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001933 } else {
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001934 // Parse either {k}{z}, {z}{k}, {k} or {z}
1935 // last one have no meaning, but GCC accepts it
1936 // Currently, we're just pass a '{' mark
1937 std::unique_ptr<X86Operand> Z;
1938 if (ParseZ(Z, consumedToken))
1939 return true;
1940 // Reaching here means that parsing of the allegadly '{z}' mark yielded
1941 // no errors.
1942 // Query for the need of further parsing for a {%k<NUM>} mark
1943 if (!Z || getLexer().is(AsmToken::LCurly)) {
Coby Tayree3bfb3652017-08-09 12:32:05 +00001944 SMLoc StartLoc = Z ? consumeToken() : consumedToken;
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001945 // Parse an op-mask register mark ({%k<NUM>}), which is now to be
1946 // expected
Coby Tayree3bfb3652017-08-09 12:32:05 +00001947 unsigned RegNo;
Coby Tayree799fa2c2017-08-13 12:03:00 +00001948 SMLoc RegLoc;
1949 if (!ParseRegister(RegNo, RegLoc, StartLoc) &&
Coby Tayree3bfb3652017-08-09 12:32:05 +00001950 X86MCRegisterClasses[X86::VK1RegClassID].contains(RegNo)) {
Coby Tayree799fa2c2017-08-13 12:03:00 +00001951 if (RegNo == X86::K0)
1952 return Error(RegLoc, "Register k0 can't be used as write mask");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001953 if (!getLexer().is(AsmToken::RCurly))
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001954 return Error(getLexer().getLoc(), "Expected } at this point");
1955 Operands.push_back(X86Operand::CreateToken("{", StartLoc));
Haojian Wuc1cae0b2017-08-09 12:49:20 +00001956 Operands.push_back(
1957 X86Operand::CreateReg(RegNo, StartLoc, StartLoc));
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001958 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1959 } else
1960 return Error(getLexer().getLoc(),
1961 "Expected an op-mask register at this point");
1962 // {%k<NUM>} mark is found, inquire for {z}
1963 if (getLexer().is(AsmToken::LCurly) && !Z) {
1964 // Have we've found a parsing error, or found no (expected) {z} mark
1965 // - report an error
1966 if (ParseZ(Z, consumeToken()) || !Z)
Coby Tayree3bfb3652017-08-09 12:32:05 +00001967 return Error(getLexer().getLoc(),
1968 "Expected a {z} mark at this point");
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001969
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001970 }
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001971 // '{z}' on its own is meaningless, hence should be ignored.
1972 // on the contrary - have it been accompanied by a K register,
1973 // allow it.
1974 if (Z)
1975 Operands.push_back(std::move(Z));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001976 }
1977 }
1978 }
1979 }
Michael Zuckerman1bee6342016-10-18 13:52:39 +00001980 return false;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001981}
1982
Chris Lattnerb9270732010-04-17 18:56:34 +00001983/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1984/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00001985std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
1986 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001987
Rafael Espindola961d4692014-11-11 05:18:41 +00001988 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001989 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1990 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001991 // only way to do this without lookahead is to eat the '(' and see what is
1992 // after it.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001993 const MCExpr *Disp = MCConstantExpr::create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001994 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001995 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00001996 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001997
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001998 // After parsing the base expression we could either have a parenthesized
1999 // memory address or not. If not, return now. If so, eat the (.
2000 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002001 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002002 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002003 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, ExprEnd);
2004 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2005 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002006 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002007
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002008 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002009 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002010 } else {
2011 // Okay, we have a '('. We don't know if this is an expression or not, but
2012 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00002013 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002014 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002015
Kevin Enderby7d912182009-09-03 17:15:07 +00002016 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002017 // Nothing to do here, fall into the code below with the '(' part of the
2018 // memory operand consumed.
2019 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00002020 SMLoc ExprEnd;
Konstantin Belochapka34777112017-09-22 23:37:48 +00002021 getLexer().UnLex(AsmToken(AsmToken::LParen, "("));
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002022
Konstantin Belochapka34777112017-09-22 23:37:48 +00002023 // It must be either an parenthesized expression, or an expression that
2024 // begins from a parenthesized expression, parse it now. Example: (1+2) or
2025 // (1+2)+3
2026 if (getParser().parseExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00002027 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002028
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002029 // After parsing the base expression we could either have a parenthesized
2030 // memory address or not. If not, return now. If so, eat the (.
2031 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002032 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002033 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00002034 return X86Operand::CreateMem(getPointerWidth(), Disp, LParenLoc,
2035 ExprEnd);
2036 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
2037 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002038 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002039
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002040 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002041 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002042 }
2043 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002044
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002045 // If we reached here, then we just ate the ( of the memory operand. Process
2046 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00002047 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00002048 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002049
Chris Lattner0c2538f2010-01-15 18:51:29 +00002050 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002051 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00002052 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00002053 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002054 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002055 Error(StartLoc, "eiz and riz can only be used as index registers",
2056 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00002057 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002058 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00002059 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002060
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002061 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002062 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002063 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002064
2065 // Following the comma we should have either an index register, or a scale
2066 // value. We don't support the later form, but we want to parse it
2067 // correctly.
2068 //
2069 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002070 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00002071 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00002072 SMLoc L;
Douglas Katzman0411e862016-10-05 15:23:35 +00002073 if (ParseRegister(IndexReg, L, L))
2074 return nullptr;
2075 if (BaseReg == X86::RIP) {
2076 Error(IndexLoc, "%rip as base register can not have an index register");
2077 return nullptr;
2078 }
2079 if (IndexReg == X86::RIP) {
2080 Error(IndexLoc, "%rip is not allowed as an index register");
2081 return nullptr;
2082 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002083
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002084 if (getLexer().isNot(AsmToken::RParen)) {
2085 // Parse the scale amount:
2086 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002087 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002088 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002089 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002090 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002091 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00002092 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002093
2094 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002095 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002096
2097 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002098 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00002099 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00002100 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00002101 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002102
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002103 // Validate the scale amount.
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002104 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
David Woodhouse6dbda442014-01-08 12:58:28 +00002105 ScaleVal != 1) {
2106 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00002107 return nullptr;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002108 }
2109 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 &&
2110 ScaleVal != 8) {
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002111 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00002112 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002113 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002114 Scale = (unsigned)ScaleVal;
2115 }
2116 }
2117 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002118 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002119 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00002120 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002121
2122 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002123 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00002124 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002125
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002126 if (Value != 1)
2127 Warning(Loc, "scale factor without index register is ignored");
2128 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002129 }
2130 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002131
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002132 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002133 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002134 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00002135 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002136 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002137 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002138 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002139
David Woodhouse6dbda442014-01-08 12:58:28 +00002140 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
2141 // and then only in non-64-bit modes. Except for DX, which is a special case
2142 // because an unofficial form of in/out instructions uses it.
2143 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
2144 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
2145 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2146 BaseReg != X86::DX) {
2147 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002148 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002149 }
2150 if (BaseReg == 0 &&
2151 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2152 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00002153 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00002154 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00002155
2156 StringRef ErrMsg;
Andrew V. Tischenko32e9b1a2017-07-25 13:05:12 +00002157 if (CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, ErrMsg)) {
Kevin Enderbybc570f22014-01-23 22:34:42 +00002158 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00002159 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002160 }
2161
Reid Klecknerb7e2f602014-07-31 23:26:35 +00002162 if (SegReg || BaseReg || IndexReg)
Craig Topper055845f2015-01-02 07:02:25 +00002163 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
2164 IndexReg, Scale, MemStart, MemEnd);
2165 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002166}
2167
David Blaikie960ea3f2014-06-08 16:18:35 +00002168bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
2169 SMLoc NameLoc, OperandVector &Operands) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002170 MCAsmParser &Parser = getParser();
Chad Rosierf0e87202012-10-25 20:41:34 +00002171 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002172 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002173
Coby Tayree48d67cd2017-07-30 11:12:47 +00002174 if ((Name.equals("jmp") || Name.equals("jc") || Name.equals("jz")) &&
2175 isParsingIntelSyntax() && isParsingInlineAsm()) {
Michael Zuckerman174d2e72016-10-14 08:09:40 +00002176 StringRef NextTok = Parser.getTok().getString();
2177 if (NextTok == "short") {
2178 SMLoc NameEndLoc =
2179 NameLoc.getFromPointer(NameLoc.getPointer() + Name.size());
2180 // Eat the short keyword
2181 Parser.Lex();
2182 // MS ignores the short keyword, it determines the jmp type based
2183 // on the distance of the label
2184 InstInfo->AsmRewrites->emplace_back(AOK_Skip, NameEndLoc,
2185 NextTok.size() + 1);
2186 }
2187 }
2188
Chris Lattner7e8a99b2010-11-28 20:23:50 +00002189 // FIXME: Hack to recognize setneb as setne.
2190 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
2191 PatchedName != "setb" && PatchedName != "setnb")
2192 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00002193
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002194 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002195 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002196 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
2197 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00002198 bool IsVCMP = PatchedName[0] == 'v';
Craig Topper78c424d2015-02-15 07:13:48 +00002199 unsigned CCIdx = IsVCMP ? 4 : 3;
2200 unsigned ComparisonCode = StringSwitch<unsigned>(
2201 PatchedName.slice(CCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00002202 .Case("eq", 0x00)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002203 .Case("eq_oq", 0x00)
Craig Toppera0a603e2012-03-29 07:11:23 +00002204 .Case("lt", 0x01)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002205 .Case("lt_os", 0x01)
Craig Toppera0a603e2012-03-29 07:11:23 +00002206 .Case("le", 0x02)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002207 .Case("le_os", 0x02)
Craig Toppera0a603e2012-03-29 07:11:23 +00002208 .Case("unord", 0x03)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002209 .Case("unord_q", 0x03)
Craig Toppera0a603e2012-03-29 07:11:23 +00002210 .Case("neq", 0x04)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002211 .Case("neq_uq", 0x04)
Craig Toppera0a603e2012-03-29 07:11:23 +00002212 .Case("nlt", 0x05)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002213 .Case("nlt_us", 0x05)
Craig Toppera0a603e2012-03-29 07:11:23 +00002214 .Case("nle", 0x06)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002215 .Case("nle_us", 0x06)
Craig Toppera0a603e2012-03-29 07:11:23 +00002216 .Case("ord", 0x07)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002217 .Case("ord_q", 0x07)
Craig Toppera0a603e2012-03-29 07:11:23 +00002218 /* AVX only from here */
2219 .Case("eq_uq", 0x08)
2220 .Case("nge", 0x09)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002221 .Case("nge_us", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002222 .Case("ngt", 0x0A)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002223 .Case("ngt_us", 0x0A)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002224 .Case("false", 0x0B)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002225 .Case("false_oq", 0x0B)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002226 .Case("neq_oq", 0x0C)
2227 .Case("ge", 0x0D)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002228 .Case("ge_os", 0x0D)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002229 .Case("gt", 0x0E)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002230 .Case("gt_os", 0x0E)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002231 .Case("true", 0x0F)
Michael Zuckerman72b72232016-01-25 08:43:26 +00002232 .Case("true_uq", 0x0F)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002233 .Case("eq_os", 0x10)
2234 .Case("lt_oq", 0x11)
2235 .Case("le_oq", 0x12)
2236 .Case("unord_s", 0x13)
2237 .Case("neq_us", 0x14)
2238 .Case("nlt_uq", 0x15)
2239 .Case("nle_uq", 0x16)
2240 .Case("ord_s", 0x17)
2241 .Case("eq_us", 0x18)
2242 .Case("nge_uq", 0x19)
2243 .Case("ngt_uq", 0x1A)
2244 .Case("false_os", 0x1B)
2245 .Case("neq_os", 0x1C)
2246 .Case("ge_oq", 0x1D)
2247 .Case("gt_oq", 0x1E)
2248 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002249 .Default(~0U);
Craig Topper78c424d2015-02-15 07:13:48 +00002250 if (ComparisonCode != ~0U && (IsVCMP || ComparisonCode < 8)) {
Craig Topper43860832015-02-14 21:54:03 +00002251
Craig Topper78c424d2015-02-15 07:13:48 +00002252 Operands.push_back(X86Operand::CreateToken(PatchedName.slice(0, CCIdx),
Craig Topper43860832015-02-14 21:54:03 +00002253 NameLoc));
2254
Jim Grosbach13760bd2015-05-30 01:25:56 +00002255 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper43860832015-02-14 21:54:03 +00002256 getParser().getContext());
2257 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2258
2259 PatchedName = PatchedName.substr(PatchedName.size() - 2);
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002260 }
2261 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002262
Craig Topper78c424d2015-02-15 07:13:48 +00002263 // FIXME: Hack to recognize vpcmp<comparison code>{ub,uw,ud,uq,b,w,d,q}.
2264 if (PatchedName.startswith("vpcmp") &&
2265 (PatchedName.endswith("b") || PatchedName.endswith("w") ||
2266 PatchedName.endswith("d") || PatchedName.endswith("q"))) {
2267 unsigned CCIdx = PatchedName.drop_back().back() == 'u' ? 2 : 1;
2268 unsigned ComparisonCode = StringSwitch<unsigned>(
2269 PatchedName.slice(5, PatchedName.size() - CCIdx))
2270 .Case("eq", 0x0) // Only allowed on unsigned. Checked below.
2271 .Case("lt", 0x1)
2272 .Case("le", 0x2)
2273 //.Case("false", 0x3) // Not a documented alias.
2274 .Case("neq", 0x4)
2275 .Case("nlt", 0x5)
2276 .Case("nle", 0x6)
2277 //.Case("true", 0x7) // Not a documented alias.
2278 .Default(~0U);
2279 if (ComparisonCode != ~0U && (ComparisonCode != 0 || CCIdx == 2)) {
2280 Operands.push_back(X86Operand::CreateToken("vpcmp", NameLoc));
2281
Jim Grosbach13760bd2015-05-30 01:25:56 +00002282 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper78c424d2015-02-15 07:13:48 +00002283 getParser().getContext());
2284 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2285
2286 PatchedName = PatchedName.substr(PatchedName.size() - CCIdx);
2287 }
2288 }
2289
Craig Topper916708f2015-02-13 07:42:25 +00002290 // FIXME: Hack to recognize vpcom<comparison code>{ub,uw,ud,uq,b,w,d,q}.
2291 if (PatchedName.startswith("vpcom") &&
2292 (PatchedName.endswith("b") || PatchedName.endswith("w") ||
2293 PatchedName.endswith("d") || PatchedName.endswith("q"))) {
Craig Topper78c424d2015-02-15 07:13:48 +00002294 unsigned CCIdx = PatchedName.drop_back().back() == 'u' ? 2 : 1;
2295 unsigned ComparisonCode = StringSwitch<unsigned>(
2296 PatchedName.slice(5, PatchedName.size() - CCIdx))
Craig Topper916708f2015-02-13 07:42:25 +00002297 .Case("lt", 0x0)
2298 .Case("le", 0x1)
2299 .Case("gt", 0x2)
2300 .Case("ge", 0x3)
2301 .Case("eq", 0x4)
2302 .Case("neq", 0x5)
2303 .Case("false", 0x6)
2304 .Case("true", 0x7)
2305 .Default(~0U);
Craig Topper78c424d2015-02-15 07:13:48 +00002306 if (ComparisonCode != ~0U) {
Craig Topper916708f2015-02-13 07:42:25 +00002307 Operands.push_back(X86Operand::CreateToken("vpcom", NameLoc));
2308
Jim Grosbach13760bd2015-05-30 01:25:56 +00002309 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonCode,
Craig Topper916708f2015-02-13 07:42:25 +00002310 getParser().getContext());
2311 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
2312
Craig Topper78c424d2015-02-15 07:13:48 +00002313 PatchedName = PatchedName.substr(PatchedName.size() - CCIdx);
Craig Topper916708f2015-02-13 07:42:25 +00002314 }
2315 }
2316
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002317 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002318
Chris Lattner086a83a2010-09-08 05:17:37 +00002319 // Determine whether this is an instruction prefix.
Coby Tayreec54c5cb2017-08-21 07:50:15 +00002320 // FIXME:
Craig Topper0768bce2017-09-26 21:35:04 +00002321 // Enhance prefixes integrity robustness. for example, following forms
Coby Tayreec54c5cb2017-08-21 07:50:15 +00002322 // are currently tolerated:
2323 // repz repnz <insn> ; GAS errors for the use of two similar prefixes
2324 // lock addq %rax, %rbx ; Destination operand must be of memory type
2325 // xacquire <insn> ; xacquire must be accompanied by 'lock'
2326 bool isPrefix = StringSwitch<bool>(Name)
2327 .Cases("lock",
2328 "rep", "repe",
2329 "repz", "repne",
2330 "repnz", "rex64",
2331 "data32", "data16", true)
2332 .Cases("xacquire", "xrelease", true)
2333 .Cases("acquire", "release", isParsingIntelSyntax())
2334 .Default(false);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002335
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002336 bool CurlyAsEndOfStatement = false;
Chris Lattner086a83a2010-09-08 05:17:37 +00002337 // This does the actual operand parsing. Don't parse any more if we have a
2338 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2339 // just want to parse the "lock" as the first instruction and the "incl" as
2340 // the next one.
2341 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002342
2343 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002344 if (getLexer().is(AsmToken::Star))
2345 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002346
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002347 // Read the operands.
Kirill Bobyrev6afbaf02017-01-18 16:34:25 +00002348 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002349 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
2350 Operands.push_back(std::move(Op));
Michael Zuckerman1bee6342016-10-18 13:52:39 +00002351 if (HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00002352 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002353 } else {
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002354 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00002355 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002356 // check for comma and eat it
2357 if (getLexer().is(AsmToken::Comma))
2358 Parser.Lex();
2359 else
2360 break;
2361 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00002362
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +00002363 // In MS inline asm curly braces mark the beginning/end of a block,
2364 // therefore they should be interepreted as end of statement
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002365 CurlyAsEndOfStatement =
2366 isParsingIntelSyntax() && isParsingInlineAsm() &&
2367 (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly));
2368 if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement)
Nirav Dave2364748a2016-09-16 18:30:20 +00002369 return TokError("unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002370 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002371
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002372 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002373 if (getLexer().is(AsmToken::EndOfStatement) ||
2374 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002375 Parser.Lex();
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002376 else if (CurlyAsEndOfStatement)
2377 // Add an actual EndOfStatement before the curly brace
2378 Info.AsmRewrites->emplace_back(AOK_EndOfStatement,
2379 getLexer().getTok().getLoc(), 0);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002380
Michael Zuckermanfd3fe9e2015-11-12 16:58:51 +00002381 // This is for gas compatibility and cannot be done in td.
2382 // Adding "p" for some floating point with no argument.
2383 // For example: fsub --> fsubp
2384 bool IsFp =
2385 Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr";
2386 if (IsFp && Operands.size() == 1) {
2387 const char *Repl = StringSwitch<const char *>(Name)
2388 .Case("fsub", "fsubp")
2389 .Case("fdiv", "fdivp")
2390 .Case("fsubr", "fsubrp")
2391 .Case("fdivr", "fdivrp");
2392 static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl);
2393 }
2394
Nirav Davef45fd2b2016-08-08 18:01:04 +00002395 // Moving a 32 or 16 bit value into a segment register has the same
2396 // behavior. Modify such instructions to always take shorter form.
2397 if ((Name == "mov" || Name == "movw" || Name == "movl") &&
2398 (Operands.size() == 3)) {
2399 X86Operand &Op1 = (X86Operand &)*Operands[1];
2400 X86Operand &Op2 = (X86Operand &)*Operands[2];
2401 SMLoc Loc = Op1.getEndLoc();
2402 if (Op1.isReg() && Op2.isReg() &&
2403 X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(
2404 Op2.getReg()) &&
2405 (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) ||
2406 X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) {
2407 // Change instruction name to match new instruction.
2408 if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) {
2409 Name = is16BitMode() ? "movw" : "movl";
2410 Operands[0] = X86Operand::CreateToken(Name, NameLoc);
2411 }
2412 // Select the correct equivalent 16-/32-bit source register.
2413 unsigned Reg =
2414 getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32);
2415 Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc);
2416 }
2417 }
2418
Nirav Dave8e103802016-06-29 19:54:27 +00002419 // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" ->
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002420 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2421 // documented form in various unofficial manuals, so a lot of code uses it.
Nirav Dave8e103802016-06-29 19:54:27 +00002422 if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" ||
2423 Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") &&
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002424 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002425 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002426 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2427 isa<MCConstantExpr>(Op.Mem.Disp) &&
2428 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2429 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2430 SMLoc Loc = Op.getEndLoc();
2431 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002432 }
2433 }
Nirav Dave8e103802016-06-29 19:54:27 +00002434 // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al".
2435 if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" ||
2436 Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") &&
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002437 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002438 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002439 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2440 isa<MCConstantExpr>(Op.Mem.Disp) &&
2441 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2442 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2443 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002444 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002445 }
2446 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002447
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002448 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands;
2449 bool HadVerifyError = false;
2450
David Woodhouse4ce66062014-01-22 15:08:55 +00002451 // Append default arguments to "ins[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002452 if (Name.startswith("ins") &&
2453 (Operands.size() == 1 || Operands.size() == 3) &&
2454 (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" ||
2455 Name == "ins")) {
2456
2457 AddDefaultSrcDestOperands(TmpOperands,
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002458 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc),
2459 DefaultMemDIOperand(NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002460 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002461 }
2462
David Woodhousec472b812014-01-22 15:08:49 +00002463 // Append default arguments to "outs[bwld]"
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002464 if (Name.startswith("outs") &&
2465 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhousec472b812014-01-22 15:08:49 +00002466 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002467 Name == "outsd" || Name == "outs")) {
2468 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
Michael Kupersteinffcc7662015-07-23 10:23:48 +00002469 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002470 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002471 }
2472
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002473 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2474 // values of $SIREG according to the mode. It would be nice if this
2475 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002476 if (Name.startswith("lods") &&
2477 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002478 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002479 Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) {
2480 TmpOperands.push_back(DefaultMemSIOperand(NameLoc));
2481 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2482 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002483
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002484 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2485 // values of $DIREG according to the mode. It would be nice if this
2486 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002487 if (Name.startswith("stos") &&
2488 (Operands.size() == 1 || Operands.size() == 2) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002489 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002490 Name == "stosl" || Name == "stosd" || Name == "stosq")) {
2491 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2492 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2493 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002494
David Woodhouse20fe4802014-01-22 15:08:27 +00002495 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2496 // values of $DIREG according to the mode. It would be nice if this
2497 // could be achieved with InstAlias in the tables.
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002498 if (Name.startswith("scas") &&
2499 (Operands.size() == 1 || Operands.size() == 2) &&
David Woodhouse20fe4802014-01-22 15:08:27 +00002500 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002501 Name == "scasl" || Name == "scasd" || Name == "scasq")) {
2502 TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
2503 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2504 }
David Woodhouse20fe4802014-01-22 15:08:27 +00002505
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002506 // Add default SI and DI operands to "cmps[bwlq]".
2507 if (Name.startswith("cmps") &&
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002508 (Operands.size() == 1 || Operands.size() == 3) &&
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002509 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2510 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002511 AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc),
2512 DefaultMemSIOperand(NameLoc));
2513 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002514 }
2515
David Woodhouse6f417de2014-01-22 15:08:42 +00002516 // Add default SI and DI operands to "movs[bwlq]".
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002517 if (((Name.startswith("movs") &&
2518 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2519 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2520 (Name.startswith("smov") &&
2521 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2522 Name == "smovl" || Name == "smovd" || Name == "smovq"))) &&
2523 (Operands.size() == 1 || Operands.size() == 3)) {
Coby Tayree94ddbb42016-11-21 15:50:56 +00002524 if (Name == "movsd" && Operands.size() == 1 && !isParsingIntelSyntax())
Marina Yatsinab9f4f622016-01-19 15:37:56 +00002525 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2526 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
2527 DefaultMemDIOperand(NameLoc));
2528 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
2529 }
2530
2531 // Check if we encountered an error for one the string insturctions
2532 if (HadVerifyError) {
2533 return HadVerifyError;
David Woodhouse6f417de2014-01-22 15:08:42 +00002534 }
2535
Chris Lattner4bd21712010-09-15 04:33:27 +00002536 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002537 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002538 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002539 Name.startswith("shl") || Name.startswith("sal") ||
2540 Name.startswith("rcl") || Name.startswith("rcr") ||
2541 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002542 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002543 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002544 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002545 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2546 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2547 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002548 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002549 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002550 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2551 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2552 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002553 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002554 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002555 }
Chad Rosier51afe632012-06-27 22:34:28 +00002556
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002557 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2558 // instalias with an immediate operand yet.
2559 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002560 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
Duncan P. N. Exon Smithd5313222015-07-23 19:27:07 +00002561 if (Op1.isImm())
2562 if (auto *CE = dyn_cast<MCConstantExpr>(Op1.getImm()))
2563 if (CE->getValue() == 3) {
2564 Operands.erase(Operands.begin() + 1);
2565 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
2566 }
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002567 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002568
Marina Yatsinad9658d12016-01-19 16:35:38 +00002569 // Transforms "xlat mem8" into "xlatb"
2570 if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) {
2571 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2572 if (Op1.isMem8()) {
2573 Warning(Op1.getStartLoc(), "memory operand is only for determining the "
2574 "size, (R|E)BX will be used for the location");
2575 Operands.pop_back();
2576 static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb");
2577 }
2578 }
2579
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002580 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002581}
2582
David Blaikie960ea3f2014-06-08 16:18:35 +00002583bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Aaron Ballmana81264b2016-05-23 15:52:59 +00002584 return false;
Devang Patelde47cce2012-01-18 22:42:29 +00002585}
2586
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002587static const char *getSubtargetFeatureName(uint64_t Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002588
David Blaikie960ea3f2014-06-08 16:18:35 +00002589void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2590 MCStreamer &Out) {
Evgeniy Stepanov77ad8662014-07-31 09:11:04 +00002591 Instrumentation->InstrumentAndEmitInstruction(Inst, Operands, getContext(),
2592 MII, Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002593}
2594
David Blaikie960ea3f2014-06-08 16:18:35 +00002595bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2596 OperandVector &Operands,
Tim Northover26bb14e2014-08-18 11:49:42 +00002597 MCStreamer &Out, uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00002598 bool MatchingInlineAsm) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002599 if (isParsingIntelSyntax())
2600 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002601 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002602 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002603 MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002604}
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002605
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002606void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
2607 OperandVector &Operands, MCStreamer &Out,
2608 bool MatchingInlineAsm) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002609 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002610 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002611 // call.
Reid Klecknerb1f2d2f2014-07-31 00:07:33 +00002612 const char *Repl = StringSwitch<const char *>(Op.getToken())
2613 .Case("finit", "fninit")
2614 .Case("fsave", "fnsave")
2615 .Case("fstcw", "fnstcw")
2616 .Case("fstcww", "fnstcw")
2617 .Case("fstenv", "fnstenv")
2618 .Case("fstsw", "fnstsw")
2619 .Case("fstsww", "fnstsw")
2620 .Case("fclex", "fnclex")
2621 .Default(nullptr);
2622 if (Repl) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002623 MCInst Inst;
2624 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002625 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002626 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002627 EmitInstruction(Inst, Operands, Out);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002628 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002629 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002630}
2631
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002632bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002633 bool MatchingInlineAsm) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002634 assert(ErrorInfo && "Unknown missing feature!");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002635 SmallString<126> Msg;
2636 raw_svector_ostream OS(Msg);
2637 OS << "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002638 uint64_t Mask = 1;
2639 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2640 if (ErrorInfo & Mask)
2641 OS << ' ' << getSubtargetFeatureName(ErrorInfo & Mask);
2642 Mask <<= 1;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002643 }
Nirav Dave2364748a2016-09-16 18:30:20 +00002644 return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002645}
2646
2647bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
2648 OperandVector &Operands,
2649 MCStreamer &Out,
2650 uint64_t &ErrorInfo,
2651 bool MatchingInlineAsm) {
2652 assert(!Operands.empty() && "Unexpect empty operand list!");
2653 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2654 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
Nirav Dave2364748a2016-09-16 18:30:20 +00002655 SMRange EmptyRange = None;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002656
2657 // First, handle aliases that expand to multiple instructions.
2658 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002659
Chris Lattner628fbec2010-09-06 21:54:15 +00002660 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002661 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002662
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002663 // First, try a direct match.
Nirav Dave6477ce22016-09-26 19:33:36 +00002664 switch (MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
2665 isParsingIntelSyntax())) {
Craig Topper589ceee2015-01-03 08:16:34 +00002666 default: llvm_unreachable("Unexpected match result!");
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002667 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002668 // Some instructions need post-processing to, for example, tweak which
2669 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002670 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002671 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002672 while (processInstruction(Inst, Operands))
2673 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002674
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002675 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002676 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002677 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002678 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002679 return false;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002680 case Match_MissingFeature:
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002681 return ErrorMissingFeature(IDLoc, ErrorInfo, MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002682 case Match_InvalidOperand:
2683 WasOriginallyInvalidOperand = true;
2684 break;
2685 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002686 break;
2687 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002688
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002689 // FIXME: Ideally, we would only attempt suffix matches for things which are
2690 // valid prefixes, and we could just infer the right unambiguous
2691 // type. However, that requires substantially more matcher support than the
2692 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002693
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002694 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002695 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002696 SmallString<16> Tmp;
2697 Tmp += Base;
2698 Tmp += ' ';
Yaron Keren075759a2015-03-30 15:42:36 +00002699 Op.setTokenValue(Tmp);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002700
Chris Lattnerfab94132010-11-06 18:28:02 +00002701 // If this instruction starts with an 'f', then it is a floating point stack
2702 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2703 // 80-bit floating point, which use the suffixes s,l,t respectively.
2704 //
2705 // Otherwise, we assume that this may be an integer instruction, which comes
2706 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2707 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002708
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002709 // Check for the various suffix matches.
Tim Northover26bb14e2014-08-18 11:49:42 +00002710 uint64_t ErrorInfoIgnore;
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002711 uint64_t ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002712 unsigned Match[4];
Chad Rosier51afe632012-06-27 22:34:28 +00002713
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002714 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
2715 Tmp.back() = Suffixes[I];
Nirav Dave6477ce22016-09-26 19:33:36 +00002716 Match[I] = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2717 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002718 // If this returned as a missing feature failure, remember that.
2719 if (Match[I] == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002720 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002721 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002722
2723 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002724 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002725
2726 // If exactly one matched, then we treat that as a successful match (and the
2727 // instruction will already have been filled in correctly, since the failing
2728 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002729 unsigned NumSuccessfulMatches =
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002730 std::count(std::begin(Match), std::end(Match), Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002731 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002732 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002733 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002734 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002735 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002736 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002737 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002738
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002739 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002740
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002741 // If we had multiple suffix matches, then identify this as an ambiguous
2742 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002743 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002744 char MatchChars[4];
2745 unsigned NumMatches = 0;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002746 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
2747 if (Match[I] == Match_Success)
2748 MatchChars[NumMatches++] = Suffixes[I];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002749
Alp Tokere69170a2014-06-26 22:52:05 +00002750 SmallString<126> Msg;
2751 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002752 OS << "ambiguous instructions require an explicit suffix (could be ";
2753 for (unsigned i = 0; i != NumMatches; ++i) {
2754 if (i != 0)
2755 OS << ", ";
2756 if (i + 1 == NumMatches)
2757 OS << "or ";
2758 OS << "'" << Base << MatchChars[i] << "'";
2759 }
2760 OS << ")";
Nirav Dave2364748a2016-09-16 18:30:20 +00002761 Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002762 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002763 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002764
Chris Lattner628fbec2010-09-06 21:54:15 +00002765 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002766
Chris Lattner628fbec2010-09-06 21:54:15 +00002767 // If all of the instructions reported an invalid mnemonic, then the original
2768 // mnemonic was invalid.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002769 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002770 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002771 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002772 Op.getLocRange(), MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002773 }
2774
2775 // Recover location info for the operand if we know which was the problem.
Tim Northover26bb14e2014-08-18 11:49:42 +00002776 if (ErrorInfo != ~0ULL) {
Chad Rosier49963552012-10-13 00:26:04 +00002777 if (ErrorInfo >= Operands.size())
Nirav Dave2364748a2016-09-16 18:30:20 +00002778 return Error(IDLoc, "too few operands for instruction", EmptyRange,
2779 MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002780
David Blaikie960ea3f2014-06-08 16:18:35 +00002781 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2782 if (Operand.getStartLoc().isValid()) {
2783 SMRange OperandRange = Operand.getLocRange();
2784 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002785 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002786 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002787 }
2788
Nirav Dave2364748a2016-09-16 18:30:20 +00002789 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Chad Rosier4453e842012-10-12 23:09:25 +00002790 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002791 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002792
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002793 // If one instruction matched with a missing feature, report this as a
2794 // missing feature.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002795 if (std::count(std::begin(Match), std::end(Match),
2796 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002797 ErrorInfo = ErrorInfoMissingFeature;
2798 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002799 MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002800 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002801
Chris Lattner628fbec2010-09-06 21:54:15 +00002802 // If one instruction matched with an invalid operand, report this as an
2803 // operand failure.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002804 if (std::count(std::begin(Match), std::end(Match),
2805 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002806 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002807 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002808 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002809
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002810 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002811 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Nirav Dave2364748a2016-09-16 18:30:20 +00002812 EmptyRange, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002813 return true;
2814}
2815
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002816bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
2817 OperandVector &Operands,
2818 MCStreamer &Out,
2819 uint64_t &ErrorInfo,
2820 bool MatchingInlineAsm) {
2821 assert(!Operands.empty() && "Unexpect empty operand list!");
2822 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2823 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
2824 StringRef Mnemonic = Op.getToken();
Nirav Dave2364748a2016-09-16 18:30:20 +00002825 SMRange EmptyRange = None;
Nirav Daveee554e62016-10-06 15:28:08 +00002826 StringRef Base = Op.getToken();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002827
2828 // First, handle aliases that expand to multiple instructions.
2829 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
2830
2831 MCInst Inst;
2832
2833 // Find one unsized memory operand, if present.
2834 X86Operand *UnsizedMemOp = nullptr;
2835 for (const auto &Op : Operands) {
2836 X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00002837 if (X86Op->isMemUnsized()) {
2838 UnsizedMemOp = X86Op;
Coby Tayree49b37332016-11-22 09:30:29 +00002839 // Have we found an unqualified memory operand,
2840 // break. IA allows only one memory operand.
2841 break;
2842 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002843 }
2844
2845 // Allow some instructions to have implicitly pointer-sized operands. This is
2846 // compatible with gas.
2847 if (UnsizedMemOp) {
2848 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"};
2849 for (const char *Instr : PtrSizedInstrs) {
2850 if (Mnemonic == Instr) {
Craig Topper055845f2015-01-02 07:02:25 +00002851 UnsizedMemOp->Mem.Size = getPointerWidth();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002852 break;
2853 }
2854 }
2855 }
2856
Nirav Daveee554e62016-10-06 15:28:08 +00002857 SmallVector<unsigned, 8> Match;
2858 uint64_t ErrorInfoMissingFeature = 0;
2859
2860 // If unsized push has immediate operand we should default the default pointer
2861 // size for the size.
2862 if (Mnemonic == "push" && Operands.size() == 2) {
2863 auto *X86Op = static_cast<X86Operand *>(Operands[1].get());
2864 if (X86Op->isImm()) {
2865 // If it's not a constant fall through and let remainder take care of it.
2866 const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm());
2867 unsigned Size = getPointerWidth();
2868 if (CE &&
2869 (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) {
2870 SmallString<16> Tmp;
2871 Tmp += Base;
2872 Tmp += (is64BitMode())
2873 ? "q"
2874 : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " ";
2875 Op.setTokenValue(Tmp);
2876 // Do match in ATT mode to allow explicit suffix usage.
2877 Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo,
2878 MatchingInlineAsm,
2879 false /*isParsingIntelSyntax()*/));
2880 Op.setTokenValue(Base);
2881 }
2882 }
2883 }
2884
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002885 // If an unsized memory operand is present, try to match with each memory
2886 // operand size. In Intel assembly, the size is not part of the instruction
2887 // mnemonic.
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002888 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) {
Ahmed Bougachad65f7872014-12-03 02:03:26 +00002889 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512};
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002890 for (unsigned Size : MopSizes) {
2891 UnsizedMemOp->Mem.Size = Size;
2892 uint64_t ErrorInfoIgnore;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002893 unsigned LastOpcode = Inst.getOpcode();
Nirav Dave6477ce22016-09-26 19:33:36 +00002894 unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
2895 MatchingInlineAsm, isParsingIntelSyntax());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002896 if (Match.empty() || LastOpcode != Inst.getOpcode())
2897 Match.push_back(M);
2898
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002899 // If this returned as a missing feature failure, remember that.
2900 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002901 ErrorInfoMissingFeature = ErrorInfoIgnore;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002902 }
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002903
2904 // Restore the size of the unsized memory operand if we modified it.
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00002905 UnsizedMemOp->Mem.Size = 0;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002906 }
2907
2908 // If we haven't matched anything yet, this is not a basic integer or FPU
Saleem Abdulrasoolc3f8ad32015-01-16 20:16:06 +00002909 // operation. There shouldn't be any ambiguity in our mnemonic table, so try
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002910 // matching with the unsized operand.
2911 if (Match.empty()) {
Nirav Dave6477ce22016-09-26 19:33:36 +00002912 Match.push_back(MatchInstruction(
2913 Operands, Inst, ErrorInfo, MatchingInlineAsm, isParsingIntelSyntax()));
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002914 // If this returned as a missing feature failure, remember that.
2915 if (Match.back() == Match_MissingFeature)
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002916 ErrorInfoMissingFeature = ErrorInfo;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002917 }
2918
2919 // Restore the size of the unsized memory operand if we modified it.
2920 if (UnsizedMemOp)
2921 UnsizedMemOp->Mem.Size = 0;
2922
2923 // If it's a bad mnemonic, all results will be the same.
2924 if (Match.back() == Match_MnemonicFail) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002925 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'",
Nirav Dave2364748a2016-09-16 18:30:20 +00002926 Op.getLocRange(), MatchingInlineAsm);
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002927 }
2928
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00002929 unsigned NumSuccessfulMatches =
2930 std::count(std::begin(Match), std::end(Match), Match_Success);
2931
2932 // If matching was ambiguous and we had size information from the frontend,
2933 // try again with that. This handles cases like "movxz eax, m8/m16".
2934 if (UnsizedMemOp && NumSuccessfulMatches > 1 &&
2935 UnsizedMemOp->getMemFrontendSize()) {
2936 UnsizedMemOp->Mem.Size = UnsizedMemOp->getMemFrontendSize();
2937 unsigned M = MatchInstruction(
2938 Operands, Inst, ErrorInfo, MatchingInlineAsm, isParsingIntelSyntax());
2939 if (M == Match_Success)
2940 NumSuccessfulMatches = 1;
2941
2942 // Add a rewrite that encodes the size information we used from the
2943 // frontend.
2944 InstInfo->AsmRewrites->emplace_back(
2945 AOK_SizeDirective, UnsizedMemOp->getStartLoc(),
2946 /*Len=*/0, UnsizedMemOp->getMemFrontendSize());
2947 }
2948
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002949 // If exactly one matched, then we treat that as a successful match (and the
2950 // instruction will already have been filled in correctly, since the failing
2951 // matches won't have modified it).
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002952 if (NumSuccessfulMatches == 1) {
2953 // Some instructions need post-processing to, for example, tweak which
2954 // encoding is selected. Loop on it while changes happen so the individual
2955 // transformations can chain off each other.
2956 if (!MatchingInlineAsm)
2957 while (processInstruction(Inst, Operands))
2958 ;
2959 Inst.setLoc(IDLoc);
2960 if (!MatchingInlineAsm)
2961 EmitInstruction(Inst, Operands, Out);
2962 Opcode = Inst.getOpcode();
2963 return false;
2964 } else if (NumSuccessfulMatches > 1) {
2965 assert(UnsizedMemOp &&
2966 "multiple matches only possible with unsized memory operands");
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002967 return Error(UnsizedMemOp->getStartLoc(),
2968 "ambiguous operand size for instruction '" + Mnemonic + "\'",
Reid Kleckner6d2ea6e2017-05-04 18:19:52 +00002969 UnsizedMemOp->getLocRange());
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002970 }
2971
2972 // If one instruction matched with a missing feature, report this as a
2973 // missing feature.
2974 if (std::count(std::begin(Match), std::end(Match),
2975 Match_MissingFeature) == 1) {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00002976 ErrorInfo = ErrorInfoMissingFeature;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002977 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
2978 MatchingInlineAsm);
2979 }
2980
2981 // If one instruction matched with an invalid operand, report this as an
2982 // operand failure.
2983 if (std::count(std::begin(Match), std::end(Match),
2984 Match_InvalidOperand) == 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002985 return Error(IDLoc, "invalid operand for instruction", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002986 MatchingInlineAsm);
2987 }
2988
2989 // If all of these were an outright failure, report it in a useless way.
Nirav Dave2364748a2016-09-16 18:30:20 +00002990 return Error(IDLoc, "unknown instruction mnemonic", EmptyRange,
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002991 MatchingInlineAsm);
2992}
2993
Nico Weber42f79db2014-07-17 20:24:55 +00002994bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
2995 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
2996}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002997
Devang Patel4a6e7782012-01-12 18:03:40 +00002998bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002999 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00003000 StringRef IDVal = DirectiveID.getIdentifier();
3001 if (IDVal == ".word")
3002 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00003003 else if (IDVal.startswith(".code"))
3004 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00003005 else if (IDVal.startswith(".att_syntax")) {
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +00003006 getParser().setParsingInlineAsm(false);
Reid Klecknerce63b792014-08-06 23:21:13 +00003007 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3008 if (Parser.getTok().getString() == "prefix")
3009 Parser.Lex();
3010 else if (Parser.getTok().getString() == "noprefix")
3011 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
3012 "supported: registers must have a "
3013 "'%' prefix in .att_syntax");
3014 }
Chad Rosier6f8d8b22012-09-10 20:54:39 +00003015 getParser().setAssemblerDialect(0);
3016 return false;
3017 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00003018 getParser().setAssemblerDialect(1);
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +00003019 getParser().setParsingInlineAsm(true);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00003020 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003021 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00003022 Parser.Lex();
Reid Klecknerce63b792014-08-06 23:21:13 +00003023 else if (Parser.getTok().getString() == "prefix")
3024 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
3025 "supported: registers must not have "
3026 "a '%' prefix in .intel_syntax");
Devang Patel9a9bb5c2012-01-30 20:02:42 +00003027 }
3028 return false;
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003029 } else if (IDVal == ".even")
3030 return parseDirectiveEven(DirectiveID.getLoc());
Chris Lattner72c0b592010-10-30 17:38:55 +00003031 return true;
3032}
3033
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003034/// parseDirectiveEven
3035/// ::= .even
3036bool X86AsmParser::parseDirectiveEven(SMLoc L) {
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003037 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3038 TokError("unexpected token in directive");
3039 return false;
3040 }
Eric Christopher445c9522016-10-14 05:47:37 +00003041 const MCSection *Section = getStreamer().getCurrentSectionOnly();
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003042 if (!Section) {
3043 getStreamer().InitSections(false);
Eric Christopher445c9522016-10-14 05:47:37 +00003044 Section = getStreamer().getCurrentSectionOnly();
Michael Zuckerman02ecd432015-12-13 17:07:23 +00003045 }
3046 if (Section->UseCodeAlign())
3047 getStreamer().EmitCodeAlignment(2, 0);
3048 else
3049 getStreamer().EmitValueToAlignment(2, 0, 1, 0);
3050 return false;
3051}
Chris Lattner72c0b592010-10-30 17:38:55 +00003052/// ParseDirectiveWord
3053/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00003054bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003055 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00003056 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3057 for (;;) {
3058 const MCExpr *Value;
David Majnemera375b262015-10-26 02:45:50 +00003059 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003060 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003061 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00003062
David Majnemera375b262015-10-26 02:45:50 +00003063 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
3064 assert(Size <= 8 && "Invalid size");
3065 uint64_t IntValue = MCE->getValue();
3066 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
3067 return Error(ExprLoc, "literal value out of range for directive");
3068 getStreamer().EmitIntValue(IntValue, Size);
3069 } else {
3070 getStreamer().EmitValue(Value, Size, ExprLoc);
3071 }
Chad Rosier51afe632012-06-27 22:34:28 +00003072
Chris Lattner72c0b592010-10-30 17:38:55 +00003073 if (getLexer().is(AsmToken::EndOfStatement))
3074 break;
Chad Rosier51afe632012-06-27 22:34:28 +00003075
Chris Lattner72c0b592010-10-30 17:38:55 +00003076 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003077 if (getLexer().isNot(AsmToken::Comma)) {
3078 Error(L, "unexpected token in directive");
3079 return false;
3080 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003081 Parser.Lex();
3082 }
3083 }
Chad Rosier51afe632012-06-27 22:34:28 +00003084
Chris Lattner72c0b592010-10-30 17:38:55 +00003085 Parser.Lex();
3086 return false;
3087}
3088
Evan Cheng481ebb02011-07-27 00:38:12 +00003089/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00003090/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00003091bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00003092 MCAsmParser &Parser = getParser();
Nirav Dave6477ce22016-09-26 19:33:36 +00003093 Code16GCC = false;
Craig Topper3c80d622014-01-06 04:55:54 +00003094 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00003095 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00003096 if (!is16BitMode()) {
3097 SwitchMode(X86::Mode16Bit);
3098 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3099 }
Nirav Dave6477ce22016-09-26 19:33:36 +00003100 } else if (IDVal == ".code16gcc") {
3101 // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode.
3102 Parser.Lex();
3103 Code16GCC = true;
3104 if (!is16BitMode()) {
3105 SwitchMode(X86::Mode16Bit);
3106 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
3107 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003108 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00003109 Parser.Lex();
3110 if (!is32BitMode()) {
3111 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003112 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
3113 }
3114 } else if (IDVal == ".code64") {
3115 Parser.Lex();
3116 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00003117 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00003118 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
3119 }
3120 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00003121 Error(L, "unknown directive " + IDVal);
3122 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00003123 }
Chris Lattner72c0b592010-10-30 17:38:55 +00003124
Evan Cheng481ebb02011-07-27 00:38:12 +00003125 return false;
3126}
Chris Lattner72c0b592010-10-30 17:38:55 +00003127
Daniel Dunbar71475772009-07-17 20:42:00 +00003128// Force static initialization.
3129extern "C" void LLVMInitializeX86AsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00003130 RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target());
3131 RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target());
Daniel Dunbar71475772009-07-17 20:42:00 +00003132}
Daniel Dunbar00331992009-07-29 00:02:19 +00003133
Chris Lattner3e4582a2010-09-06 19:11:01 +00003134#define GET_REGISTER_MATCHER
3135#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00003136#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00003137#include "X86GenAsmMatcher.inc"