blob: d743aa6ef33343302de45ddbcfdd60383b5b8dac [file] [log] [blame]
Daniel Dunbar71475772009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng11424442011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000011#include "X86AsmInstrumentation.h"
Evgeniy Stepanove3804d42014-02-28 12:28:07 +000012#include "X86AsmParserCommon.h"
13#include "X86Operand.h"
Chad Rosier6844ea02012-10-24 22:13:37 +000014#include "llvm/ADT/APFloat.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000015#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000018#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000020#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000023#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCParser/MCAsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSubtargetInfo.h"
30#include "llvm/MC/MCSymbol.h"
31#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000032#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000034#include "llvm/Support/raw_ostream.h"
Reid Kleckner7b1e1a02014-07-30 22:23:11 +000035#include <algorithm>
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000036#include <memory>
Evan Cheng4d1ca962011-07-08 01:53:10 +000037
Daniel Dunbar71475772009-07-17 20:42:00 +000038using namespace llvm;
39
40namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000041
Chad Rosier5362af92013-04-16 18:15:40 +000042static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000043 0, // IC_OR
44 1, // IC_AND
Kevin Enderbyd6b10712014-02-06 01:21:15 +000045 2, // IC_LSHIFT
46 2, // IC_RSHIFT
47 3, // IC_PLUS
48 3, // IC_MINUS
49 4, // IC_MULTIPLY
50 4, // IC_DIVIDE
51 5, // IC_RPAREN
52 6, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000053 0, // IC_IMM
54 0 // IC_REGISTER
55};
56
Devang Patel4a6e7782012-01-12 18:03:40 +000057class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000058 MCSubtargetInfo &STI;
Evgeniy Stepanovf4a36992014-04-24 13:29:34 +000059 const MCInstrInfo &MII;
Chad Rosierf0e87202012-10-25 20:41:34 +000060 ParseInstructionInfo *InstInfo;
Evgeniy Stepanov49e26252014-03-14 08:58:04 +000061 std::unique_ptr<X86AsmInstrumentation> Instrumentation;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000062private:
Alp Tokera5b88a52013-12-02 16:06:06 +000063 SMLoc consumeToken() {
Rafael Espindola961d4692014-11-11 05:18:41 +000064 MCAsmParser &Parser = getParser();
Alp Tokera5b88a52013-12-02 16:06:06 +000065 SMLoc Result = Parser.getTok().getLoc();
66 Parser.Lex();
67 return Result;
68 }
69
Chad Rosier5362af92013-04-16 18:15:40 +000070 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000071 IC_OR = 0,
72 IC_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +000073 IC_LSHIFT,
74 IC_RSHIFT,
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000075 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000076 IC_MINUS,
77 IC_MULTIPLY,
78 IC_DIVIDE,
79 IC_RPAREN,
80 IC_LPAREN,
81 IC_IMM,
82 IC_REGISTER
83 };
84
85 class InfixCalculator {
86 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
87 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
88 SmallVector<ICToken, 4> PostfixStack;
Michael Liao5bf95782014-12-04 05:20:33 +000089
Chad Rosier5362af92013-04-16 18:15:40 +000090 public:
91 int64_t popOperand() {
92 assert (!PostfixStack.empty() && "Poped an empty stack!");
93 ICToken Op = PostfixStack.pop_back_val();
94 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
95 && "Expected and immediate or register!");
96 return Op.second;
97 }
98 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
99 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
100 "Unexpected operand!");
101 PostfixStack.push_back(std::make_pair(Op, Val));
102 }
Michael Liao5bf95782014-12-04 05:20:33 +0000103
Jakub Staszak9c349222013-08-08 15:48:46 +0000104 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000105 void pushOperator(InfixCalculatorTok Op) {
106 // Push the new operator if the stack is empty.
107 if (InfixOperatorStack.empty()) {
108 InfixOperatorStack.push_back(Op);
109 return;
110 }
Michael Liao5bf95782014-12-04 05:20:33 +0000111
Chad Rosier5362af92013-04-16 18:15:40 +0000112 // Push the new operator if it has a higher precedence than the operator
113 // on the top of the stack or the operator on the top of the stack is a
114 // left parentheses.
115 unsigned Idx = InfixOperatorStack.size() - 1;
116 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
117 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
118 InfixOperatorStack.push_back(Op);
119 return;
120 }
Michael Liao5bf95782014-12-04 05:20:33 +0000121
Chad Rosier5362af92013-04-16 18:15:40 +0000122 // The operator on the top of the stack has higher precedence than the
123 // new operator.
124 unsigned ParenCount = 0;
125 while (1) {
126 // Nothing to process.
127 if (InfixOperatorStack.empty())
128 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000129
Chad Rosier5362af92013-04-16 18:15:40 +0000130 Idx = InfixOperatorStack.size() - 1;
131 StackOp = InfixOperatorStack[Idx];
132 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
133 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000134
Chad Rosier5362af92013-04-16 18:15:40 +0000135 // If we have an even parentheses count and we see a left parentheses,
136 // then stop processing.
137 if (!ParenCount && StackOp == IC_LPAREN)
138 break;
Michael Liao5bf95782014-12-04 05:20:33 +0000139
Chad Rosier5362af92013-04-16 18:15:40 +0000140 if (StackOp == IC_RPAREN) {
141 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000142 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000143 } else if (StackOp == IC_LPAREN) {
144 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000145 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000146 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000147 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000148 PostfixStack.push_back(std::make_pair(StackOp, 0));
149 }
150 }
151 // Push the new operator.
152 InfixOperatorStack.push_back(Op);
153 }
154 int64_t execute() {
155 // Push any remaining operators onto the postfix stack.
156 while (!InfixOperatorStack.empty()) {
157 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
158 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
159 PostfixStack.push_back(std::make_pair(StackOp, 0));
160 }
Michael Liao5bf95782014-12-04 05:20:33 +0000161
Chad Rosier5362af92013-04-16 18:15:40 +0000162 if (PostfixStack.empty())
163 return 0;
Michael Liao5bf95782014-12-04 05:20:33 +0000164
Chad Rosier5362af92013-04-16 18:15:40 +0000165 SmallVector<ICToken, 16> OperandStack;
166 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
167 ICToken Op = PostfixStack[i];
168 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
169 OperandStack.push_back(Op);
170 } else {
171 assert (OperandStack.size() > 1 && "Too few operands.");
172 int64_t Val;
173 ICToken Op2 = OperandStack.pop_back_val();
174 ICToken Op1 = OperandStack.pop_back_val();
175 switch (Op.first) {
176 default:
177 report_fatal_error("Unexpected operator!");
178 break;
179 case IC_PLUS:
180 Val = Op1.second + Op2.second;
181 OperandStack.push_back(std::make_pair(IC_IMM, Val));
182 break;
183 case IC_MINUS:
184 Val = Op1.second - Op2.second;
185 OperandStack.push_back(std::make_pair(IC_IMM, Val));
186 break;
187 case IC_MULTIPLY:
188 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
189 "Multiply operation with an immediate and a register!");
190 Val = Op1.second * Op2.second;
191 OperandStack.push_back(std::make_pair(IC_IMM, Val));
192 break;
193 case IC_DIVIDE:
194 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
195 "Divide operation with an immediate and a register!");
196 assert (Op2.second != 0 && "Division by zero!");
197 Val = Op1.second / Op2.second;
198 OperandStack.push_back(std::make_pair(IC_IMM, Val));
199 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000200 case IC_OR:
201 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
202 "Or operation with an immediate and a register!");
203 Val = Op1.second | Op2.second;
204 OperandStack.push_back(std::make_pair(IC_IMM, Val));
205 break;
206 case IC_AND:
207 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
208 "And operation with an immediate and a register!");
209 Val = Op1.second & Op2.second;
210 OperandStack.push_back(std::make_pair(IC_IMM, Val));
211 break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000212 case IC_LSHIFT:
213 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
214 "Left shift operation with an immediate and a register!");
215 Val = Op1.second << Op2.second;
216 OperandStack.push_back(std::make_pair(IC_IMM, Val));
217 break;
218 case IC_RSHIFT:
219 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
220 "Right shift operation with an immediate and a register!");
221 Val = Op1.second >> Op2.second;
222 OperandStack.push_back(std::make_pair(IC_IMM, Val));
223 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000224 }
225 }
226 }
227 assert (OperandStack.size() == 1 && "Expected a single result.");
228 return OperandStack.pop_back_val().second;
229 }
230 };
231
232 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000233 IES_OR,
234 IES_AND,
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000235 IES_LSHIFT,
236 IES_RSHIFT,
Chad Rosier5362af92013-04-16 18:15:40 +0000237 IES_PLUS,
238 IES_MINUS,
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000239 IES_NOT,
Chad Rosier5362af92013-04-16 18:15:40 +0000240 IES_MULTIPLY,
241 IES_DIVIDE,
242 IES_LBRAC,
243 IES_RBRAC,
244 IES_LPAREN,
245 IES_RPAREN,
246 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000247 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000248 IES_IDENTIFIER,
249 IES_ERROR
250 };
251
252 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000253 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000254 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000255 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000256 const MCExpr *Sym;
257 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000258 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000259 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000260 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000261 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000262 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000263 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
Craig Topper062a2ba2014-04-25 05:30:21 +0000264 Scale(1), Imm(imm), Sym(nullptr), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000265 AddImmPrefix(addimmprefix) { Info.clear(); }
Michael Liao5bf95782014-12-04 05:20:33 +0000266
Chad Rosier5362af92013-04-16 18:15:40 +0000267 unsigned getBaseReg() { return BaseReg; }
268 unsigned getIndexReg() { return IndexReg; }
269 unsigned getScale() { return Scale; }
270 const MCExpr *getSym() { return Sym; }
271 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000272 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000273 bool isValidEndState() {
274 return State == IES_RBRAC || State == IES_INTEGER;
275 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000276 bool getStopOnLBrac() { return StopOnLBrac; }
277 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000278 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000279
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000280 InlineAsmIdentifierInfo &getIdentifierInfo() {
281 return Info;
282 }
283
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000284 void onOr() {
285 IntelExprState CurrState = State;
286 switch (State) {
287 default:
288 State = IES_ERROR;
289 break;
290 case IES_INTEGER:
291 case IES_RPAREN:
292 case IES_REGISTER:
293 State = IES_OR;
294 IC.pushOperator(IC_OR);
295 break;
296 }
297 PrevState = CurrState;
298 }
299 void onAnd() {
300 IntelExprState CurrState = State;
301 switch (State) {
302 default:
303 State = IES_ERROR;
304 break;
305 case IES_INTEGER:
306 case IES_RPAREN:
307 case IES_REGISTER:
308 State = IES_AND;
309 IC.pushOperator(IC_AND);
310 break;
311 }
312 PrevState = CurrState;
313 }
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000314 void onLShift() {
315 IntelExprState CurrState = State;
316 switch (State) {
317 default:
318 State = IES_ERROR;
319 break;
320 case IES_INTEGER:
321 case IES_RPAREN:
322 case IES_REGISTER:
323 State = IES_LSHIFT;
324 IC.pushOperator(IC_LSHIFT);
325 break;
326 }
327 PrevState = CurrState;
328 }
329 void onRShift() {
330 IntelExprState CurrState = State;
331 switch (State) {
332 default:
333 State = IES_ERROR;
334 break;
335 case IES_INTEGER:
336 case IES_RPAREN:
337 case IES_REGISTER:
338 State = IES_RSHIFT;
339 IC.pushOperator(IC_RSHIFT);
340 break;
341 }
342 PrevState = CurrState;
343 }
Chad Rosier5362af92013-04-16 18:15:40 +0000344 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000345 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000346 switch (State) {
347 default:
348 State = IES_ERROR;
349 break;
350 case IES_INTEGER:
351 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000352 case IES_REGISTER:
353 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000354 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000355 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
356 // If we already have a BaseReg, then assume this is the IndexReg with
357 // a scale of 1.
358 if (!BaseReg) {
359 BaseReg = TmpReg;
360 } else {
361 assert (!IndexReg && "BaseReg/IndexReg already set!");
362 IndexReg = TmpReg;
363 Scale = 1;
364 }
365 }
Chad Rosier5362af92013-04-16 18:15:40 +0000366 break;
367 }
Chad Rosier31246272013-04-17 21:01:45 +0000368 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000369 }
370 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000371 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000372 switch (State) {
373 default:
374 State = IES_ERROR;
375 break;
376 case IES_PLUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000377 case IES_NOT:
Chad Rosier31246272013-04-17 21:01:45 +0000378 case IES_MULTIPLY:
379 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000380 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000381 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000382 case IES_LBRAC:
383 case IES_RBRAC:
384 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000385 case IES_REGISTER:
386 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000387 // Only push the minus operator if it is not a unary operator.
388 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
389 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
390 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
391 IC.pushOperator(IC_MINUS);
392 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
393 // If we already have a BaseReg, then assume this is the IndexReg with
394 // a scale of 1.
395 if (!BaseReg) {
396 BaseReg = TmpReg;
397 } else {
398 assert (!IndexReg && "BaseReg/IndexReg already set!");
399 IndexReg = TmpReg;
400 Scale = 1;
401 }
Chad Rosier5362af92013-04-16 18:15:40 +0000402 }
Chad Rosier5362af92013-04-16 18:15:40 +0000403 break;
404 }
Chad Rosier31246272013-04-17 21:01:45 +0000405 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000406 }
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000407 void onNot() {
408 IntelExprState CurrState = State;
409 switch (State) {
410 default:
411 State = IES_ERROR;
412 break;
413 case IES_PLUS:
414 case IES_NOT:
415 State = IES_NOT;
416 break;
417 }
418 PrevState = CurrState;
419 }
Chad Rosier5362af92013-04-16 18:15:40 +0000420 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000421 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000422 switch (State) {
423 default:
424 State = IES_ERROR;
425 break;
426 case IES_PLUS:
427 case IES_LPAREN:
428 State = IES_REGISTER;
429 TmpReg = Reg;
430 IC.pushOperand(IC_REGISTER);
431 break;
Chad Rosier31246272013-04-17 21:01:45 +0000432 case IES_MULTIPLY:
433 // Index Register - Scale * Register
434 if (PrevState == IES_INTEGER) {
435 assert (!IndexReg && "IndexReg already set!");
436 State = IES_REGISTER;
437 IndexReg = Reg;
438 // Get the scale and replace the 'Scale * Register' with '0'.
439 Scale = IC.popOperand();
440 IC.pushOperand(IC_IMM);
441 IC.popOperator();
442 } else {
443 State = IES_ERROR;
444 }
Chad Rosier5362af92013-04-16 18:15:40 +0000445 break;
446 }
Chad Rosier31246272013-04-17 21:01:45 +0000447 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000448 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000449 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000450 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000451 switch (State) {
452 default:
453 State = IES_ERROR;
454 break;
455 case IES_PLUS:
456 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000457 case IES_NOT:
Chad Rosier5362af92013-04-16 18:15:40 +0000458 State = IES_INTEGER;
459 Sym = SymRef;
460 SymName = SymRefName;
461 IC.pushOperand(IC_IMM);
462 break;
463 }
464 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000465 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000466 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000467 switch (State) {
468 default:
469 State = IES_ERROR;
470 break;
471 case IES_PLUS:
472 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000473 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000474 case IES_OR:
475 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000476 case IES_LSHIFT:
477 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000478 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000479 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000480 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000481 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000482 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
483 // Index Register - Register * Scale
484 assert (!IndexReg && "IndexReg already set!");
485 IndexReg = TmpReg;
486 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000487 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
488 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
489 return true;
490 }
Chad Rosier31246272013-04-17 21:01:45 +0000491 // Get the scale and replace the 'Register * Scale' with '0'.
492 IC.popOperator();
493 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000494 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000495 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosier31246272013-04-17 21:01:45 +0000496 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000497 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
498 PrevState == IES_NOT) &&
Chad Rosier31246272013-04-17 21:01:45 +0000499 CurrState == IES_MINUS) {
500 // Unary minus. No need to pop the minus operand because it was never
501 // pushed.
502 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000503 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
504 PrevState == IES_OR || PrevState == IES_AND ||
505 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
506 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
507 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
508 PrevState == IES_NOT) &&
509 CurrState == IES_NOT) {
510 // Unary not. No need to pop the not operand because it was never
511 // pushed.
512 IC.pushOperand(IC_IMM, ~TmpInt); // Push ~Imm.
Chad Rosier31246272013-04-17 21:01:45 +0000513 } else {
514 IC.pushOperand(IC_IMM, TmpInt);
515 }
Chad Rosier5362af92013-04-16 18:15:40 +0000516 break;
517 }
Chad Rosier31246272013-04-17 21:01:45 +0000518 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000519 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000520 }
521 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000522 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000523 switch (State) {
524 default:
525 State = IES_ERROR;
526 break;
527 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000528 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000529 case IES_RPAREN:
530 State = IES_MULTIPLY;
531 IC.pushOperator(IC_MULTIPLY);
532 break;
533 }
534 }
535 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000536 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000537 switch (State) {
538 default:
539 State = IES_ERROR;
540 break;
541 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000542 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000543 State = IES_DIVIDE;
544 IC.pushOperator(IC_DIVIDE);
545 break;
546 }
547 }
548 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000549 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000550 switch (State) {
551 default:
552 State = IES_ERROR;
553 break;
554 case IES_RBRAC:
555 State = IES_PLUS;
556 IC.pushOperator(IC_PLUS);
557 break;
558 }
559 }
560 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000561 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000562 switch (State) {
563 default:
564 State = IES_ERROR;
565 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000566 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000567 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000568 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000569 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000570 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
571 // If we already have a BaseReg, then assume this is the IndexReg with
572 // a scale of 1.
573 if (!BaseReg) {
574 BaseReg = TmpReg;
575 } else {
576 assert (!IndexReg && "BaseReg/IndexReg already set!");
577 IndexReg = TmpReg;
578 Scale = 1;
579 }
Chad Rosier5362af92013-04-16 18:15:40 +0000580 }
581 break;
582 }
Chad Rosier31246272013-04-17 21:01:45 +0000583 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000584 }
585 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000586 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000587 switch (State) {
588 default:
589 State = IES_ERROR;
590 break;
591 case IES_PLUS:
592 case IES_MINUS:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000593 case IES_NOT:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000594 case IES_OR:
595 case IES_AND:
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000596 case IES_LSHIFT:
597 case IES_RSHIFT:
Chad Rosier5362af92013-04-16 18:15:40 +0000598 case IES_MULTIPLY:
599 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000600 case IES_LPAREN:
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000601 // FIXME: We don't handle this type of unary minus or not, yet.
Chad Rosierdb003992013-04-18 16:28:19 +0000602 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000603 PrevState == IES_OR || PrevState == IES_AND ||
Kevin Enderbyd6b10712014-02-06 01:21:15 +0000604 PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
Chad Rosierdb003992013-04-18 16:28:19 +0000605 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +0000606 PrevState == IES_LPAREN || PrevState == IES_LBRAC ||
607 PrevState == IES_NOT) &&
608 (CurrState == IES_MINUS || CurrState == IES_NOT)) {
Chad Rosierdb003992013-04-18 16:28:19 +0000609 State = IES_ERROR;
610 break;
611 }
Chad Rosier5362af92013-04-16 18:15:40 +0000612 State = IES_LPAREN;
613 IC.pushOperator(IC_LPAREN);
614 break;
615 }
Chad Rosier31246272013-04-17 21:01:45 +0000616 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000617 }
618 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000619 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000620 switch (State) {
621 default:
622 State = IES_ERROR;
623 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000624 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000625 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000626 case IES_RPAREN:
627 State = IES_RPAREN;
628 IC.pushOperator(IC_RPAREN);
629 break;
630 }
631 }
632 };
633
Chris Lattnera3a06812011-10-16 04:47:35 +0000634 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000635 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000636 bool MatchingInlineAsm = false) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000637 MCAsmParser &Parser = getParser();
Chad Rosier4453e842012-10-12 23:09:25 +0000638 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000639 return Parser.Error(L, Msg, Ranges);
640 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000641
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000642 bool ErrorAndEatStatement(SMLoc L, const Twine &Msg,
643 ArrayRef<SMRange> Ranges = None,
644 bool MatchingInlineAsm = false) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000645 MCAsmParser &Parser = getParser();
646 Parser.eatToEndOfStatement();
647 return Error(L, Msg, Ranges, MatchingInlineAsm);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000648 }
649
David Blaikie960ea3f2014-06-08 16:18:35 +0000650 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
Devang Patel41b9dde2012-01-17 18:00:18 +0000651 Error(Loc, Msg);
Craig Topper062a2ba2014-04-25 05:30:21 +0000652 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +0000653 }
654
David Blaikie960ea3f2014-06-08 16:18:35 +0000655 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
656 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
657 std::unique_ptr<X86Operand> ParseOperand();
658 std::unique_ptr<X86Operand> ParseATTOperand();
659 std::unique_ptr<X86Operand> ParseIntelOperand();
660 std::unique_ptr<X86Operand> ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000661 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
David Blaikie960ea3f2014-06-08 16:18:35 +0000662 std::unique_ptr<X86Operand> ParseIntelOperator(unsigned OpKind);
663 std::unique_ptr<X86Operand>
664 ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
665 std::unique_ptr<X86Operand>
666 ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000667 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
David Blaikie960ea3f2014-06-08 16:18:35 +0000668 std::unique_ptr<X86Operand> ParseIntelBracExpression(unsigned SegReg,
669 SMLoc Start,
670 int64_t ImmDisp,
671 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000672 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
673 InlineAsmIdentifierInfo &Info,
674 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000675
David Blaikie960ea3f2014-06-08 16:18:35 +0000676 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000677
David Blaikie960ea3f2014-06-08 16:18:35 +0000678 std::unique_ptr<X86Operand>
679 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg,
680 unsigned IndexReg, unsigned Scale, SMLoc Start,
681 SMLoc End, unsigned Size, StringRef Identifier,
682 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000683
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000684 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000685 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000686
Saleem Abdulrasoolca24b1d2015-01-14 05:10:21 +0000687 bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
David Blaikie960ea3f2014-06-08 16:18:35 +0000688 bool processInstruction(MCInst &Inst, const OperandVector &Ops);
Devang Patelde47cce2012-01-18 22:42:29 +0000689
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000690 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
691 /// instrumentation around Inst.
David Blaikie960ea3f2014-06-08 16:18:35 +0000692 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +0000693
Chad Rosier49963552012-10-13 00:26:04 +0000694 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000695 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000696 uint64_t &ErrorInfo,
Craig Topper39012cc2014-03-09 18:03:14 +0000697 bool MatchingInlineAsm) override;
Chad Rosier9cb988f2012-08-09 22:04:55 +0000698
Reid Klecknerf6fb7802014-08-26 20:32:34 +0000699 void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands,
700 MCStreamer &Out, bool MatchingInlineAsm);
701
702 bool ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
703 bool MatchingInlineAsm);
704
705 bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
706 OperandVector &Operands, MCStreamer &Out,
707 uint64_t &ErrorInfo,
708 bool MatchingInlineAsm);
709
710 bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
711 OperandVector &Operands, MCStreamer &Out,
712 uint64_t &ErrorInfo,
713 bool MatchingInlineAsm);
714
Craig Topperfd38cbe2014-08-30 16:48:34 +0000715 bool OmitRegisterFromClobberLists(unsigned RegNo) override;
Nico Weber42f79db2014-07-17 20:24:55 +0000716
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000717 /// doSrcDstMatch - Returns true if operands are matching in their
718 /// word size (%si and %di, %esi and %edi, etc.). Order depends on
719 /// the parsing mode (Intel vs. AT&T).
720 bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
721
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000722 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
723 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
724 /// \return \c true if no parsing errors occurred, \c false otherwise.
David Blaikie960ea3f2014-06-08 16:18:35 +0000725 bool HandleAVX512Operand(OperandVector &Operands,
726 const MCParsedAsmOperand &Op);
Elena Demikhovskyc9657012014-02-20 06:34:39 +0000727
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000728 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000729 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000730 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000731 }
Craig Topper3c80d622014-01-06 04:55:54 +0000732 bool is32BitMode() const {
733 // FIXME: Can tablegen auto-generate this?
734 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
735 }
736 bool is16BitMode() const {
737 // FIXME: Can tablegen auto-generate this?
738 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
739 }
740 void SwitchMode(uint64_t mode) {
741 uint64_t oldMode = STI.getFeatureBits() &
742 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
743 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000744 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000745 assert(mode == (STI.getFeatureBits() &
746 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000747 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000748
Reid Kleckner5b37c182014-08-01 20:21:24 +0000749 unsigned getPointerWidth() {
750 if (is16BitMode()) return 16;
751 if (is32BitMode()) return 32;
752 if (is64BitMode()) return 64;
753 llvm_unreachable("invalid mode");
754 }
755
Chad Rosierc2f055d2013-04-18 16:13:18 +0000756 bool isParsingIntelSyntax() {
757 return getParser().getAssemblerDialect();
758 }
759
Daniel Dunbareefe8612010-07-19 05:44:09 +0000760 /// @name Auto-generated Matcher Functions
761 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000762
Chris Lattner3e4582a2010-09-06 19:11:01 +0000763#define GET_ASSEMBLER_HEADER
764#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000765
Daniel Dunbar00331992009-07-29 00:02:19 +0000766 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000767
768public:
Rafael Espindola961d4692014-11-11 05:18:41 +0000769 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &Parser,
770 const MCInstrInfo &mii, const MCTargetOptions &Options)
771 : MCTargetAsmParser(), STI(sti), MII(mii), InstInfo(nullptr) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000772
Daniel Dunbareefe8612010-07-19 05:44:09 +0000773 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000774 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000775 Instrumentation.reset(
776 CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000777 }
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000778
Craig Topper39012cc2014-03-09 18:03:14 +0000779 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000780
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000781 void SetFrameRegister(unsigned RegNo) override;
782
David Blaikie960ea3f2014-06-08 16:18:35 +0000783 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
784 SMLoc NameLoc, OperandVector &Operands) override;
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000785
Craig Topper39012cc2014-03-09 18:03:14 +0000786 bool ParseDirective(AsmToken DirectiveID) override;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000787};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000788} // end anonymous namespace
789
Sean Callanan86c11812010-01-23 00:40:33 +0000790/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000791/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000792
Chris Lattner60db0a62010-02-09 00:34:28 +0000793static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000794
795/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000796
Kevin Enderbybc570f22014-01-23 22:34:42 +0000797static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
798 StringRef &ErrMsg) {
799 // If we have both a base register and an index register make sure they are
800 // both 64-bit or 32-bit registers.
801 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
802 if (BaseReg != 0 && IndexReg != 0) {
803 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
804 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
805 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
806 IndexReg != X86::RIZ) {
807 ErrMsg = "base register is 64-bit, but index register is not";
808 return true;
809 }
810 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
811 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
812 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
813 IndexReg != X86::EIZ){
814 ErrMsg = "base register is 32-bit, but index register is not";
815 return true;
816 }
817 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
818 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
819 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
820 ErrMsg = "base register is 16-bit, but index register is not";
821 return true;
822 }
823 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
824 IndexReg != X86::SI && IndexReg != X86::DI) ||
825 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
826 IndexReg != X86::BX && IndexReg != X86::BP)) {
827 ErrMsg = "invalid 16-bit base/index register combination";
828 return true;
829 }
830 }
831 }
832 return false;
833}
834
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000835bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
836{
837 // Return true and let a normal complaint about bogus operands happen.
838 if (!Op1.isMem() || !Op2.isMem())
839 return true;
840
841 // Actually these might be the other way round if Intel syntax is
842 // being used. It doesn't matter.
843 unsigned diReg = Op1.Mem.BaseReg;
844 unsigned siReg = Op2.Mem.BaseReg;
845
846 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
847 return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
848 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
849 return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
850 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
851 return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
852 // Again, return true and let another error happen.
853 return true;
854}
855
Devang Patel4a6e7782012-01-12 18:03:40 +0000856bool X86AsmParser::ParseRegister(unsigned &RegNo,
857 SMLoc &StartLoc, SMLoc &EndLoc) {
Rafael Espindola961d4692014-11-11 05:18:41 +0000858 MCAsmParser &Parser = getParser();
Chris Lattnercc2ad082010-01-15 18:27:19 +0000859 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000860 const AsmToken &PercentTok = Parser.getTok();
861 StartLoc = PercentTok.getLoc();
862
863 // If we encounter a %, ignore it. This code handles registers with and
864 // without the prefix, unprefixed registers can occur in cfi directives.
865 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000866 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000867
Sean Callanan936b0d32010-01-19 21:44:56 +0000868 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000869 EndLoc = Tok.getEndLoc();
870
Devang Patelce6a2ca2012-01-20 22:32:05 +0000871 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000872 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000873 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000874 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000875 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000876
Kevin Enderby7d912182009-09-03 17:15:07 +0000877 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000878
Chris Lattner1261b812010-09-22 04:11:10 +0000879 // If the match failed, try the register name as lowercase.
880 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000881 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000882
Evan Chengeda1d4f2011-07-27 23:22:03 +0000883 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +0000884 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +0000885 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
886 // checked.
887 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
888 // REX prefix.
889 if (RegNo == X86::RIZ ||
890 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
891 X86II::isX86_64NonExtLowByteReg(RegNo) ||
892 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000893 return Error(StartLoc, "register %"
894 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000895 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000896 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000897
Chris Lattner1261b812010-09-22 04:11:10 +0000898 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
899 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000900 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000901 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000902
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000903 // Check to see if we have '(4)' after %st.
904 if (getLexer().isNot(AsmToken::LParen))
905 return false;
906 // Lex the paren.
907 getParser().Lex();
908
909 const AsmToken &IntTok = Parser.getTok();
910 if (IntTok.isNot(AsmToken::Integer))
911 return Error(IntTok.getLoc(), "expected stack index");
912 switch (IntTok.getIntVal()) {
913 case 0: RegNo = X86::ST0; break;
914 case 1: RegNo = X86::ST1; break;
915 case 2: RegNo = X86::ST2; break;
916 case 3: RegNo = X86::ST3; break;
917 case 4: RegNo = X86::ST4; break;
918 case 5: RegNo = X86::ST5; break;
919 case 6: RegNo = X86::ST6; break;
920 case 7: RegNo = X86::ST7; break;
921 default: return Error(IntTok.getLoc(), "invalid stack index");
922 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000923
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000924 if (getParser().Lex().isNot(AsmToken::RParen))
925 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000926
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000927 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000928 Parser.Lex(); // Eat ')'
929 return false;
930 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000931
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000932 EndLoc = Parser.getTok().getEndLoc();
933
Chris Lattner80486622010-06-24 07:29:18 +0000934 // If this is "db[0-7]", match it as an alias
935 // for dr[0-7].
936 if (RegNo == 0 && Tok.getString().size() == 3 &&
937 Tok.getString().startswith("db")) {
938 switch (Tok.getString()[2]) {
939 case '0': RegNo = X86::DR0; break;
940 case '1': RegNo = X86::DR1; break;
941 case '2': RegNo = X86::DR2; break;
942 case '3': RegNo = X86::DR3; break;
943 case '4': RegNo = X86::DR4; break;
944 case '5': RegNo = X86::DR5; break;
945 case '6': RegNo = X86::DR6; break;
946 case '7': RegNo = X86::DR7; break;
947 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000948
Chris Lattner80486622010-06-24 07:29:18 +0000949 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000950 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000951 Parser.Lex(); // Eat it.
952 return false;
953 }
954 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000955
Devang Patelce6a2ca2012-01-20 22:32:05 +0000956 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000957 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000958 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000959 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000960 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000961
Sean Callanana83fd7d2010-01-19 20:27:46 +0000962 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000963 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000964}
965
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000966void X86AsmParser::SetFrameRegister(unsigned RegNo) {
Yuri Gorshenine8c81fd2014-10-07 11:03:09 +0000967 Instrumentation->SetInitialFrameRegister(RegNo);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000968}
969
David Blaikie960ea3f2014-06-08 16:18:35 +0000970std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000971 unsigned basereg =
972 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
973 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +0000974 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
975 /*BaseReg=*/basereg, /*IndexReg=*/0, /*Scale=*/1,
976 Loc, Loc, 0);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000977}
978
David Blaikie960ea3f2014-06-08 16:18:35 +0000979std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000980 unsigned basereg =
981 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
982 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Craig Topper055845f2015-01-02 07:02:25 +0000983 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
984 /*BaseReg=*/basereg, /*IndexReg=*/0, /*Scale=*/1,
985 Loc, Loc, 0);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000986}
987
David Blaikie960ea3f2014-06-08 16:18:35 +0000988std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000989 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000990 return ParseIntelOperand();
991 return ParseATTOperand();
992}
993
Devang Patel41b9dde2012-01-17 18:00:18 +0000994/// getIntelMemOperandSize - Return intel memory operand size.
995static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000996 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000997 .Cases("BYTE", "byte", 8)
998 .Cases("WORD", "word", 16)
999 .Cases("DWORD", "dword", 32)
1000 .Cases("QWORD", "qword", 64)
1001 .Cases("XWORD", "xword", 80)
1002 .Cases("XMMWORD", "xmmword", 128)
1003 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +00001004 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +00001005 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +00001006 .Default(0);
1007 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001008}
1009
David Blaikie960ea3f2014-06-08 16:18:35 +00001010std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm(
1011 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
1012 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
1013 InlineAsmIdentifierInfo &Info) {
Reid Kleckner5b37c182014-08-01 20:21:24 +00001014 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
1015 // some other label reference.
1016 if (isa<MCSymbolRefExpr>(Disp) && Info.OpDecl && !Info.IsVarDecl) {
1017 // Insert an explicit size if the user didn't have one.
1018 if (!Size) {
1019 Size = getPointerWidth();
1020 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1021 /*Len=*/0, Size));
1022 }
1023
1024 // Create an absolute memory reference in order to match against
1025 // instructions taking a PC relative operand.
Craig Topper055845f2015-01-02 07:02:25 +00001026 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size,
1027 Identifier, Info.OpDecl);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001028 }
1029
1030 // We either have a direct symbol reference, or an offset from a symbol. The
1031 // parser always puts the symbol on the LHS, so look there for size
1032 // calculation purposes.
1033 const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
1034 bool IsSymRef =
1035 isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
1036 if (IsSymRef) {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001037 if (!Size) {
1038 Size = Info.Type * 8; // Size is in terms of bits in this context.
1039 if (Size)
1040 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1041 /*Len=*/0, Size));
1042 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001043 }
1044
Chad Rosier7ca135b2013-03-19 21:11:56 +00001045 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001046 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001047 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001048 BaseReg = BaseReg ? BaseReg : 1;
Craig Topper055845f2015-01-02 07:02:25 +00001049 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1050 IndexReg, Scale, Start, End, Size, Identifier,
1051 Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001052}
1053
Chad Rosierd383db52013-04-12 20:20:54 +00001054static void
1055RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1056 StringRef SymName, int64_t ImmDisp,
1057 int64_t FinalImmDisp, SMLoc &BracLoc,
1058 SMLoc &StartInBrac, SMLoc &End) {
1059 // Remove the '[' and ']' from the IR string.
1060 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1061 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1062
1063 // If ImmDisp is non-zero, then we parsed a displacement before the
1064 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1065 // If ImmDisp doesn't match the displacement computed by the state machine
1066 // then we have an additional displacement in the bracketed expression.
1067 if (ImmDisp != FinalImmDisp) {
1068 if (ImmDisp) {
1069 // We have an immediate displacement before the bracketed expression.
1070 // Adjust this to match the final immediate displacement.
1071 bool Found = false;
1072 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1073 E = AsmRewrites->end(); I != E; ++I) {
1074 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1075 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001076 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1077 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001078 (*I).Kind = AOK_Imm;
1079 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1080 (*I).Val = FinalImmDisp;
1081 Found = true;
1082 break;
1083 }
1084 }
1085 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001086 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001087 } else {
1088 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001089 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001090 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001091 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001092 }
1093 }
1094 // Remove all the ImmPrefix rewrites within the brackets.
1095 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1096 E = AsmRewrites->end(); I != E; ++I) {
1097 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1098 continue;
1099 if ((*I).Kind == AOK_ImmPrefix)
1100 (*I).Kind = AOK_Delete;
1101 }
1102 const char *SymLocPtr = SymName.data();
Michael Liao5bf95782014-12-04 05:20:33 +00001103 // Skip everything before the symbol.
Chad Rosierd383db52013-04-12 20:20:54 +00001104 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1105 assert(Len > 0 && "Expected a non-negative length.");
1106 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1107 }
1108 // Skip everything after the symbol.
1109 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1110 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1111 assert(Len > 0 && "Expected a non-negative length.");
1112 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1113 }
1114}
1115
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001116bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001117 MCAsmParser &Parser = getParser();
Chad Rosier6844ea02012-10-24 22:13:37 +00001118 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001119
Chad Rosier5c118fd2013-01-14 22:31:35 +00001120 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001121 while (!Done) {
1122 bool UpdateLocLex = true;
1123
1124 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1125 // identifier. Don't try an parse it as a register.
1126 if (Tok.getString().startswith("."))
1127 break;
Michael Liao5bf95782014-12-04 05:20:33 +00001128
Chad Rosierbfb70992013-04-17 00:11:46 +00001129 // If we're parsing an immediate expression, we don't expect a '['.
1130 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1131 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001132
David Majnemer6a5b8122014-06-19 01:25:43 +00001133 AsmToken::TokenKind TK = getLexer().getKind();
1134 switch (TK) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001135 default: {
1136 if (SM.isValidEndState()) {
1137 Done = true;
1138 break;
1139 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001140 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001141 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001142 case AsmToken::EndOfStatement: {
1143 Done = true;
1144 break;
1145 }
David Majnemer6a5b8122014-06-19 01:25:43 +00001146 case AsmToken::String:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001147 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001148 // This could be a register or a symbolic displacement.
1149 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001150 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001151 SMLoc IdentLoc = Tok.getLoc();
1152 StringRef Identifier = Tok.getString();
David Majnemer6a5b8122014-06-19 01:25:43 +00001153 if (TK != AsmToken::String && !ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001154 SM.onRegister(TmpReg);
1155 UpdateLocLex = false;
1156 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001157 } else {
1158 if (!isParsingInlineAsm()) {
1159 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001160 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001161 } else {
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001162 // This is a dot operator, not an adjacent identifier.
1163 if (Identifier.find('.') != StringRef::npos) {
1164 return false;
1165 } else {
1166 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1167 if (ParseIntelIdentifier(Val, Identifier, Info,
1168 /*Unevaluated=*/false, End))
1169 return true;
1170 }
Chad Rosier95ce8892013-04-19 18:39:50 +00001171 }
1172 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001173 UpdateLocLex = false;
1174 break;
1175 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001176 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001177 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001178 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001179 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001180 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001181 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1182 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001183 // Look for 'b' or 'f' following an Integer as a directional label
1184 SMLoc Loc = getTok().getLoc();
1185 int64_t IntVal = getTok().getIntVal();
1186 End = consumeToken();
1187 UpdateLocLex = false;
1188 if (getLexer().getKind() == AsmToken::Identifier) {
1189 StringRef IDVal = getTok().getString();
1190 if (IDVal == "f" || IDVal == "b") {
1191 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +00001192 getContext().GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Kevin Enderby36eba252013-12-19 23:16:14 +00001193 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Michael Liao5bf95782014-12-04 05:20:33 +00001194 const MCExpr *Val =
Kevin Enderby36eba252013-12-19 23:16:14 +00001195 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1196 if (IDVal == "b" && Sym->isUndefined())
1197 return Error(Loc, "invalid reference to undefined symbol");
1198 StringRef Identifier = Sym->getName();
1199 SM.onIdentifierExpr(Val, Identifier);
1200 End = consumeToken();
1201 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001202 if (SM.onInteger(IntVal, ErrMsg))
1203 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001204 }
1205 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001206 if (SM.onInteger(IntVal, ErrMsg))
1207 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001208 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001209 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001210 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001211 case AsmToken::Plus: SM.onPlus(); break;
1212 case AsmToken::Minus: SM.onMinus(); break;
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001213 case AsmToken::Tilde: SM.onNot(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001214 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001215 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001216 case AsmToken::Pipe: SM.onOr(); break;
1217 case AsmToken::Amp: SM.onAnd(); break;
Kevin Enderbyd6b10712014-02-06 01:21:15 +00001218 case AsmToken::LessLess:
1219 SM.onLShift(); break;
1220 case AsmToken::GreaterGreater:
1221 SM.onRShift(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001222 case AsmToken::LBrac: SM.onLBrac(); break;
1223 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001224 case AsmToken::LParen: SM.onLParen(); break;
1225 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001226 }
Chad Rosier31246272013-04-17 21:01:45 +00001227 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001228 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001229
Alp Tokera5b88a52013-12-02 16:06:06 +00001230 if (!Done && UpdateLocLex)
1231 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001232 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001233 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001234}
1235
David Blaikie960ea3f2014-06-08 16:18:35 +00001236std::unique_ptr<X86Operand>
1237X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
1238 int64_t ImmDisp, unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001239 MCAsmParser &Parser = getParser();
Chad Rosier5362af92013-04-16 18:15:40 +00001240 const AsmToken &Tok = Parser.getTok();
1241 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1242 if (getLexer().isNot(AsmToken::LBrac))
1243 return ErrorOperand(BracLoc, "Expected '[' token!");
1244 Parser.Lex(); // Eat '['
1245
1246 SMLoc StartInBrac = Tok.getLoc();
1247 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1248 // may have already parsed an immediate displacement before the bracketed
1249 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001250 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001251 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001252 return nullptr;
Devang Patel41b9dde2012-01-17 18:00:18 +00001253
Craig Topper062a2ba2014-04-25 05:30:21 +00001254 const MCExpr *Disp = nullptr;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001255 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001256 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001257 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001258 if (isParsingInlineAsm())
1259 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001260 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001261 End);
Reid Klecknerd84e70e2014-03-04 00:33:17 +00001262 }
1263
1264 if (SM.getImm() || !Disp) {
1265 const MCExpr *Imm = MCConstantExpr::Create(SM.getImm(), getContext());
1266 if (Disp)
1267 Disp = MCBinaryExpr::CreateAdd(Disp, Imm, getContext());
1268 else
1269 Disp = Imm; // An immediate displacement only.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001270 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001271
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001272 // Parse struct field access. Intel requires a dot, but MSVC doesn't. MSVC
1273 // will in fact do global lookup the field name inside all global typedefs,
1274 // but we don't emulate that.
1275 if (Tok.getString().find('.') != StringRef::npos) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001276 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001277 if (ParseIntelDotOperator(Disp, NewDisp))
Craig Topper062a2ba2014-04-25 05:30:21 +00001278 return nullptr;
Michael Liao5bf95782014-12-04 05:20:33 +00001279
Chad Rosier70f47592013-04-10 20:07:47 +00001280 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001281 Parser.Lex(); // Eat the field.
1282 Disp = NewDisp;
1283 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001284
Chad Rosier5c118fd2013-01-14 22:31:35 +00001285 int BaseReg = SM.getBaseReg();
1286 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001287 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001288 if (!isParsingInlineAsm()) {
1289 // handle [-42]
1290 if (!BaseReg && !IndexReg) {
1291 if (!SegReg)
Craig Topper055845f2015-01-02 07:02:25 +00001292 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size);
1293 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
1294 Start, End, Size);
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001295 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001296 StringRef ErrMsg;
1297 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1298 Error(StartInBrac, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001299 return nullptr;
Kevin Enderbybc570f22014-01-23 22:34:42 +00001300 }
Craig Topper055845f2015-01-02 07:02:25 +00001301 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1302 IndexReg, Scale, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001303 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001304
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001305 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001306 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001307 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001308}
1309
Chad Rosier8a244662013-04-02 20:02:33 +00001310// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001311bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1312 StringRef &Identifier,
1313 InlineAsmIdentifierInfo &Info,
1314 bool IsUnevaluatedOperand, SMLoc &End) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001315 MCAsmParser &Parser = getParser();
Chad Rosier95ce8892013-04-19 18:39:50 +00001316 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
Craig Topper062a2ba2014-04-25 05:30:21 +00001317 Val = nullptr;
Chad Rosier8a244662013-04-02 20:02:33 +00001318
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001319 StringRef LineBuf(Identifier.data());
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001320 void *Result =
1321 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001322
Chad Rosier8a244662013-04-02 20:02:33 +00001323 const AsmToken &Tok = Parser.getTok();
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001324 SMLoc Loc = Tok.getLoc();
John McCallf73981b2013-05-03 00:15:41 +00001325
1326 // Advance the token stream until the end of the current token is
1327 // after the end of what the frontend claimed.
1328 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1329 while (true) {
1330 End = Tok.getEndLoc();
1331 getLexer().Lex();
1332
1333 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1334 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001335 }
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001336 Identifier = LineBuf;
1337
1338 // If the identifier lookup was unsuccessful, assume that we are dealing with
1339 // a label.
1340 if (!Result) {
Ehsan Akhgaribb6bb072014-09-22 20:40:36 +00001341 StringRef InternalName =
1342 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(),
1343 Loc, false);
1344 assert(InternalName.size() && "We should have an internal name here.");
1345 // Push a rewrite for replacing the identifier name with the internal name.
1346 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Label, Loc,
1347 Identifier.size(),
1348 InternalName));
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001349 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001350
1351 // Create the symbol reference.
Chad Rosier8a244662013-04-02 20:02:33 +00001352 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1353 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001354 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001355 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001356}
1357
David Majnemeraa34d792013-08-27 21:56:17 +00001358/// \brief Parse intel style segment override.
David Blaikie960ea3f2014-06-08 16:18:35 +00001359std::unique_ptr<X86Operand>
1360X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start,
1361 unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001362 MCAsmParser &Parser = getParser();
David Majnemeraa34d792013-08-27 21:56:17 +00001363 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1364 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1365 if (Tok.isNot(AsmToken::Colon))
1366 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1367 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001368
David Majnemeraa34d792013-08-27 21:56:17 +00001369 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001370 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001371 ImmDisp = Tok.getIntVal();
1372 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1373
Chad Rosier1530ba52013-03-27 21:49:56 +00001374 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001375 InstInfo->AsmRewrites->push_back(
1376 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1377
1378 if (getLexer().isNot(AsmToken::LBrac)) {
1379 // An immediate following a 'segment register', 'colon' token sequence can
1380 // be followed by a bracketed expression. If it isn't we know we have our
1381 // final segment override.
1382 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
Craig Topper055845f2015-01-02 07:02:25 +00001383 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp,
1384 /*BaseReg=*/0, /*IndexReg=*/0, /*Scale=*/1,
1385 Start, ImmDispToken.getEndLoc(), Size);
David Majnemeraa34d792013-08-27 21:56:17 +00001386 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001387 }
1388
Chad Rosier91c82662012-10-24 17:22:29 +00001389 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001390 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001391
David Majnemeraa34d792013-08-27 21:56:17 +00001392 const MCExpr *Val;
1393 SMLoc End;
1394 if (!isParsingInlineAsm()) {
1395 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001396 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001397
Craig Topper055845f2015-01-02 07:02:25 +00001398 return X86Operand::CreateMem(getPointerWidth(), Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001399 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001400
David Majnemeraa34d792013-08-27 21:56:17 +00001401 InlineAsmIdentifierInfo Info;
1402 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001403 if (ParseIntelIdentifier(Val, Identifier, Info,
1404 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001405 return nullptr;
David Majnemeraa34d792013-08-27 21:56:17 +00001406 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1407 /*Scale=*/1, Start, End, Size, Identifier, Info);
1408}
1409
1410/// ParseIntelMemOperand - Parse intel style memory operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00001411std::unique_ptr<X86Operand> X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp,
1412 SMLoc Start,
1413 unsigned Size) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001414 MCAsmParser &Parser = getParser();
David Majnemeraa34d792013-08-27 21:56:17 +00001415 const AsmToken &Tok = Parser.getTok();
1416 SMLoc End;
1417
1418 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1419 if (getLexer().is(AsmToken::LBrac))
1420 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001421 assert(ImmDisp == 0);
David Majnemeraa34d792013-08-27 21:56:17 +00001422
Chad Rosier95ce8892013-04-19 18:39:50 +00001423 const MCExpr *Val;
1424 if (!isParsingInlineAsm()) {
1425 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001426 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001427
Craig Topper055845f2015-01-02 07:02:25 +00001428 return X86Operand::CreateMem(getPointerWidth(), Val, Start, End, Size);
Chad Rosier95ce8892013-04-19 18:39:50 +00001429 }
1430
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001431 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001432 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001433 if (ParseIntelIdentifier(Val, Identifier, Info,
1434 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001435 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001436
1437 if (!getLexer().is(AsmToken::LBrac))
1438 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
1439 /*Scale=*/1, Start, End, Size, Identifier, Info);
1440
1441 Parser.Lex(); // Eat '['
1442
1443 // Parse Identifier [ ImmDisp ]
1444 IntelExprStateMachine SM(/*ImmDisp=*/0, /*StopOnLBrac=*/true,
1445 /*AddImmPrefix=*/false);
1446 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001447 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001448
1449 if (SM.getSym()) {
1450 Error(Start, "cannot use more than one symbol in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001451 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001452 }
1453 if (SM.getBaseReg()) {
1454 Error(Start, "cannot use base register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001455 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001456 }
1457 if (SM.getIndexReg()) {
1458 Error(Start, "cannot use index register with variable reference");
Craig Topper062a2ba2014-04-25 05:30:21 +00001459 return nullptr;
Reid Kleckner4e3bd512014-03-04 17:57:01 +00001460 }
1461
1462 const MCExpr *Disp = MCConstantExpr::Create(SM.getImm(), getContext());
1463 // BaseReg is non-zero to avoid assertions. In the context of inline asm,
1464 // we're pointing to a local variable in memory, so the base register is
1465 // really the frame or stack pointer.
Craig Topper055845f2015-01-02 07:02:25 +00001466 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
1467 /*BaseReg=*/1, /*IndexReg=*/0, /*Scale=*/1,
1468 Start, End, Size, Identifier, Info.OpDecl);
Chad Rosier91c82662012-10-24 17:22:29 +00001469}
1470
Chad Rosier5dcb4662012-10-24 22:21:50 +00001471/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001472bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001473 const MCExpr *&NewDisp) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001474 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001475 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001476 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001477
1478 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001479 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001480 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001481 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001482 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001483
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00001484 // Drop the optional '.'.
1485 StringRef DotDispStr = Tok.getString();
1486 if (DotDispStr.startswith("."))
1487 DotDispStr = DotDispStr.drop_front(1);
Chad Rosier5dcb4662012-10-24 22:21:50 +00001488
Chad Rosier5dcb4662012-10-24 22:21:50 +00001489 // .Imm gets lexed as a real.
1490 if (Tok.is(AsmToken::Real)) {
1491 APInt DotDisp;
1492 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001493 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001494 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001495 unsigned DotDisp;
1496 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1497 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001498 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001499 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001500 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001501 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001502 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001503
Chad Rosier240b7b92012-10-25 21:51:10 +00001504 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1505 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1506 unsigned Len = DotDispStr.size();
1507 unsigned Val = OrigDispVal + DotDispVal;
1508 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1509 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001510 }
1511
Chad Rosiercc541e82013-04-19 15:57:00 +00001512 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001513 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001514}
1515
Chad Rosier91c82662012-10-24 17:22:29 +00001516/// Parse the 'offset' operator. This operator is used to specify the
1517/// location rather then the content of a variable.
David Blaikie960ea3f2014-06-08 16:18:35 +00001518std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOffsetOfOperator() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001519 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001520 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001521 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001522 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001523
Chad Rosier91c82662012-10-24 17:22:29 +00001524 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001525 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001526 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001527 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001528 if (ParseIntelIdentifier(Val, Identifier, Info,
1529 /*Unevaluated=*/false, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001530 return nullptr;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001531
Chad Rosiere2f03772012-10-26 16:09:20 +00001532 // Don't emit the offset operator.
1533 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1534
Chad Rosier91c82662012-10-24 17:22:29 +00001535 // The offset operator will have an 'r' constraint, thus we need to create
1536 // register operand to ensure proper matching. Just pick a GPR based on
1537 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001538 unsigned RegNo =
1539 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001540 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001541 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001542}
1543
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001544enum IntelOperatorKind {
1545 IOK_LENGTH,
1546 IOK_SIZE,
1547 IOK_TYPE
1548};
1549
1550/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1551/// returns the number of elements in an array. It returns the value 1 for
1552/// non-array variables. The SIZE operator returns the size of a C or C++
1553/// variable. A variable's size is the product of its LENGTH and TYPE. The
1554/// TYPE operator returns the size of a C or C++ type or variable. If the
1555/// variable is an array, TYPE returns the size of a single element.
David Blaikie960ea3f2014-06-08 16:18:35 +00001556std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001557 MCAsmParser &Parser = getParser();
Chad Rosier18785852013-04-09 20:58:48 +00001558 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001559 SMLoc TypeLoc = Tok.getLoc();
1560 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001561
Craig Topper062a2ba2014-04-25 05:30:21 +00001562 const MCExpr *Val = nullptr;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001563 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001564 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001565 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001566 if (ParseIntelIdentifier(Val, Identifier, Info,
1567 /*Unevaluated=*/true, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001568 return nullptr;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001569
1570 if (!Info.OpDecl)
1571 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001572
Chad Rosierf6675c32013-04-22 17:01:46 +00001573 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001574 switch(OpKind) {
1575 default: llvm_unreachable("Unexpected operand kind!");
1576 case IOK_LENGTH: CVal = Info.Length; break;
1577 case IOK_SIZE: CVal = Info.Size; break;
1578 case IOK_TYPE: CVal = Info.Type; break;
1579 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001580
1581 // Rewrite the type operator and the C or C++ type or variable in terms of an
1582 // immediate. E.g. TYPE foo -> $$4
1583 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001584 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001585
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001586 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001587 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001588}
1589
David Blaikie960ea3f2014-06-08 16:18:35 +00001590std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001591 MCAsmParser &Parser = getParser();
Chad Rosier70f47592013-04-10 20:07:47 +00001592 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001593 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001594
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001595 // Offset, length, type and size operators.
1596 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001597 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001598 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001599 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001600 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001601 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001602 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001603 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001604 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001605 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001606 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001607
David Majnemeraa34d792013-08-27 21:56:17 +00001608 unsigned Size = getIntelMemOperandSize(Tok.getString());
1609 if (Size) {
1610 Parser.Lex(); // Eat operand size (e.g., byte, word).
1611 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
Reid Kleckner71ff3f22014-08-01 00:59:22 +00001612 return ErrorOperand(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
David Majnemeraa34d792013-08-27 21:56:17 +00001613 Parser.Lex(); // Eat ptr.
1614 }
1615 Start = Tok.getLoc();
1616
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001617 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001618 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
Ehsan Akhgari4103da62014-07-04 19:13:05 +00001619 getLexer().is(AsmToken::Tilde) || getLexer().is(AsmToken::LParen)) {
Chad Rosierbfb70992013-04-17 00:11:46 +00001620 AsmToken StartTok = Tok;
1621 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1622 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001623 if (ParseIntelExpression(SM, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001624 return nullptr;
Chad Rosierbfb70992013-04-17 00:11:46 +00001625
1626 int64_t Imm = SM.getImm();
1627 if (isParsingInlineAsm()) {
1628 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1629 if (StartTok.getString().size() == Len)
1630 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001631 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001632 else
1633 // Otherwise, rewrite the complex expression as a single immediate.
1634 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001635 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001636
1637 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001638 // If a directional label (ie. 1f or 2b) was parsed above from
1639 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1640 // to the MCExpr with the directional local symbol and this is a
1641 // memory operand not an immediate operand.
1642 if (SM.getSym())
Craig Topper055845f2015-01-02 07:02:25 +00001643 return X86Operand::CreateMem(getPointerWidth(), SM.getSym(), Start, End,
1644 Size);
Kevin Enderby36eba252013-12-19 23:16:14 +00001645
Chad Rosierbfb70992013-04-17 00:11:46 +00001646 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1647 return X86Operand::CreateImm(ImmExpr, Start, End);
1648 }
1649
1650 // Only positive immediates are valid.
1651 if (Imm < 0)
1652 return ErrorOperand(Start, "expected a positive immediate displacement "
1653 "before bracketed expr.");
1654
1655 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001656 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001657 }
1658
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001659 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001660 unsigned RegNo = 0;
1661 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001662 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001663 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001664 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001665 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001666
David Majnemeraa34d792013-08-27 21:56:17 +00001667 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001668 }
1669
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001670 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001671 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001672}
1673
David Blaikie960ea3f2014-06-08 16:18:35 +00001674std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() {
Rafael Espindola961d4692014-11-11 05:18:41 +00001675 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001676 switch (getLexer().getKind()) {
1677 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001678 // Parse a memory operand with no segment register.
1679 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001680 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001681 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001682 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001683 SMLoc Start, End;
Craig Topper062a2ba2014-04-25 05:30:21 +00001684 if (ParseRegister(RegNo, Start, End)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001685 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001686 Error(Start, "%eiz and %riz can only be used as index registers",
1687 SMRange(Start, End));
Craig Topper062a2ba2014-04-25 05:30:21 +00001688 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001689 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001690
Chris Lattnerb9270732010-04-17 18:56:34 +00001691 // If this is a segment register followed by a ':', then this is the start
1692 // of a memory reference, otherwise this is a normal register reference.
1693 if (getLexer().isNot(AsmToken::Colon))
1694 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001695
Reid Kleckner0c5da972014-07-31 23:03:22 +00001696 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
1697 return ErrorOperand(Start, "invalid segment register");
1698
Chris Lattnerb9270732010-04-17 18:56:34 +00001699 getParser().Lex(); // Eat the colon.
1700 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001701 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001702 case AsmToken::Dollar: {
1703 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001704 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001705 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001706 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001707 if (getParser().parseExpression(Val, End))
Craig Topper062a2ba2014-04-25 05:30:21 +00001708 return nullptr;
Chris Lattner528d00b2010-01-15 19:28:38 +00001709 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001710 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001711 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001712}
1713
David Blaikie960ea3f2014-06-08 16:18:35 +00001714bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
1715 const MCParsedAsmOperand &Op) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001716 MCAsmParser &Parser = getParser();
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001717 if(STI.getFeatureBits() & X86::FeatureAVX512) {
1718 if (getLexer().is(AsmToken::LCurly)) {
1719 // Eat "{" and mark the current place.
1720 const SMLoc consumedToken = consumeToken();
1721 // Distinguish {1to<NUM>} from {%k<NUM>}.
1722 if(getLexer().is(AsmToken::Integer)) {
1723 // Parse memory broadcasting ({1to<NUM>}).
1724 if (getLexer().getTok().getIntVal() != 1)
1725 return !ErrorAndEatStatement(getLexer().getLoc(),
1726 "Expected 1to<NUM> at this point");
1727 Parser.Lex(); // Eat "1" of 1to8
1728 if (!getLexer().is(AsmToken::Identifier) ||
1729 !getLexer().getTok().getIdentifier().startswith("to"))
1730 return !ErrorAndEatStatement(getLexer().getLoc(),
1731 "Expected 1to<NUM> at this point");
1732 // Recognize only reasonable suffixes.
1733 const char *BroadcastPrimitive =
1734 StringSwitch<const char*>(getLexer().getTok().getIdentifier())
Robert Khasanovbfa01312014-07-21 14:54:21 +00001735 .Case("to2", "{1to2}")
1736 .Case("to4", "{1to4}")
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001737 .Case("to8", "{1to8}")
1738 .Case("to16", "{1to16}")
Craig Topper062a2ba2014-04-25 05:30:21 +00001739 .Default(nullptr);
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001740 if (!BroadcastPrimitive)
1741 return !ErrorAndEatStatement(getLexer().getLoc(),
1742 "Invalid memory broadcast primitive.");
1743 Parser.Lex(); // Eat "toN" of 1toN
1744 if (!getLexer().is(AsmToken::RCurly))
1745 return !ErrorAndEatStatement(getLexer().getLoc(),
1746 "Expected } at this point");
1747 Parser.Lex(); // Eat "}"
1748 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1749 consumedToken));
1750 // No AVX512 specific primitives can pass
1751 // after memory broadcasting, so return.
1752 return true;
1753 } else {
1754 // Parse mask register {%k1}
1755 Operands.push_back(X86Operand::CreateToken("{", consumedToken));
David Blaikie960ea3f2014-06-08 16:18:35 +00001756 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
1757 Operands.push_back(std::move(Op));
Elena Demikhovskyc9657012014-02-20 06:34:39 +00001758 if (!getLexer().is(AsmToken::RCurly))
1759 return !ErrorAndEatStatement(getLexer().getLoc(),
1760 "Expected } at this point");
1761 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1762
1763 // Parse "zeroing non-masked" semantic {z}
1764 if (getLexer().is(AsmToken::LCurly)) {
1765 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1766 if (!getLexer().is(AsmToken::Identifier) ||
1767 getLexer().getTok().getIdentifier() != "z")
1768 return !ErrorAndEatStatement(getLexer().getLoc(),
1769 "Expected z at this point");
1770 Parser.Lex(); // Eat the z
1771 if (!getLexer().is(AsmToken::RCurly))
1772 return !ErrorAndEatStatement(getLexer().getLoc(),
1773 "Expected } at this point");
1774 Parser.Lex(); // Eat the }
1775 }
1776 }
1777 }
1778 }
1779 }
1780 return true;
1781}
1782
Chris Lattnerb9270732010-04-17 18:56:34 +00001783/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1784/// has already been parsed if present.
David Blaikie960ea3f2014-06-08 16:18:35 +00001785std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
1786 SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001787
Rafael Espindola961d4692014-11-11 05:18:41 +00001788 MCAsmParser &Parser = getParser();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001789 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1790 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001791 // only way to do this without lookahead is to eat the '(' and see what is
1792 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001793 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001794 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001795 SMLoc ExprEnd;
Craig Topper062a2ba2014-04-25 05:30:21 +00001796 if (getParser().parseExpression(Disp, ExprEnd)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001797
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001798 // After parsing the base expression we could either have a parenthesized
1799 // memory address or not. If not, return now. If so, eat the (.
1800 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001801 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001802 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00001803 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, ExprEnd);
1804 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
1805 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001806 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001807
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001808 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001809 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001810 } else {
1811 // Okay, we have a '('. We don't know if this is an expression or not, but
1812 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001813 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001814 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001815
Kevin Enderby7d912182009-09-03 17:15:07 +00001816 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001817 // Nothing to do here, fall into the code below with the '(' part of the
1818 // memory operand consumed.
1819 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001820 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001821
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001822 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001823 if (getParser().parseParenExpression(Disp, ExprEnd))
Craig Topper062a2ba2014-04-25 05:30:21 +00001824 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001825
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001826 // After parsing the base expression we could either have a parenthesized
1827 // memory address or not. If not, return now. If so, eat the (.
1828 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001829 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001830 if (SegReg == 0)
Craig Topper055845f2015-01-02 07:02:25 +00001831 return X86Operand::CreateMem(getPointerWidth(), Disp, LParenLoc,
1832 ExprEnd);
1833 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1,
1834 MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001835 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001836
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001837 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001838 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001839 }
1840 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001841
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001842 // If we reached here, then we just ate the ( of the memory operand. Process
1843 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001844 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00001845 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001846
Chris Lattner0c2538f2010-01-15 18:51:29 +00001847 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001848 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00001849 BaseLoc = Parser.getTok().getLoc();
Craig Topper062a2ba2014-04-25 05:30:21 +00001850 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001851 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001852 Error(StartLoc, "eiz and riz can only be used as index registers",
1853 SMRange(StartLoc, EndLoc));
Craig Topper062a2ba2014-04-25 05:30:21 +00001854 return nullptr;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001855 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001856 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001857
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001858 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001859 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001860 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001861
1862 // Following the comma we should have either an index register, or a scale
1863 // value. We don't support the later form, but we want to parse it
1864 // correctly.
1865 //
1866 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001867 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001868 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001869 SMLoc L;
Craig Topper062a2ba2014-04-25 05:30:21 +00001870 if (ParseRegister(IndexReg, L, L)) return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001871
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001872 if (getLexer().isNot(AsmToken::RParen)) {
1873 // Parse the scale amount:
1874 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001875 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001876 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001877 "expected comma in scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001878 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001879 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001880 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001881
1882 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001883 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001884
1885 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001886 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001887 Error(Loc, "expected scale expression");
Craig Topper062a2ba2014-04-25 05:30:21 +00001888 return nullptr;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001889 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001890
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001891 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00001892 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1893 ScaleVal != 1) {
1894 Error(Loc, "scale factor in 16-bit address must be 1");
Craig Topper062a2ba2014-04-25 05:30:21 +00001895 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001896 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001897 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1898 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
Craig Topper062a2ba2014-04-25 05:30:21 +00001899 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001900 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001901 Scale = (unsigned)ScaleVal;
1902 }
1903 }
1904 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001905 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001906 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001907 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001908
1909 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001910 if (getParser().parseAbsoluteExpression(Value))
Craig Topper062a2ba2014-04-25 05:30:21 +00001911 return nullptr;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001912
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001913 if (Value != 1)
1914 Warning(Loc, "scale factor without index register is ignored");
1915 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001916 }
1917 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001918
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001919 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001920 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001921 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Craig Topper062a2ba2014-04-25 05:30:21 +00001922 return nullptr;
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001923 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001924 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001925 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001926
David Woodhouse6dbda442014-01-08 12:58:28 +00001927 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1928 // and then only in non-64-bit modes. Except for DX, which is a special case
1929 // because an unofficial form of in/out instructions uses it.
1930 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1931 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1932 BaseReg != X86::SI && BaseReg != X86::DI)) &&
1933 BaseReg != X86::DX) {
1934 Error(BaseLoc, "invalid 16-bit base register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001935 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001936 }
1937 if (BaseReg == 0 &&
1938 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
1939 Error(IndexLoc, "16-bit memory operand may not include only index register");
Craig Topper062a2ba2014-04-25 05:30:21 +00001940 return nullptr;
David Woodhouse6dbda442014-01-08 12:58:28 +00001941 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001942
1943 StringRef ErrMsg;
1944 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1945 Error(BaseLoc, ErrMsg);
Craig Topper062a2ba2014-04-25 05:30:21 +00001946 return nullptr;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001947 }
1948
Reid Klecknerb7e2f602014-07-31 23:26:35 +00001949 if (SegReg || BaseReg || IndexReg)
Craig Topper055845f2015-01-02 07:02:25 +00001950 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
1951 IndexReg, Scale, MemStart, MemEnd);
1952 return X86Operand::CreateMem(getPointerWidth(), Disp, MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001953}
1954
David Blaikie960ea3f2014-06-08 16:18:35 +00001955bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
1956 SMLoc NameLoc, OperandVector &Operands) {
Rafael Espindola961d4692014-11-11 05:18:41 +00001957 MCAsmParser &Parser = getParser();
Chad Rosierf0e87202012-10-25 20:41:34 +00001958 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001959 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001960
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001961 // FIXME: Hack to recognize setneb as setne.
1962 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1963 PatchedName != "setb" && PatchedName != "setnb")
1964 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001965
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001966 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
Craig Topper062a2ba2014-04-25 05:30:21 +00001967 const MCExpr *ExtraImmOp = nullptr;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001968 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001969 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1970 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001971 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001972 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001973 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001974 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001975 .Case("eq", 0x00)
1976 .Case("lt", 0x01)
1977 .Case("le", 0x02)
1978 .Case("unord", 0x03)
1979 .Case("neq", 0x04)
1980 .Case("nlt", 0x05)
1981 .Case("nle", 0x06)
1982 .Case("ord", 0x07)
1983 /* AVX only from here */
1984 .Case("eq_uq", 0x08)
1985 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001986 .Case("ngt", 0x0A)
1987 .Case("false", 0x0B)
1988 .Case("neq_oq", 0x0C)
1989 .Case("ge", 0x0D)
1990 .Case("gt", 0x0E)
1991 .Case("true", 0x0F)
1992 .Case("eq_os", 0x10)
1993 .Case("lt_oq", 0x11)
1994 .Case("le_oq", 0x12)
1995 .Case("unord_s", 0x13)
1996 .Case("neq_us", 0x14)
1997 .Case("nlt_uq", 0x15)
1998 .Case("nle_uq", 0x16)
1999 .Case("ord_s", 0x17)
2000 .Case("eq_us", 0x18)
2001 .Case("nge_uq", 0x19)
2002 .Case("ngt_uq", 0x1A)
2003 .Case("false_os", 0x1B)
2004 .Case("neq_os", 0x1C)
2005 .Case("ge_oq", 0x1D)
2006 .Case("gt_oq", 0x1E)
2007 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002008 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00002009 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002010 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
2011 getParser().getContext());
2012 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002013 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002014 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002015 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002016 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002017 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002018 } else {
2019 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002020 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002021 }
2022 }
2023 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002024
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002025 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002026
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002027 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002028 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00002029
Chris Lattner086a83a2010-09-08 05:17:37 +00002030 // Determine whether this is an instruction prefix.
2031 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00002032 Name == "lock" || Name == "rep" ||
2033 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00002034 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00002035 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00002036
2037
Chris Lattner086a83a2010-09-08 05:17:37 +00002038 // This does the actual operand parsing. Don't parse any more if we have a
2039 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2040 // just want to parse the "lock" as the first instruction and the "incl" as
2041 // the next one.
2042 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002043
2044 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002045 if (getLexer().is(AsmToken::Star))
2046 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002047
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002048 // Read the operands.
2049 while(1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002050 if (std::unique_ptr<X86Operand> Op = ParseOperand()) {
2051 Operands.push_back(std::move(Op));
2052 if (!HandleAVX512Operand(Operands, *Operands.back()))
Elena Demikhovsky89529742013-09-12 08:55:00 +00002053 return true;
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002054 } else {
2055 Parser.eatToEndOfStatement();
2056 return true;
Elena Demikhovsky89529742013-09-12 08:55:00 +00002057 }
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002058 // check for comma and eat it
2059 if (getLexer().is(AsmToken::Comma))
2060 Parser.Lex();
2061 else
2062 break;
2063 }
Elena Demikhovsky89529742013-09-12 08:55:00 +00002064
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002065 if (getLexer().isNot(AsmToken::EndOfStatement))
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002066 return ErrorAndEatStatement(getLexer().getLoc(),
2067 "unexpected token in argument list");
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002068 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002069
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002070 // Consume the EndOfStatement or the prefix separator Slash
Elena Demikhovsky9f09b3e2014-02-20 07:00:10 +00002071 if (getLexer().is(AsmToken::EndOfStatement) ||
2072 (isPrefix && getLexer().is(AsmToken::Slash)))
Elena Demikhovskyc9657012014-02-20 06:34:39 +00002073 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002074
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002075 if (ExtraImmOp && isParsingIntelSyntax())
2076 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2077
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002078 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2079 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2080 // documented form in various unofficial manuals, so a lot of code uses it.
2081 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2082 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002083 X86Operand &Op = (X86Operand &)*Operands.back();
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002084 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2085 isa<MCConstantExpr>(Op.Mem.Disp) &&
2086 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2087 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2088 SMLoc Loc = Op.getEndLoc();
2089 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002090 }
2091 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002092 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2093 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2094 Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002095 X86Operand &Op = (X86Operand &)*Operands[1];
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002096 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2097 isa<MCConstantExpr>(Op.Mem.Disp) &&
2098 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2099 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2100 SMLoc Loc = Op.getEndLoc();
David Blaikie960ea3f2014-06-08 16:18:35 +00002101 Operands[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002102 }
2103 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002104
2105 // Append default arguments to "ins[bwld]"
2106 if (Name.startswith("ins") && Operands.size() == 1 &&
2107 (Name == "insb" || Name == "insw" || Name == "insl" ||
2108 Name == "insd" )) {
2109 if (isParsingIntelSyntax()) {
2110 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2111 Operands.push_back(DefaultMemDIOperand(NameLoc));
2112 } else {
2113 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2114 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002115 }
2116 }
2117
David Woodhousec472b812014-01-22 15:08:49 +00002118 // Append default arguments to "outs[bwld]"
2119 if (Name.startswith("outs") && Operands.size() == 1 &&
2120 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
2121 Name == "outsd" )) {
2122 if (isParsingIntelSyntax()) {
2123 Operands.push_back(DefaultMemSIOperand(NameLoc));
2124 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2125 } else {
2126 Operands.push_back(DefaultMemSIOperand(NameLoc));
2127 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002128 }
2129 }
2130
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002131 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2132 // values of $SIREG according to the mode. It would be nice if this
2133 // could be achieved with InstAlias in the tables.
2134 if (Name.startswith("lods") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002135 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002136 Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
2137 Operands.push_back(DefaultMemSIOperand(NameLoc));
2138
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002139 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2140 // values of $DIREG according to the mode. It would be nice if this
2141 // could be achieved with InstAlias in the tables.
2142 if (Name.startswith("stos") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002143 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002144 Name == "stosl" || Name == "stosd" || Name == "stosq"))
2145 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002146
David Woodhouse20fe4802014-01-22 15:08:27 +00002147 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2148 // values of $DIREG according to the mode. It would be nice if this
2149 // could be achieved with InstAlias in the tables.
2150 if (Name.startswith("scas") && Operands.size() == 1 &&
2151 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
2152 Name == "scasl" || Name == "scasd" || Name == "scasq"))
2153 Operands.push_back(DefaultMemDIOperand(NameLoc));
2154
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002155 // Add default SI and DI operands to "cmps[bwlq]".
2156 if (Name.startswith("cmps") &&
2157 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2158 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
2159 if (Operands.size() == 1) {
2160 if (isParsingIntelSyntax()) {
2161 Operands.push_back(DefaultMemSIOperand(NameLoc));
2162 Operands.push_back(DefaultMemDIOperand(NameLoc));
2163 } else {
2164 Operands.push_back(DefaultMemDIOperand(NameLoc));
2165 Operands.push_back(DefaultMemSIOperand(NameLoc));
2166 }
2167 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002168 X86Operand &Op = (X86Operand &)*Operands[1];
2169 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002170 if (!doSrcDstMatch(Op, Op2))
2171 return Error(Op.getStartLoc(),
2172 "mismatching source and destination index registers");
2173 }
2174 }
2175
David Woodhouse6f417de2014-01-22 15:08:42 +00002176 // Add default SI and DI operands to "movs[bwlq]".
2177 if ((Name.startswith("movs") &&
2178 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2179 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2180 (Name.startswith("smov") &&
2181 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2182 Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2183 if (Operands.size() == 1) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002184 if (Name == "movsd")
David Woodhouse6f417de2014-01-22 15:08:42 +00002185 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2186 if (isParsingIntelSyntax()) {
2187 Operands.push_back(DefaultMemDIOperand(NameLoc));
2188 Operands.push_back(DefaultMemSIOperand(NameLoc));
2189 } else {
2190 Operands.push_back(DefaultMemSIOperand(NameLoc));
2191 Operands.push_back(DefaultMemDIOperand(NameLoc));
2192 }
2193 } else if (Operands.size() == 3) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002194 X86Operand &Op = (X86Operand &)*Operands[1];
2195 X86Operand &Op2 = (X86Operand &)*Operands[2];
David Woodhouse6f417de2014-01-22 15:08:42 +00002196 if (!doSrcDstMatch(Op, Op2))
2197 return Error(Op.getStartLoc(),
2198 "mismatching source and destination index registers");
2199 }
2200 }
2201
Chris Lattner4bd21712010-09-15 04:33:27 +00002202 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002203 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002204 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002205 Name.startswith("shl") || Name.startswith("sal") ||
2206 Name.startswith("rcl") || Name.startswith("rcr") ||
2207 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002208 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002209 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002210 // Intel syntax
David Blaikie960ea3f2014-06-08 16:18:35 +00002211 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]);
2212 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2213 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002214 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002215 } else {
David Blaikie960ea3f2014-06-08 16:18:35 +00002216 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2217 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2218 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1)
Craig Topper6bf3ed42012-07-18 04:59:16 +00002219 Operands.erase(Operands.begin() + 1);
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002220 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002221 }
Chad Rosier51afe632012-06-27 22:34:28 +00002222
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002223 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2224 // instalias with an immediate operand yet.
2225 if (Name == "int" && Operands.size() == 2) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002226 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
2227 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) &&
2228 cast<MCConstantExpr>(Op1.getImm())->getValue() == 3) {
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002229 Operands.erase(Operands.begin() + 1);
David Blaikie960ea3f2014-06-08 16:18:35 +00002230 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3");
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002231 }
2232 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002233
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002234 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002235}
2236
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002237static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2238 bool isCmp) {
2239 MCInst TmpInst;
2240 TmpInst.setOpcode(Opcode);
2241 if (!isCmp)
2242 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2243 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2244 TmpInst.addOperand(Inst.getOperand(0));
2245 Inst = TmpInst;
2246 return true;
2247}
2248
2249static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2250 bool isCmp = false) {
2251 if (!Inst.getOperand(0).isImm() ||
2252 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2253 return false;
2254
2255 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2256}
2257
2258static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2259 bool isCmp = false) {
2260 if (!Inst.getOperand(0).isImm() ||
2261 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2262 return false;
2263
2264 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2265}
2266
2267static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2268 bool isCmp = false) {
2269 if (!Inst.getOperand(0).isImm() ||
2270 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2271 return false;
2272
2273 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2274}
2275
Saleem Abdulrasoolca24b1d2015-01-14 05:10:21 +00002276bool X86AsmParser::validateInstruction(MCInst &Inst, const OperandVector &Ops) {
2277 switch (Inst.getOpcode()) {
2278 default: return true;
2279 case X86::INT:
David Majnemer7efc6132015-01-14 06:14:36 +00002280 X86Operand &Op = static_cast<X86Operand &>(*Ops[1]);
2281 assert(Op.isImm() && "expected immediate");
2282 int64_t Res;
2283 if (!Op.getImm()->EvaluateAsAbsolute(Res) || Res > 255) {
2284 Error(Op.getStartLoc(), "interrupt vector must be in range [0-255]");
Saleem Abdulrasoolca24b1d2015-01-14 05:10:21 +00002285 return false;
2286 }
2287 return true;
2288 }
2289 llvm_unreachable("handle the instruction appropriately");
2290}
2291
David Blaikie960ea3f2014-06-08 16:18:35 +00002292bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
Devang Patelde47cce2012-01-18 22:42:29 +00002293 switch (Inst.getOpcode()) {
2294 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002295 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2296 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2297 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2298 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2299 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2300 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2301 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2302 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2303 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2304 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2305 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2306 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2307 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2308 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2309 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2310 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2311 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2312 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002313 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2314 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2315 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2316 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2317 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2318 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002319 case X86::VMOVAPDrr:
2320 case X86::VMOVAPDYrr:
2321 case X86::VMOVAPSrr:
2322 case X86::VMOVAPSYrr:
2323 case X86::VMOVDQArr:
2324 case X86::VMOVDQAYrr:
2325 case X86::VMOVDQUrr:
2326 case X86::VMOVDQUYrr:
2327 case X86::VMOVUPDrr:
2328 case X86::VMOVUPDYrr:
2329 case X86::VMOVUPSrr:
2330 case X86::VMOVUPSYrr: {
2331 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2332 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2333 return false;
2334
2335 unsigned NewOpc;
2336 switch (Inst.getOpcode()) {
2337 default: llvm_unreachable("Invalid opcode");
2338 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2339 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2340 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2341 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2342 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2343 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2344 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2345 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2346 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2347 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2348 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2349 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2350 }
2351 Inst.setOpcode(NewOpc);
2352 return true;
2353 }
2354 case X86::VMOVSDrr:
2355 case X86::VMOVSSrr: {
2356 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2357 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2358 return false;
2359 unsigned NewOpc;
2360 switch (Inst.getOpcode()) {
2361 default: llvm_unreachable("Invalid opcode");
2362 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2363 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2364 }
2365 Inst.setOpcode(NewOpc);
2366 return true;
2367 }
Devang Patelde47cce2012-01-18 22:42:29 +00002368 }
Devang Patelde47cce2012-01-18 22:42:29 +00002369}
2370
Tim Northover26bb14e2014-08-18 11:49:42 +00002371static const char *getSubtargetFeatureName(uint64_t Val);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002372
David Blaikie960ea3f2014-06-08 16:18:35 +00002373void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands,
2374 MCStreamer &Out) {
Evgeniy Stepanov77ad8662014-07-31 09:11:04 +00002375 Instrumentation->InstrumentAndEmitInstruction(Inst, Operands, getContext(),
2376 MII, Out);
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002377}
2378
David Blaikie960ea3f2014-06-08 16:18:35 +00002379bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2380 OperandVector &Operands,
Tim Northover26bb14e2014-08-18 11:49:42 +00002381 MCStreamer &Out, uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00002382 bool MatchingInlineAsm) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002383 if (isParsingIntelSyntax())
2384 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
2385 MatchingInlineAsm);
2386 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
2387 MatchingInlineAsm);
2388}
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002389
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002390void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
2391 OperandVector &Operands, MCStreamer &Out,
2392 bool MatchingInlineAsm) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002393 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002394 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002395 // call.
Reid Klecknerb1f2d2f2014-07-31 00:07:33 +00002396 const char *Repl = StringSwitch<const char *>(Op.getToken())
2397 .Case("finit", "fninit")
2398 .Case("fsave", "fnsave")
2399 .Case("fstcw", "fnstcw")
2400 .Case("fstcww", "fnstcw")
2401 .Case("fstenv", "fnstenv")
2402 .Case("fstsw", "fnstsw")
2403 .Case("fstsww", "fnstsw")
2404 .Case("fclex", "fnclex")
2405 .Default(nullptr);
2406 if (Repl) {
Chris Lattnera63292a2010-09-29 01:50:45 +00002407 MCInst Inst;
2408 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002409 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002410 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002411 EmitInstruction(Inst, Operands, Out);
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002412 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002413 }
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002414}
2415
2416bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
2417 bool MatchingInlineAsm) {
2418 assert(ErrorInfo && "Unknown missing feature!");
2419 ArrayRef<SMRange> EmptyRanges = None;
2420 SmallString<126> Msg;
2421 raw_svector_ostream OS(Msg);
2422 OS << "instruction requires:";
2423 uint64_t Mask = 1;
2424 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2425 if (ErrorInfo & Mask)
2426 OS << ' ' << getSubtargetFeatureName(ErrorInfo & Mask);
2427 Mask <<= 1;
2428 }
2429 return Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
2430}
2431
2432bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
2433 OperandVector &Operands,
2434 MCStreamer &Out,
2435 uint64_t &ErrorInfo,
2436 bool MatchingInlineAsm) {
2437 assert(!Operands.empty() && "Unexpect empty operand list!");
2438 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2439 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
2440 ArrayRef<SMRange> EmptyRanges = None;
2441
2442 // First, handle aliases that expand to multiple instructions.
2443 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002444
Chris Lattner628fbec2010-09-06 21:54:15 +00002445 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002446 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002447
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002448 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002449 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002450 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002451 isParsingIntelSyntax())) {
Craig Topper589ceee2015-01-03 08:16:34 +00002452 default: llvm_unreachable("Unexpected match result!");
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002453 case Match_Success:
Saleem Abdulrasoolca24b1d2015-01-14 05:10:21 +00002454 if (!validateInstruction(Inst, Operands))
2455 return true;
2456
Devang Patelde47cce2012-01-18 22:42:29 +00002457 // Some instructions need post-processing to, for example, tweak which
2458 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002459 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002460 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002461 while (processInstruction(Inst, Operands))
2462 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002463
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002464 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002465 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002466 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002467 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002468 return false;
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002469 case Match_MissingFeature:
2470 return ErrorMissingFeature(IDLoc, ErrorInfo, MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002471 case Match_InvalidOperand:
2472 WasOriginallyInvalidOperand = true;
2473 break;
2474 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002475 break;
2476 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002477
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002478 // FIXME: Ideally, we would only attempt suffix matches for things which are
2479 // valid prefixes, and we could just infer the right unambiguous
2480 // type. However, that requires substantially more matcher support than the
2481 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002482
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002483 // Change the operand to point to a temporary token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002484 StringRef Base = Op.getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002485 SmallString<16> Tmp;
2486 Tmp += Base;
2487 Tmp += ' ';
David Blaikie960ea3f2014-06-08 16:18:35 +00002488 Op.setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002489
Chris Lattnerfab94132010-11-06 18:28:02 +00002490 // If this instruction starts with an 'f', then it is a floating point stack
2491 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2492 // 80-bit floating point, which use the suffixes s,l,t respectively.
2493 //
2494 // Otherwise, we assume that this may be an integer instruction, which comes
2495 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2496 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002497
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002498 // Check for the various suffix matches.
Tim Northover26bb14e2014-08-18 11:49:42 +00002499 uint64_t ErrorInfoIgnore;
2500 uint64_t ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002501 unsigned Match[4];
Chad Rosier51afe632012-06-27 22:34:28 +00002502
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002503 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
2504 Tmp.back() = Suffixes[I];
2505 Match[I] = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2506 MatchingInlineAsm, isParsingIntelSyntax());
2507 // If this returned as a missing feature failure, remember that.
2508 if (Match[I] == Match_MissingFeature)
2509 ErrorInfoMissingFeature = ErrorInfoIgnore;
2510 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002511
2512 // Restore the old token.
David Blaikie960ea3f2014-06-08 16:18:35 +00002513 Op.setTokenValue(Base);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002514
2515 // If exactly one matched, then we treat that as a successful match (and the
2516 // instruction will already have been filled in correctly, since the failing
2517 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002518 unsigned NumSuccessfulMatches =
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002519 std::count(std::begin(Match), std::end(Match), Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002520 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002521 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002522 if (!MatchingInlineAsm)
Evgeniy Stepanov49e26252014-03-14 08:58:04 +00002523 EmitInstruction(Inst, Operands, Out);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002524 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002525 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002526 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002527
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002528 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002529
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002530 // If we had multiple suffix matches, then identify this as an ambiguous
2531 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002532 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002533 char MatchChars[4];
2534 unsigned NumMatches = 0;
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002535 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
2536 if (Match[I] == Match_Success)
2537 MatchChars[NumMatches++] = Suffixes[I];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002538
Alp Tokere69170a2014-06-26 22:52:05 +00002539 SmallString<126> Msg;
2540 raw_svector_ostream OS(Msg);
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002541 OS << "ambiguous instructions require an explicit suffix (could be ";
2542 for (unsigned i = 0; i != NumMatches; ++i) {
2543 if (i != 0)
2544 OS << ", ";
2545 if (i + 1 == NumMatches)
2546 OS << "or ";
2547 OS << "'" << Base << MatchChars[i] << "'";
2548 }
2549 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002550 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002551 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002552 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002553
Chris Lattner628fbec2010-09-06 21:54:15 +00002554 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002555
Chris Lattner628fbec2010-09-06 21:54:15 +00002556 // If all of the instructions reported an invalid mnemonic, then the original
2557 // mnemonic was invalid.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002558 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002559 if (!WasOriginallyInvalidOperand) {
David Blaikie960ea3f2014-06-08 16:18:35 +00002560 ArrayRef<SMRange> Ranges =
2561 MatchingInlineAsm ? EmptyRanges : Op.getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002562 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002563 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002564 }
2565
2566 // Recover location info for the operand if we know which was the problem.
Tim Northover26bb14e2014-08-18 11:49:42 +00002567 if (ErrorInfo != ~0ULL) {
Chad Rosier49963552012-10-13 00:26:04 +00002568 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002569 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002570 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002571
David Blaikie960ea3f2014-06-08 16:18:35 +00002572 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
2573 if (Operand.getStartLoc().isValid()) {
2574 SMRange OperandRange = Operand.getLocRange();
2575 return Error(Operand.getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002576 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002577 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002578 }
2579
Chad Rosier3d4bc622012-08-21 19:36:59 +00002580 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002581 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002582 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002583
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002584 // If one instruction matched with a missing feature, report this as a
2585 // missing feature.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002586 if (std::count(std::begin(Match), std::end(Match),
2587 Match_MissingFeature) == 1) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002588 ErrorInfo = ErrorInfoMissingFeature;
2589 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
2590 MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002591 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002592
Chris Lattner628fbec2010-09-06 21:54:15 +00002593 // If one instruction matched with an invalid operand, report this as an
2594 // operand failure.
Reid Kleckner7b1e1a02014-07-30 22:23:11 +00002595 if (std::count(std::begin(Match), std::end(Match),
2596 Match_InvalidOperand) == 1) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002597 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
2598 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002599 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002600
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002601 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002602 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002603 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002604 return true;
2605}
2606
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002607bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
2608 OperandVector &Operands,
2609 MCStreamer &Out,
2610 uint64_t &ErrorInfo,
2611 bool MatchingInlineAsm) {
2612 assert(!Operands.empty() && "Unexpect empty operand list!");
2613 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
2614 assert(Op.isToken() && "Leading operand should always be a mnemonic!");
2615 StringRef Mnemonic = Op.getToken();
2616 ArrayRef<SMRange> EmptyRanges = None;
2617
2618 // First, handle aliases that expand to multiple instructions.
2619 MatchFPUWaitAlias(IDLoc, Op, Operands, Out, MatchingInlineAsm);
2620
2621 MCInst Inst;
2622
2623 // Find one unsized memory operand, if present.
2624 X86Operand *UnsizedMemOp = nullptr;
2625 for (const auto &Op : Operands) {
2626 X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002627 if (X86Op->isMemUnsized())
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002628 UnsizedMemOp = X86Op;
2629 }
2630
2631 // Allow some instructions to have implicitly pointer-sized operands. This is
2632 // compatible with gas.
2633 if (UnsizedMemOp) {
2634 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"};
2635 for (const char *Instr : PtrSizedInstrs) {
2636 if (Mnemonic == Instr) {
Craig Topper055845f2015-01-02 07:02:25 +00002637 UnsizedMemOp->Mem.Size = getPointerWidth();
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002638 break;
2639 }
2640 }
2641 }
2642
2643 // If an unsized memory operand is present, try to match with each memory
2644 // operand size. In Intel assembly, the size is not part of the instruction
2645 // mnemonic.
2646 SmallVector<unsigned, 8> Match;
2647 uint64_t ErrorInfoMissingFeature = 0;
2648 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) {
Ahmed Bougachad65f7872014-12-03 02:03:26 +00002649 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512};
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002650 for (unsigned Size : MopSizes) {
2651 UnsizedMemOp->Mem.Size = Size;
2652 uint64_t ErrorInfoIgnore;
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002653 unsigned LastOpcode = Inst.getOpcode();
2654 unsigned M =
2655 MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2656 MatchingInlineAsm, isParsingIntelSyntax());
2657 if (Match.empty() || LastOpcode != Inst.getOpcode())
2658 Match.push_back(M);
2659
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002660 // If this returned as a missing feature failure, remember that.
2661 if (Match.back() == Match_MissingFeature)
2662 ErrorInfoMissingFeature = ErrorInfoIgnore;
2663 }
Reid Kleckner7b7a5992014-08-27 20:10:38 +00002664
2665 // Restore the size of the unsized memory operand if we modified it.
2666 if (UnsizedMemOp)
2667 UnsizedMemOp->Mem.Size = 0;
2668 }
2669
2670 // If we haven't matched anything yet, this is not a basic integer or FPU
2671 // operation. There shouldn't be any ambiguity in our mneumonic table, so try
2672 // matching with the unsized operand.
2673 if (Match.empty()) {
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002674 Match.push_back(MatchInstructionImpl(Operands, Inst, ErrorInfo,
2675 MatchingInlineAsm,
2676 isParsingIntelSyntax()));
2677 // If this returned as a missing feature failure, remember that.
2678 if (Match.back() == Match_MissingFeature)
2679 ErrorInfoMissingFeature = ErrorInfo;
2680 }
2681
2682 // Restore the size of the unsized memory operand if we modified it.
2683 if (UnsizedMemOp)
2684 UnsizedMemOp->Mem.Size = 0;
2685
2686 // If it's a bad mnemonic, all results will be the same.
2687 if (Match.back() == Match_MnemonicFail) {
2688 ArrayRef<SMRange> Ranges =
2689 MatchingInlineAsm ? EmptyRanges : Op.getLocRange();
2690 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'",
2691 Ranges, MatchingInlineAsm);
2692 }
2693
2694 // If exactly one matched, then we treat that as a successful match (and the
2695 // instruction will already have been filled in correctly, since the failing
2696 // matches won't have modified it).
2697 unsigned NumSuccessfulMatches =
2698 std::count(std::begin(Match), std::end(Match), Match_Success);
2699 if (NumSuccessfulMatches == 1) {
Saleem Abdulrasoolca24b1d2015-01-14 05:10:21 +00002700 if (!validateInstruction(Inst, Operands))
2701 return true;
2702
Reid Klecknerf6fb7802014-08-26 20:32:34 +00002703 // Some instructions need post-processing to, for example, tweak which
2704 // encoding is selected. Loop on it while changes happen so the individual
2705 // transformations can chain off each other.
2706 if (!MatchingInlineAsm)
2707 while (processInstruction(Inst, Operands))
2708 ;
2709 Inst.setLoc(IDLoc);
2710 if (!MatchingInlineAsm)
2711 EmitInstruction(Inst, Operands, Out);
2712 Opcode = Inst.getOpcode();
2713 return false;
2714 } else if (NumSuccessfulMatches > 1) {
2715 assert(UnsizedMemOp &&
2716 "multiple matches only possible with unsized memory operands");
2717 ArrayRef<SMRange> Ranges =
2718 MatchingInlineAsm ? EmptyRanges : UnsizedMemOp->getLocRange();
2719 return Error(UnsizedMemOp->getStartLoc(),
2720 "ambiguous operand size for instruction '" + Mnemonic + "\'",
2721 Ranges, MatchingInlineAsm);
2722 }
2723
2724 // If one instruction matched with a missing feature, report this as a
2725 // missing feature.
2726 if (std::count(std::begin(Match), std::end(Match),
2727 Match_MissingFeature) == 1) {
2728 ErrorInfo = ErrorInfoMissingFeature;
2729 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeature,
2730 MatchingInlineAsm);
2731 }
2732
2733 // If one instruction matched with an invalid operand, report this as an
2734 // operand failure.
2735 if (std::count(std::begin(Match), std::end(Match),
2736 Match_InvalidOperand) == 1) {
2737 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
2738 MatchingInlineAsm);
2739 }
2740
2741 // If all of these were an outright failure, report it in a useless way.
2742 return Error(IDLoc, "unknown instruction mnemonic", EmptyRanges,
2743 MatchingInlineAsm);
2744}
2745
Nico Weber42f79db2014-07-17 20:24:55 +00002746bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
2747 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
2748}
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002749
Devang Patel4a6e7782012-01-12 18:03:40 +00002750bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002751 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00002752 StringRef IDVal = DirectiveID.getIdentifier();
2753 if (IDVal == ".word")
2754 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002755 else if (IDVal.startswith(".code"))
2756 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002757 else if (IDVal.startswith(".att_syntax")) {
Reid Klecknerce63b792014-08-06 23:21:13 +00002758 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2759 if (Parser.getTok().getString() == "prefix")
2760 Parser.Lex();
2761 else if (Parser.getTok().getString() == "noprefix")
2762 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
2763 "supported: registers must have a "
2764 "'%' prefix in .att_syntax");
2765 }
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002766 getParser().setAssemblerDialect(0);
2767 return false;
2768 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002769 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002770 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002771 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002772 Parser.Lex();
Reid Klecknerce63b792014-08-06 23:21:13 +00002773 else if (Parser.getTok().getString() == "prefix")
2774 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
2775 "supported: registers must not have "
2776 "a '%' prefix in .intel_syntax");
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002777 }
2778 return false;
2779 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002780 return true;
2781}
2782
2783/// ParseDirectiveWord
2784/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002785bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002786 MCAsmParser &Parser = getParser();
Chris Lattner72c0b592010-10-30 17:38:55 +00002787 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2788 for (;;) {
2789 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002790 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002791 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002792
Eric Christopherbf7bc492013-01-09 03:52:05 +00002793 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002794
Chris Lattner72c0b592010-10-30 17:38:55 +00002795 if (getLexer().is(AsmToken::EndOfStatement))
2796 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002797
Chris Lattner72c0b592010-10-30 17:38:55 +00002798 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002799 if (getLexer().isNot(AsmToken::Comma)) {
2800 Error(L, "unexpected token in directive");
2801 return false;
2802 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002803 Parser.Lex();
2804 }
2805 }
Chad Rosier51afe632012-06-27 22:34:28 +00002806
Chris Lattner72c0b592010-10-30 17:38:55 +00002807 Parser.Lex();
2808 return false;
2809}
2810
Evan Cheng481ebb02011-07-27 00:38:12 +00002811/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002812/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002813bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Rafael Espindola961d4692014-11-11 05:18:41 +00002814 MCAsmParser &Parser = getParser();
Craig Topper3c80d622014-01-06 04:55:54 +00002815 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002816 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002817 if (!is16BitMode()) {
2818 SwitchMode(X86::Mode16Bit);
2819 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2820 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002821 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002822 Parser.Lex();
2823 if (!is32BitMode()) {
2824 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002825 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2826 }
2827 } else if (IDVal == ".code64") {
2828 Parser.Lex();
2829 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002830 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002831 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2832 }
2833 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002834 Error(L, "unknown directive " + IDVal);
2835 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002836 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002837
Evan Cheng481ebb02011-07-27 00:38:12 +00002838 return false;
2839}
Chris Lattner72c0b592010-10-30 17:38:55 +00002840
Daniel Dunbar71475772009-07-17 20:42:00 +00002841// Force static initialization.
2842extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002843 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2844 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002845}
Daniel Dunbar00331992009-07-29 00:02:19 +00002846
Chris Lattner3e4582a2010-09-06 19:11:01 +00002847#define GET_REGISTER_MATCHER
2848#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002849#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002850#include "X86GenAsmMatcher.inc"