blob: 296416ec0a2997cd4c3ce6e72c814d5f98750cb2 [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"
Chad Rosier6844ea02012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000012#include "llvm/ADT/STLExtras.h"
Chris Lattner1261b812010-09-22 04:11:10 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000015#include "llvm/ADT/StringSwitch.h"
16#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000017#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCParser/MCAsmLexer.h"
21#include "llvm/MC/MCParser/MCAsmParser.h"
22#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCStreamer.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000028#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000030#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000031
Daniel Dunbar71475772009-07-17 20:42:00 +000032using namespace llvm;
33
34namespace {
Benjamin Kramerb60210e2009-07-31 11:35:26 +000035struct X86Operand;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000036
Chad Rosier5362af92013-04-16 18:15:40 +000037static const char OpPrecedence[] = {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000038 0, // IC_OR
39 1, // IC_AND
40 2, // IC_PLUS
41 2, // IC_MINUS
42 3, // IC_MULTIPLY
43 3, // IC_DIVIDE
44 4, // IC_RPAREN
45 5, // IC_LPAREN
Chad Rosier5362af92013-04-16 18:15:40 +000046 0, // IC_IMM
47 0 // IC_REGISTER
48};
49
Devang Patel4a6e7782012-01-12 18:03:40 +000050class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000051 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000052 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000053 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000054private:
Alp Tokera5b88a52013-12-02 16:06:06 +000055 SMLoc consumeToken() {
56 SMLoc Result = Parser.getTok().getLoc();
57 Parser.Lex();
58 return Result;
59 }
60
Chad Rosier5362af92013-04-16 18:15:40 +000061 enum InfixCalculatorTok {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +000062 IC_OR = 0,
63 IC_AND,
64 IC_PLUS,
Chad Rosier5362af92013-04-16 18:15:40 +000065 IC_MINUS,
66 IC_MULTIPLY,
67 IC_DIVIDE,
68 IC_RPAREN,
69 IC_LPAREN,
70 IC_IMM,
71 IC_REGISTER
72 };
73
74 class InfixCalculator {
75 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
76 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
77 SmallVector<ICToken, 4> PostfixStack;
78
79 public:
80 int64_t popOperand() {
81 assert (!PostfixStack.empty() && "Poped an empty stack!");
82 ICToken Op = PostfixStack.pop_back_val();
83 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
84 && "Expected and immediate or register!");
85 return Op.second;
86 }
87 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
88 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
89 "Unexpected operand!");
90 PostfixStack.push_back(std::make_pair(Op, Val));
91 }
92
Jakub Staszak9c349222013-08-08 15:48:46 +000093 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +000094 void pushOperator(InfixCalculatorTok Op) {
95 // Push the new operator if the stack is empty.
96 if (InfixOperatorStack.empty()) {
97 InfixOperatorStack.push_back(Op);
98 return;
99 }
100
101 // Push the new operator if it has a higher precedence than the operator
102 // on the top of the stack or the operator on the top of the stack is a
103 // left parentheses.
104 unsigned Idx = InfixOperatorStack.size() - 1;
105 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
106 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
107 InfixOperatorStack.push_back(Op);
108 return;
109 }
110
111 // The operator on the top of the stack has higher precedence than the
112 // new operator.
113 unsigned ParenCount = 0;
114 while (1) {
115 // Nothing to process.
116 if (InfixOperatorStack.empty())
117 break;
118
119 Idx = InfixOperatorStack.size() - 1;
120 StackOp = InfixOperatorStack[Idx];
121 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
122 break;
123
124 // If we have an even parentheses count and we see a left parentheses,
125 // then stop processing.
126 if (!ParenCount && StackOp == IC_LPAREN)
127 break;
128
129 if (StackOp == IC_RPAREN) {
130 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000131 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000132 } else if (StackOp == IC_LPAREN) {
133 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000134 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000135 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000136 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000137 PostfixStack.push_back(std::make_pair(StackOp, 0));
138 }
139 }
140 // Push the new operator.
141 InfixOperatorStack.push_back(Op);
142 }
143 int64_t execute() {
144 // Push any remaining operators onto the postfix stack.
145 while (!InfixOperatorStack.empty()) {
146 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
147 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
148 PostfixStack.push_back(std::make_pair(StackOp, 0));
149 }
150
151 if (PostfixStack.empty())
152 return 0;
153
154 SmallVector<ICToken, 16> OperandStack;
155 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
156 ICToken Op = PostfixStack[i];
157 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
158 OperandStack.push_back(Op);
159 } else {
160 assert (OperandStack.size() > 1 && "Too few operands.");
161 int64_t Val;
162 ICToken Op2 = OperandStack.pop_back_val();
163 ICToken Op1 = OperandStack.pop_back_val();
164 switch (Op.first) {
165 default:
166 report_fatal_error("Unexpected operator!");
167 break;
168 case IC_PLUS:
169 Val = Op1.second + Op2.second;
170 OperandStack.push_back(std::make_pair(IC_IMM, Val));
171 break;
172 case IC_MINUS:
173 Val = Op1.second - Op2.second;
174 OperandStack.push_back(std::make_pair(IC_IMM, Val));
175 break;
176 case IC_MULTIPLY:
177 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
178 "Multiply operation with an immediate and a register!");
179 Val = Op1.second * Op2.second;
180 OperandStack.push_back(std::make_pair(IC_IMM, Val));
181 break;
182 case IC_DIVIDE:
183 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
184 "Divide operation with an immediate and a register!");
185 assert (Op2.second != 0 && "Division by zero!");
186 Val = Op1.second / Op2.second;
187 OperandStack.push_back(std::make_pair(IC_IMM, Val));
188 break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000189 case IC_OR:
190 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
191 "Or operation with an immediate and a register!");
192 Val = Op1.second | Op2.second;
193 OperandStack.push_back(std::make_pair(IC_IMM, Val));
194 break;
195 case IC_AND:
196 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
197 "And operation with an immediate and a register!");
198 Val = Op1.second & Op2.second;
199 OperandStack.push_back(std::make_pair(IC_IMM, Val));
200 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000201 }
202 }
203 }
204 assert (OperandStack.size() == 1 && "Expected a single result.");
205 return OperandStack.pop_back_val().second;
206 }
207 };
208
209 enum IntelExprState {
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000210 IES_OR,
211 IES_AND,
Chad Rosier5362af92013-04-16 18:15:40 +0000212 IES_PLUS,
213 IES_MINUS,
214 IES_MULTIPLY,
215 IES_DIVIDE,
216 IES_LBRAC,
217 IES_RBRAC,
218 IES_LPAREN,
219 IES_RPAREN,
220 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000221 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000222 IES_IDENTIFIER,
223 IES_ERROR
224 };
225
226 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000227 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000228 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000229 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000230 const MCExpr *Sym;
231 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000232 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000233 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000234 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000235 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000236 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000237 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
238 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000239 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000240
241 unsigned getBaseReg() { return BaseReg; }
242 unsigned getIndexReg() { return IndexReg; }
243 unsigned getScale() { return Scale; }
244 const MCExpr *getSym() { return Sym; }
245 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000246 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000247 bool isValidEndState() {
248 return State == IES_RBRAC || State == IES_INTEGER;
249 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000250 bool getStopOnLBrac() { return StopOnLBrac; }
251 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000252 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000253
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000254 InlineAsmIdentifierInfo &getIdentifierInfo() {
255 return Info;
256 }
257
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000258 void onOr() {
259 IntelExprState CurrState = State;
260 switch (State) {
261 default:
262 State = IES_ERROR;
263 break;
264 case IES_INTEGER:
265 case IES_RPAREN:
266 case IES_REGISTER:
267 State = IES_OR;
268 IC.pushOperator(IC_OR);
269 break;
270 }
271 PrevState = CurrState;
272 }
273 void onAnd() {
274 IntelExprState CurrState = State;
275 switch (State) {
276 default:
277 State = IES_ERROR;
278 break;
279 case IES_INTEGER:
280 case IES_RPAREN:
281 case IES_REGISTER:
282 State = IES_AND;
283 IC.pushOperator(IC_AND);
284 break;
285 }
286 PrevState = CurrState;
287 }
Chad Rosier5362af92013-04-16 18:15:40 +0000288 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000289 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000290 switch (State) {
291 default:
292 State = IES_ERROR;
293 break;
294 case IES_INTEGER:
295 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000296 case IES_REGISTER:
297 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000298 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000299 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
300 // If we already have a BaseReg, then assume this is the IndexReg with
301 // a scale of 1.
302 if (!BaseReg) {
303 BaseReg = TmpReg;
304 } else {
305 assert (!IndexReg && "BaseReg/IndexReg already set!");
306 IndexReg = TmpReg;
307 Scale = 1;
308 }
309 }
Chad Rosier5362af92013-04-16 18:15:40 +0000310 break;
311 }
Chad Rosier31246272013-04-17 21:01:45 +0000312 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000313 }
314 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000315 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000316 switch (State) {
317 default:
318 State = IES_ERROR;
319 break;
320 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000321 case IES_MULTIPLY:
322 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000323 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000324 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000325 case IES_LBRAC:
326 case IES_RBRAC:
327 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000328 case IES_REGISTER:
329 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000330 // Only push the minus operator if it is not a unary operator.
331 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
332 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
333 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
334 IC.pushOperator(IC_MINUS);
335 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
336 // If we already have a BaseReg, then assume this is the IndexReg with
337 // a scale of 1.
338 if (!BaseReg) {
339 BaseReg = TmpReg;
340 } else {
341 assert (!IndexReg && "BaseReg/IndexReg already set!");
342 IndexReg = TmpReg;
343 Scale = 1;
344 }
Chad Rosier5362af92013-04-16 18:15:40 +0000345 }
Chad Rosier5362af92013-04-16 18:15:40 +0000346 break;
347 }
Chad Rosier31246272013-04-17 21:01:45 +0000348 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000349 }
350 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000351 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000352 switch (State) {
353 default:
354 State = IES_ERROR;
355 break;
356 case IES_PLUS:
357 case IES_LPAREN:
358 State = IES_REGISTER;
359 TmpReg = Reg;
360 IC.pushOperand(IC_REGISTER);
361 break;
Chad Rosier31246272013-04-17 21:01:45 +0000362 case IES_MULTIPLY:
363 // Index Register - Scale * Register
364 if (PrevState == IES_INTEGER) {
365 assert (!IndexReg && "IndexReg already set!");
366 State = IES_REGISTER;
367 IndexReg = Reg;
368 // Get the scale and replace the 'Scale * Register' with '0'.
369 Scale = IC.popOperand();
370 IC.pushOperand(IC_IMM);
371 IC.popOperator();
372 } else {
373 State = IES_ERROR;
374 }
Chad Rosier5362af92013-04-16 18:15:40 +0000375 break;
376 }
Chad Rosier31246272013-04-17 21:01:45 +0000377 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000378 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000379 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000380 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000381 switch (State) {
382 default:
383 State = IES_ERROR;
384 break;
385 case IES_PLUS:
386 case IES_MINUS:
387 State = IES_INTEGER;
388 Sym = SymRef;
389 SymName = SymRefName;
390 IC.pushOperand(IC_IMM);
391 break;
392 }
393 }
Kevin Enderby9d117022014-01-23 21:52:41 +0000394 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
Chad Rosier31246272013-04-17 21:01:45 +0000395 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000396 switch (State) {
397 default:
398 State = IES_ERROR;
399 break;
400 case IES_PLUS:
401 case IES_MINUS:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000402 case IES_OR:
403 case IES_AND:
Chad Rosier5362af92013-04-16 18:15:40 +0000404 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000405 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000406 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000407 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000408 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
409 // Index Register - Register * Scale
410 assert (!IndexReg && "IndexReg already set!");
411 IndexReg = TmpReg;
412 Scale = TmpInt;
Kevin Enderby9d117022014-01-23 21:52:41 +0000413 if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
414 ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
415 return true;
416 }
Chad Rosier31246272013-04-17 21:01:45 +0000417 // Get the scale and replace the 'Register * Scale' with '0'.
418 IC.popOperator();
419 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000420 PrevState == IES_OR || PrevState == IES_AND ||
Chad Rosier31246272013-04-17 21:01:45 +0000421 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
422 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
423 CurrState == IES_MINUS) {
424 // Unary minus. No need to pop the minus operand because it was never
425 // pushed.
426 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
427 } else {
428 IC.pushOperand(IC_IMM, TmpInt);
429 }
Chad Rosier5362af92013-04-16 18:15:40 +0000430 break;
431 }
Chad Rosier31246272013-04-17 21:01:45 +0000432 PrevState = CurrState;
Kevin Enderby9d117022014-01-23 21:52:41 +0000433 return false;
Chad Rosier5362af92013-04-16 18:15:40 +0000434 }
435 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000436 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000437 switch (State) {
438 default:
439 State = IES_ERROR;
440 break;
441 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000442 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000443 case IES_RPAREN:
444 State = IES_MULTIPLY;
445 IC.pushOperator(IC_MULTIPLY);
446 break;
447 }
448 }
449 void onDivide() {
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_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000456 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000457 State = IES_DIVIDE;
458 IC.pushOperator(IC_DIVIDE);
459 break;
460 }
461 }
462 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000463 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000464 switch (State) {
465 default:
466 State = IES_ERROR;
467 break;
468 case IES_RBRAC:
469 State = IES_PLUS;
470 IC.pushOperator(IC_PLUS);
471 break;
472 }
473 }
474 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000475 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000476 switch (State) {
477 default:
478 State = IES_ERROR;
479 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000480 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000481 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000482 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000483 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000484 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
485 // If we already have a BaseReg, then assume this is the IndexReg with
486 // a scale of 1.
487 if (!BaseReg) {
488 BaseReg = TmpReg;
489 } else {
490 assert (!IndexReg && "BaseReg/IndexReg already set!");
491 IndexReg = TmpReg;
492 Scale = 1;
493 }
Chad Rosier5362af92013-04-16 18:15:40 +0000494 }
495 break;
496 }
Chad Rosier31246272013-04-17 21:01:45 +0000497 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000498 }
499 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000500 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000501 switch (State) {
502 default:
503 State = IES_ERROR;
504 break;
505 case IES_PLUS:
506 case IES_MINUS:
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000507 case IES_OR:
508 case IES_AND:
Chad Rosier5362af92013-04-16 18:15:40 +0000509 case IES_MULTIPLY:
510 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000511 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000512 // FIXME: We don't handle this type of unary minus, yet.
513 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
Kevin Enderby2e13b1c2014-01-15 19:05:24 +0000514 PrevState == IES_OR || PrevState == IES_AND ||
Chad Rosierdb003992013-04-18 16:28:19 +0000515 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
516 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
517 CurrState == IES_MINUS) {
518 State = IES_ERROR;
519 break;
520 }
Chad Rosier5362af92013-04-16 18:15:40 +0000521 State = IES_LPAREN;
522 IC.pushOperator(IC_LPAREN);
523 break;
524 }
Chad Rosier31246272013-04-17 21:01:45 +0000525 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000526 }
527 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000528 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000529 switch (State) {
530 default:
531 State = IES_ERROR;
532 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000533 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000534 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000535 case IES_RPAREN:
536 State = IES_RPAREN;
537 IC.pushOperator(IC_RPAREN);
538 break;
539 }
540 }
541 };
542
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000543 MCAsmParser &getParser() const { return Parser; }
544
545 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
546
Chris Lattnera3a06812011-10-16 04:47:35 +0000547 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000548 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000549 bool MatchingInlineAsm = false) {
550 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000551 return Parser.Error(L, Msg, Ranges);
552 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000553
Devang Patel41b9dde2012-01-17 18:00:18 +0000554 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
555 Error(Loc, Msg);
556 return 0;
557 }
558
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000559 X86Operand *DefaultMemSIOperand(SMLoc Loc);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000560 X86Operand *DefaultMemDIOperand(SMLoc Loc);
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000561 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000562 X86Operand *ParseATTOperand();
563 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000564 X86Operand *ParseIntelOffsetOfOperator();
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000565 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000566 X86Operand *ParseIntelOperator(unsigned OpKind);
David Majnemeraa34d792013-08-27 21:56:17 +0000567 X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
568 X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
569 unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000570 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000571 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000572 int64_t ImmDisp, unsigned Size);
Benjamin Kramer951b15e2013-12-01 11:47:42 +0000573 bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
574 InlineAsmIdentifierInfo &Info,
575 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000576
Chris Lattnerb9270732010-04-17 18:56:34 +0000577 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000578
Chad Rosier175d0ae2013-04-12 18:21:18 +0000579 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
580 unsigned BaseReg, unsigned IndexReg,
581 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000582 unsigned Size, StringRef Identifier,
583 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000584
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000585 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000586 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000587
Devang Patelde47cce2012-01-18 22:42:29 +0000588 bool processInstruction(MCInst &Inst,
589 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
590
Chad Rosier49963552012-10-13 00:26:04 +0000591 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000592 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000593 MCStreamer &Out, unsigned &ErrorInfo,
594 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000595
David Woodhouse9bbf7ca2014-01-22 15:08:36 +0000596 /// doSrcDstMatch - Returns true if operands are matching in their
597 /// word size (%si and %di, %esi and %edi, etc.). Order depends on
598 /// the parsing mode (Intel vs. AT&T).
599 bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
600
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000601 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000602 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000603 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000604 }
Craig Topper3c80d622014-01-06 04:55:54 +0000605 bool is32BitMode() const {
606 // FIXME: Can tablegen auto-generate this?
607 return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
608 }
609 bool is16BitMode() const {
610 // FIXME: Can tablegen auto-generate this?
611 return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
612 }
613 void SwitchMode(uint64_t mode) {
614 uint64_t oldMode = STI.getFeatureBits() &
615 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
616 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
Evan Cheng481ebb02011-07-27 00:38:12 +0000617 setAvailableFeatures(FB);
Craig Topper3c80d622014-01-06 04:55:54 +0000618 assert(mode == (STI.getFeatureBits() &
619 (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
Evan Cheng481ebb02011-07-27 00:38:12 +0000620 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000621
Chad Rosierc2f055d2013-04-18 16:13:18 +0000622 bool isParsingIntelSyntax() {
623 return getParser().getAssemblerDialect();
624 }
625
Daniel Dunbareefe8612010-07-19 05:44:09 +0000626 /// @name Auto-generated Matcher Functions
627 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000628
Chris Lattner3e4582a2010-09-06 19:11:01 +0000629#define GET_ASSEMBLER_HEADER
630#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000631
Daniel Dunbar00331992009-07-29 00:02:19 +0000632 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000633
634public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000635 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
636 const MCInstrInfo &MII)
637 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000638
Daniel Dunbareefe8612010-07-19 05:44:09 +0000639 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000640 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000641 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000642 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000643
Chad Rosierf0e87202012-10-25 20:41:34 +0000644 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
645 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000646 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000647
648 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000649};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000650} // end anonymous namespace
651
Sean Callanan86c11812010-01-23 00:40:33 +0000652/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000653/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000654
Chris Lattner60db0a62010-02-09 00:34:28 +0000655static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000656
657/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000658
Craig Topper6bf3ed42012-07-18 04:59:16 +0000659static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000660 return (( Value <= 0x000000000000007FULL)||
661 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
662 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
663}
664
665static bool isImmSExti32i8Value(uint64_t Value) {
666 return (( Value <= 0x000000000000007FULL)||
667 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
668 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
669}
670
671static bool isImmZExtu32u8Value(uint64_t Value) {
672 return (Value <= 0x00000000000000FFULL);
673}
674
675static bool isImmSExti64i8Value(uint64_t Value) {
676 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000677 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000678}
679
680static bool isImmSExti64i32Value(uint64_t Value) {
681 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000682 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000683}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000684namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000685
686/// X86Operand - Instances of this class represent a parsed X86 machine
687/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000688struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000689 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000690 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000691 Register,
692 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000693 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000694 } Kind;
695
Chris Lattner0c2538f2010-01-15 18:51:29 +0000696 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000697 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000698 StringRef SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000699 void *OpDecl;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000700 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000701
Eric Christopher8996c5d2013-03-15 00:42:55 +0000702 struct TokOp {
703 const char *Data;
704 unsigned Length;
705 };
706
707 struct RegOp {
708 unsigned RegNo;
709 };
710
711 struct ImmOp {
712 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000713 };
714
715 struct MemOp {
716 unsigned SegReg;
717 const MCExpr *Disp;
718 unsigned BaseReg;
719 unsigned IndexReg;
720 unsigned Scale;
721 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000722 };
723
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000724 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000725 struct TokOp Tok;
726 struct RegOp Reg;
727 struct ImmOp Imm;
728 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000729 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000730
Chris Lattner015cfb12010-01-15 19:33:43 +0000731 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000732 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000733
Chad Rosiere81309b2013-04-09 17:53:49 +0000734 StringRef getSymName() { return SymName; }
Chad Rosier732b8372013-04-22 22:04:25 +0000735 void *getOpDecl() { return OpDecl; }
Chad Rosiere81309b2013-04-09 17:53:49 +0000736
Chris Lattner86e61532010-01-15 19:06:59 +0000737 /// getStartLoc - Get the location of the first token of this operand.
738 SMLoc getStartLoc() const { return StartLoc; }
739 /// getEndLoc - Get the location of the last token of this operand.
740 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000741 /// getLocRange - Get the range between the first and last token of this
742 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000743 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000744 /// getOffsetOfLoc - Get the location of the offset operator.
745 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000746
Jim Grosbach602aa902011-07-13 15:34:57 +0000747 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000748
Daniel Dunbare10787e2009-08-07 08:26:05 +0000749 StringRef getToken() const {
750 assert(Kind == Token && "Invalid access!");
751 return StringRef(Tok.Data, Tok.Length);
752 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000753 void setTokenValue(StringRef Value) {
754 assert(Kind == Token && "Invalid access!");
755 Tok.Data = Value.data();
756 Tok.Length = Value.size();
757 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000758
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000759 unsigned getReg() const {
760 assert(Kind == Register && "Invalid access!");
761 return Reg.RegNo;
762 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000763
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000764 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000765 assert(Kind == Immediate && "Invalid access!");
766 return Imm.Val;
767 }
768
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000769 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000770 assert(Kind == Memory && "Invalid access!");
771 return Mem.Disp;
772 }
773 unsigned getMemSegReg() const {
774 assert(Kind == Memory && "Invalid access!");
775 return Mem.SegReg;
776 }
777 unsigned getMemBaseReg() const {
778 assert(Kind == Memory && "Invalid access!");
779 return Mem.BaseReg;
780 }
781 unsigned getMemIndexReg() const {
782 assert(Kind == Memory && "Invalid access!");
783 return Mem.IndexReg;
784 }
785 unsigned getMemScale() const {
786 assert(Kind == Memory && "Invalid access!");
787 return Mem.Scale;
788 }
789
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000790 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000791
792 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000793
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000794 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000795 if (!isImm())
796 return false;
797
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000798 // If this isn't a constant expr, just assume it fits and let relaxation
799 // handle it.
800 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
801 if (!CE)
802 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000803
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000804 // Otherwise, check the value is in a range that makes sense for this
805 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000806 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000807 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000808 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000809 if (!isImm())
810 return false;
811
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000812 // If this isn't a constant expr, just assume it fits and let relaxation
813 // handle it.
814 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
815 if (!CE)
816 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000817
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000818 // Otherwise, check the value is in a range that makes sense for this
819 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000820 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000821 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000822 bool isImmZExtu32u8() const {
823 if (!isImm())
824 return false;
825
826 // If this isn't a constant expr, just assume it fits and let relaxation
827 // handle it.
828 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
829 if (!CE)
830 return true;
831
832 // Otherwise, check the value is in a range that makes sense for this
833 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000834 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000835 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000836 bool isImmSExti64i8() const {
837 if (!isImm())
838 return false;
839
840 // If this isn't a constant expr, just assume it fits and let relaxation
841 // handle it.
842 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
843 if (!CE)
844 return true;
845
846 // Otherwise, check the value is in a range that makes sense for this
847 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000848 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000849 }
850 bool isImmSExti64i32() const {
851 if (!isImm())
852 return false;
853
854 // If this isn't a constant expr, just assume it fits and let relaxation
855 // handle it.
856 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
857 if (!CE)
858 return true;
859
860 // Otherwise, check the value is in a range that makes sense for this
861 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000862 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000863 }
864
Chad Rosier5bca3f92012-10-22 19:50:35 +0000865 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000866 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000867 }
868
Chad Rosiera4bc9432013-01-10 22:10:27 +0000869 bool needAddressOf() const {
870 return AddressOf;
871 }
872
Daniel Dunbare10787e2009-08-07 08:26:05 +0000873 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000874 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000875 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000876 }
Chad Rosier51afe632012-06-27 22:34:28 +0000877 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000878 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000879 }
Chad Rosier51afe632012-06-27 22:34:28 +0000880 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000881 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000882 }
Chad Rosier51afe632012-06-27 22:34:28 +0000883 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000884 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000885 }
Chad Rosier51afe632012-06-27 22:34:28 +0000886 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000887 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000888 }
Chad Rosier51afe632012-06-27 22:34:28 +0000889 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000890 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000891 }
Chad Rosier51afe632012-06-27 22:34:28 +0000892 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000893 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000894 }
Craig Topper8c26c422013-08-25 23:18:05 +0000895 bool isMem512() const {
896 return Kind == Memory && (!Mem.Size || Mem.Size == 512);
897 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000898
Craig Topper01deb5f2012-07-18 04:11:12 +0000899 bool isMemVX32() const {
900 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
901 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
902 }
903 bool isMemVY32() const {
904 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
905 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
906 }
907 bool isMemVX64() const {
908 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
909 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
910 }
911 bool isMemVY64() const {
912 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
913 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
914 }
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000915 bool isMemVZ32() const {
916 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
917 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
918 }
919 bool isMemVZ64() const {
920 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
921 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
922 }
923
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000924 bool isAbsMem() const {
925 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000926 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000927 }
928
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000929 bool isSrcIdx() const {
930 return !getMemIndexReg() && getMemScale() == 1 &&
931 (getMemBaseReg() == X86::RSI || getMemBaseReg() == X86::ESI ||
932 getMemBaseReg() == X86::SI) && isa<MCConstantExpr>(getMemDisp()) &&
933 cast<MCConstantExpr>(getMemDisp())->getValue() == 0;
934 }
935 bool isSrcIdx8() const {
936 return isMem8() && isSrcIdx();
937 }
938 bool isSrcIdx16() const {
939 return isMem16() && isSrcIdx();
940 }
941 bool isSrcIdx32() const {
942 return isMem32() && isSrcIdx();
943 }
944 bool isSrcIdx64() const {
945 return isMem64() && isSrcIdx();
946 }
947
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000948 bool isDstIdx() const {
949 return !getMemIndexReg() && getMemScale() == 1 &&
950 (getMemSegReg() == 0 || getMemSegReg() == X86::ES) &&
951 (getMemBaseReg() == X86::RDI || getMemBaseReg() == X86::EDI ||
952 getMemBaseReg() == X86::DI) && isa<MCConstantExpr>(getMemDisp()) &&
953 cast<MCConstantExpr>(getMemDisp())->getValue() == 0;
954 }
955 bool isDstIdx8() const {
956 return isMem8() && isDstIdx();
957 }
958 bool isDstIdx16() const {
959 return isMem16() && isDstIdx();
960 }
961 bool isDstIdx32() const {
962 return isMem32() && isDstIdx();
963 }
964 bool isDstIdx64() const {
965 return isMem64() && isDstIdx();
966 }
967
Craig Topper18854172013-08-25 22:23:38 +0000968 bool isMemOffs8() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000969 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000970 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 8);
971 }
972 bool isMemOffs16() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000973 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000974 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 16);
975 }
976 bool isMemOffs32() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000977 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000978 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 32);
979 }
980 bool isMemOffs64() const {
Craig Topper35da3d12014-01-16 07:36:58 +0000981 return Kind == Memory && !getMemBaseReg() &&
Craig Topper18854172013-08-25 22:23:38 +0000982 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 64);
983 }
984
Daniel Dunbare10787e2009-08-07 08:26:05 +0000985 bool isReg() const { return Kind == Register; }
986
Craig Toppera422b092013-10-14 04:55:01 +0000987 bool isGR32orGR64() const {
988 return Kind == Register &&
989 (X86MCRegisterClasses[X86::GR32RegClassID].contains(getReg()) ||
990 X86MCRegisterClasses[X86::GR64RegClassID].contains(getReg()));
991 }
992
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000993 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
994 // Add as immediates when possible.
995 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
996 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
997 else
998 Inst.addOperand(MCOperand::CreateExpr(Expr));
999 }
1000
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +00001001 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +00001002 assert(N == 1 && "Invalid number of operands!");
1003 Inst.addOperand(MCOperand::CreateReg(getReg()));
1004 }
1005
Craig Toppera422b092013-10-14 04:55:01 +00001006 static unsigned getGR32FromGR64(unsigned RegNo) {
1007 switch (RegNo) {
1008 default: llvm_unreachable("Unexpected register");
1009 case X86::RAX: return X86::EAX;
1010 case X86::RCX: return X86::ECX;
1011 case X86::RDX: return X86::EDX;
1012 case X86::RBX: return X86::EBX;
1013 case X86::RBP: return X86::EBP;
1014 case X86::RSP: return X86::ESP;
1015 case X86::RSI: return X86::ESI;
1016 case X86::RDI: return X86::EDI;
1017 case X86::R8: return X86::R8D;
1018 case X86::R9: return X86::R9D;
1019 case X86::R10: return X86::R10D;
1020 case X86::R11: return X86::R11D;
1021 case X86::R12: return X86::R12D;
1022 case X86::R13: return X86::R13D;
1023 case X86::R14: return X86::R14D;
1024 case X86::R15: return X86::R15D;
1025 case X86::RIP: return X86::EIP;
1026 }
1027 }
1028
1029 void addGR32orGR64Operands(MCInst &Inst, unsigned N) const {
1030 assert(N == 1 && "Invalid number of operands!");
1031 unsigned RegNo = getReg();
1032 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo))
1033 RegNo = getGR32FromGR64(RegNo);
1034 Inst.addOperand(MCOperand::CreateReg(RegNo));
1035 }
1036
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +00001037 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +00001038 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +00001039 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +00001040 }
1041
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +00001042 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +00001043 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +00001044 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
1045 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
1046 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +00001047 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +00001048 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
1049 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001050
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001051 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
1052 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001053 // Add as immediates when possible.
1054 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
1055 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1056 else
1057 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001058 }
1059
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001060 void addSrcIdxOperands(MCInst &Inst, unsigned N) const {
1061 assert((N == 2) && "Invalid number of operands!");
1062 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
1063 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
1064 }
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001065 void addDstIdxOperands(MCInst &Inst, unsigned N) const {
1066 assert((N == 1) && "Invalid number of operands!");
1067 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
1068 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001069
Craig Topper18854172013-08-25 22:23:38 +00001070 void addMemOffsOperands(MCInst &Inst, unsigned N) const {
Craig Topper35da3d12014-01-16 07:36:58 +00001071 assert((N == 2) && "Invalid number of operands!");
Craig Topper18854172013-08-25 22:23:38 +00001072 // Add as immediates when possible.
1073 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
1074 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1075 else
1076 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Craig Topper35da3d12014-01-16 07:36:58 +00001077 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
Craig Topper18854172013-08-25 22:23:38 +00001078 }
1079
Chris Lattner528d00b2010-01-15 19:28:38 +00001080 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001081 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +00001082 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001083 Res->Tok.Data = Str.data();
1084 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +00001085 return Res;
1086 }
1087
Chad Rosier91c82662012-10-24 17:22:29 +00001088 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +00001089 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +00001090 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +00001091 StringRef SymName = StringRef(),
1092 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +00001093 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001094 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001095 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +00001096 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +00001097 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +00001098 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001099 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001100 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001101
Chad Rosierf3c04f62013-03-19 21:58:18 +00001102 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +00001103 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001104 Res->Imm.Val = Val;
1105 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001106 }
Daniel Dunbare10787e2009-08-07 08:26:05 +00001107
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001108 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +00001109 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +00001110 unsigned Size = 0, StringRef SymName = StringRef(),
1111 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001112 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
1113 Res->Mem.SegReg = 0;
1114 Res->Mem.Disp = Disp;
1115 Res->Mem.BaseReg = 0;
1116 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +00001117 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +00001118 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001119 Res->SymName = SymName;
1120 Res->OpDecl = OpDecl;
1121 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001122 return Res;
1123 }
1124
1125 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001126 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
1127 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +00001128 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +00001129 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +00001130 StringRef SymName = StringRef(),
1131 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001132 // We should never just have a displacement, that should be parsed as an
1133 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001134 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
1135
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001136 // The scale should always be one of {1,2,4,8}.
1137 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001138 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +00001139 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +00001140 Res->Mem.SegReg = SegReg;
1141 Res->Mem.Disp = Disp;
1142 Res->Mem.BaseReg = BaseReg;
1143 Res->Mem.IndexReg = IndexReg;
1144 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +00001145 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +00001146 Res->SymName = SymName;
1147 Res->OpDecl = OpDecl;
1148 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001149 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001150 }
1151};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00001152
Chris Lattner4eb9df02009-07-29 06:33:53 +00001153} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +00001154
Kevin Enderbybc570f22014-01-23 22:34:42 +00001155static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
1156 StringRef &ErrMsg) {
1157 // If we have both a base register and an index register make sure they are
1158 // both 64-bit or 32-bit registers.
1159 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
1160 if (BaseReg != 0 && IndexReg != 0) {
1161 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
1162 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1163 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
1164 IndexReg != X86::RIZ) {
1165 ErrMsg = "base register is 64-bit, but index register is not";
1166 return true;
1167 }
1168 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
1169 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1170 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
1171 IndexReg != X86::EIZ){
1172 ErrMsg = "base register is 32-bit, but index register is not";
1173 return true;
1174 }
1175 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
1176 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
1177 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
1178 ErrMsg = "base register is 16-bit, but index register is not";
1179 return true;
1180 }
1181 if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
1182 IndexReg != X86::SI && IndexReg != X86::DI) ||
1183 ((BaseReg == X86::SI || BaseReg == X86::DI) &&
1184 IndexReg != X86::BX && IndexReg != X86::BP)) {
1185 ErrMsg = "invalid 16-bit base/index register combination";
1186 return true;
1187 }
1188 }
1189 }
1190 return false;
1191}
1192
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00001193bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
1194{
1195 // Return true and let a normal complaint about bogus operands happen.
1196 if (!Op1.isMem() || !Op2.isMem())
1197 return true;
1198
1199 // Actually these might be the other way round if Intel syntax is
1200 // being used. It doesn't matter.
1201 unsigned diReg = Op1.Mem.BaseReg;
1202 unsigned siReg = Op2.Mem.BaseReg;
1203
1204 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
1205 return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
1206 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
1207 return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
1208 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
1209 return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
1210 // Again, return true and let another error happen.
1211 return true;
1212}
1213
Devang Patel4a6e7782012-01-12 18:03:40 +00001214bool X86AsmParser::ParseRegister(unsigned &RegNo,
1215 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001216 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001217 const AsmToken &PercentTok = Parser.getTok();
1218 StartLoc = PercentTok.getLoc();
1219
1220 // If we encounter a %, ignore it. This code handles registers with and
1221 // without the prefix, unprefixed registers can occur in cfi directives.
1222 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001223 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001224
Sean Callanan936b0d32010-01-19 21:44:56 +00001225 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001226 EndLoc = Tok.getEndLoc();
1227
Devang Patelce6a2ca2012-01-20 22:32:05 +00001228 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001229 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001230 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001231 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001232 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001233
Kevin Enderby7d912182009-09-03 17:15:07 +00001234 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001235
Chris Lattner1261b812010-09-22 04:11:10 +00001236 // If the match failed, try the register name as lowercase.
1237 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001238 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001239
Evan Chengeda1d4f2011-07-27 23:22:03 +00001240 if (!is64BitMode()) {
Eric Christopherc0a5aae2013-12-20 02:04:49 +00001241 // FIXME: This should be done using Requires<Not64BitMode> and
Evan Chengeda1d4f2011-07-27 23:22:03 +00001242 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1243 // checked.
1244 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1245 // REX prefix.
1246 if (RegNo == X86::RIZ ||
1247 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1248 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1249 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001250 return Error(StartLoc, "register %"
1251 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001252 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001253 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001254
Chris Lattner1261b812010-09-22 04:11:10 +00001255 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1256 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001257 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001258 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001259
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001260 // Check to see if we have '(4)' after %st.
1261 if (getLexer().isNot(AsmToken::LParen))
1262 return false;
1263 // Lex the paren.
1264 getParser().Lex();
1265
1266 const AsmToken &IntTok = Parser.getTok();
1267 if (IntTok.isNot(AsmToken::Integer))
1268 return Error(IntTok.getLoc(), "expected stack index");
1269 switch (IntTok.getIntVal()) {
1270 case 0: RegNo = X86::ST0; break;
1271 case 1: RegNo = X86::ST1; break;
1272 case 2: RegNo = X86::ST2; break;
1273 case 3: RegNo = X86::ST3; break;
1274 case 4: RegNo = X86::ST4; break;
1275 case 5: RegNo = X86::ST5; break;
1276 case 6: RegNo = X86::ST6; break;
1277 case 7: RegNo = X86::ST7; break;
1278 default: return Error(IntTok.getLoc(), "invalid stack index");
1279 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001280
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001281 if (getParser().Lex().isNot(AsmToken::RParen))
1282 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001283
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001284 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001285 Parser.Lex(); // Eat ')'
1286 return false;
1287 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001288
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001289 EndLoc = Parser.getTok().getEndLoc();
1290
Chris Lattner80486622010-06-24 07:29:18 +00001291 // If this is "db[0-7]", match it as an alias
1292 // for dr[0-7].
1293 if (RegNo == 0 && Tok.getString().size() == 3 &&
1294 Tok.getString().startswith("db")) {
1295 switch (Tok.getString()[2]) {
1296 case '0': RegNo = X86::DR0; break;
1297 case '1': RegNo = X86::DR1; break;
1298 case '2': RegNo = X86::DR2; break;
1299 case '3': RegNo = X86::DR3; break;
1300 case '4': RegNo = X86::DR4; break;
1301 case '5': RegNo = X86::DR5; break;
1302 case '6': RegNo = X86::DR6; break;
1303 case '7': RegNo = X86::DR7; break;
1304 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001305
Chris Lattner80486622010-06-24 07:29:18 +00001306 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001307 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001308 Parser.Lex(); // Eat it.
1309 return false;
1310 }
1311 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001312
Devang Patelce6a2ca2012-01-20 22:32:05 +00001313 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001314 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001315 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001316 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001317 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001318
Sean Callanana83fd7d2010-01-19 20:27:46 +00001319 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001320 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001321}
1322
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001323X86Operand *X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
1324 unsigned basereg =
1325 is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
1326 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
1327 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
1328 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
1329}
1330
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001331X86Operand *X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
1332 unsigned basereg =
1333 is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
1334 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
1335 return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
1336 /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
1337}
1338
Devang Patel4a6e7782012-01-12 18:03:40 +00001339X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001340 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001341 return ParseIntelOperand();
1342 return ParseATTOperand();
1343}
1344
Devang Patel41b9dde2012-01-17 18:00:18 +00001345/// getIntelMemOperandSize - Return intel memory operand size.
1346static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001347 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001348 .Cases("BYTE", "byte", 8)
1349 .Cases("WORD", "word", 16)
1350 .Cases("DWORD", "dword", 32)
1351 .Cases("QWORD", "qword", 64)
1352 .Cases("XWORD", "xword", 80)
1353 .Cases("XMMWORD", "xmmword", 128)
1354 .Cases("YMMWORD", "ymmword", 256)
Craig Topper9ac290a2014-01-17 07:37:39 +00001355 .Cases("ZMMWORD", "zmmword", 512)
Craig Topper2d4b3c92014-01-17 07:44:10 +00001356 .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
Chad Rosierb6b8e962012-09-11 21:10:25 +00001357 .Default(0);
1358 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001359}
1360
Chad Rosier175d0ae2013-04-12 18:21:18 +00001361X86Operand *
1362X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1363 unsigned BaseReg, unsigned IndexReg,
1364 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001365 unsigned Size, StringRef Identifier,
1366 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001367 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001368 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1369 // reference. We need an 'r' constraint here, so we need to create register
1370 // operand to ensure proper matching. Just pick a GPR based on the size of
1371 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001372 if (!Info.IsVarDecl) {
Craig Topper3c80d622014-01-06 04:55:54 +00001373 unsigned RegNo =
1374 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001375 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001376 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001377 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001378 if (!Size) {
1379 Size = Info.Type * 8; // Size is in terms of bits in this context.
1380 if (Size)
1381 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1382 /*Len=*/0, Size));
1383 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001384 }
1385
Chad Rosier7ca135b2013-03-19 21:11:56 +00001386 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001387 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001388 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001389 BaseReg = BaseReg ? BaseReg : 1;
1390 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001391 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001392}
1393
Chad Rosierd383db52013-04-12 20:20:54 +00001394static void
1395RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1396 StringRef SymName, int64_t ImmDisp,
1397 int64_t FinalImmDisp, SMLoc &BracLoc,
1398 SMLoc &StartInBrac, SMLoc &End) {
1399 // Remove the '[' and ']' from the IR string.
1400 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1401 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1402
1403 // If ImmDisp is non-zero, then we parsed a displacement before the
1404 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1405 // If ImmDisp doesn't match the displacement computed by the state machine
1406 // then we have an additional displacement in the bracketed expression.
1407 if (ImmDisp != FinalImmDisp) {
1408 if (ImmDisp) {
1409 // We have an immediate displacement before the bracketed expression.
1410 // Adjust this to match the final immediate displacement.
1411 bool Found = false;
1412 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1413 E = AsmRewrites->end(); I != E; ++I) {
1414 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1415 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001416 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1417 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001418 (*I).Kind = AOK_Imm;
1419 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1420 (*I).Val = FinalImmDisp;
1421 Found = true;
1422 break;
1423 }
1424 }
1425 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001426 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001427 } else {
1428 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001429 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001430 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001431 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001432 }
1433 }
1434 // Remove all the ImmPrefix rewrites within the brackets.
1435 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1436 E = AsmRewrites->end(); I != E; ++I) {
1437 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1438 continue;
1439 if ((*I).Kind == AOK_ImmPrefix)
1440 (*I).Kind = AOK_Delete;
1441 }
1442 const char *SymLocPtr = SymName.data();
1443 // Skip everything before the symbol.
1444 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1445 assert(Len > 0 && "Expected a non-negative length.");
1446 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1447 }
1448 // Skip everything after the symbol.
1449 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1450 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1451 assert(Len > 0 && "Expected a non-negative length.");
1452 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1453 }
1454}
1455
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001456bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001457 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001458
Chad Rosier5c118fd2013-01-14 22:31:35 +00001459 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001460 while (!Done) {
1461 bool UpdateLocLex = true;
1462
1463 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1464 // identifier. Don't try an parse it as a register.
1465 if (Tok.getString().startswith("."))
1466 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001467
1468 // If we're parsing an immediate expression, we don't expect a '['.
1469 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1470 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001471
1472 switch (getLexer().getKind()) {
1473 default: {
1474 if (SM.isValidEndState()) {
1475 Done = true;
1476 break;
1477 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001478 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001479 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001480 case AsmToken::EndOfStatement: {
1481 Done = true;
1482 break;
1483 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001484 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001485 // This could be a register or a symbolic displacement.
1486 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001487 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001488 SMLoc IdentLoc = Tok.getLoc();
1489 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001490 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001491 SM.onRegister(TmpReg);
1492 UpdateLocLex = false;
1493 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001494 } else {
1495 if (!isParsingInlineAsm()) {
1496 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001497 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier95ce8892013-04-19 18:39:50 +00001498 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001499 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001500 if (ParseIntelIdentifier(Val, Identifier, Info,
1501 /*Unevaluated=*/false, End))
1502 return true;
Chad Rosier95ce8892013-04-19 18:39:50 +00001503 }
1504 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001505 UpdateLocLex = false;
1506 break;
1507 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001508 return Error(Tok.getLoc(), "Unexpected identifier!");
Chad Rosier5c118fd2013-01-14 22:31:35 +00001509 }
Kevin Enderby36eba252013-12-19 23:16:14 +00001510 case AsmToken::Integer: {
Kevin Enderby9d117022014-01-23 21:52:41 +00001511 StringRef ErrMsg;
Chad Rosierbfb70992013-04-17 00:11:46 +00001512 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001513 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1514 Tok.getLoc()));
Kevin Enderby36eba252013-12-19 23:16:14 +00001515 // Look for 'b' or 'f' following an Integer as a directional label
1516 SMLoc Loc = getTok().getLoc();
1517 int64_t IntVal = getTok().getIntVal();
1518 End = consumeToken();
1519 UpdateLocLex = false;
1520 if (getLexer().getKind() == AsmToken::Identifier) {
1521 StringRef IDVal = getTok().getString();
1522 if (IDVal == "f" || IDVal == "b") {
1523 MCSymbol *Sym =
1524 getContext().GetDirectionalLocalSymbol(IntVal,
1525 IDVal == "f" ? 1 : 0);
1526 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1527 const MCExpr *Val =
1528 MCSymbolRefExpr::Create(Sym, Variant, getContext());
1529 if (IDVal == "b" && Sym->isUndefined())
1530 return Error(Loc, "invalid reference to undefined symbol");
1531 StringRef Identifier = Sym->getName();
1532 SM.onIdentifierExpr(Val, Identifier);
1533 End = consumeToken();
1534 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001535 if (SM.onInteger(IntVal, ErrMsg))
1536 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001537 }
1538 } else {
Kevin Enderby9d117022014-01-23 21:52:41 +00001539 if (SM.onInteger(IntVal, ErrMsg))
1540 return Error(Loc, ErrMsg);
Kevin Enderby36eba252013-12-19 23:16:14 +00001541 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001542 break;
Kevin Enderby36eba252013-12-19 23:16:14 +00001543 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001544 case AsmToken::Plus: SM.onPlus(); break;
1545 case AsmToken::Minus: SM.onMinus(); break;
1546 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001547 case AsmToken::Slash: SM.onDivide(); break;
Kevin Enderby2e13b1c2014-01-15 19:05:24 +00001548 case AsmToken::Pipe: SM.onOr(); break;
1549 case AsmToken::Amp: SM.onAnd(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001550 case AsmToken::LBrac: SM.onLBrac(); break;
1551 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001552 case AsmToken::LParen: SM.onLParen(); break;
1553 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001554 }
Chad Rosier31246272013-04-17 21:01:45 +00001555 if (SM.hadError())
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001556 return Error(Tok.getLoc(), "unknown token in expression");
Chad Rosier31246272013-04-17 21:01:45 +00001557
Alp Tokera5b88a52013-12-02 16:06:06 +00001558 if (!Done && UpdateLocLex)
1559 End = consumeToken();
Devang Patel41b9dde2012-01-17 18:00:18 +00001560 }
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001561 return false;
Chad Rosier5362af92013-04-16 18:15:40 +00001562}
1563
1564X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001565 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001566 unsigned Size) {
1567 const AsmToken &Tok = Parser.getTok();
1568 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1569 if (getLexer().isNot(AsmToken::LBrac))
1570 return ErrorOperand(BracLoc, "Expected '[' token!");
1571 Parser.Lex(); // Eat '['
1572
1573 SMLoc StartInBrac = Tok.getLoc();
1574 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1575 // may have already parsed an immediate displacement before the bracketed
1576 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001577 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001578 if (ParseIntelExpression(SM, End))
1579 return 0;
Devang Patel41b9dde2012-01-17 18:00:18 +00001580
Chad Rosier175d0ae2013-04-12 18:21:18 +00001581 const MCExpr *Disp;
1582 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001583 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001584 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001585 if (isParsingInlineAsm())
1586 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001587 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001588 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001589 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001590 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001591 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001592 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001593
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001594 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001595 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001596 const MCExpr *NewDisp;
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001597 if (ParseIntelDotOperator(Disp, NewDisp))
1598 return 0;
Chad Rosier911c1f32012-10-25 17:37:43 +00001599
Chad Rosier70f47592013-04-10 20:07:47 +00001600 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001601 Parser.Lex(); // Eat the field.
1602 Disp = NewDisp;
1603 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001604
Chad Rosier5c118fd2013-01-14 22:31:35 +00001605 int BaseReg = SM.getBaseReg();
1606 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001607 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001608 if (!isParsingInlineAsm()) {
1609 // handle [-42]
1610 if (!BaseReg && !IndexReg) {
1611 if (!SegReg)
1612 return X86Operand::CreateMem(Disp, Start, End, Size);
1613 else
1614 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1615 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00001616 StringRef ErrMsg;
1617 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1618 Error(StartInBrac, ErrMsg);
1619 return 0;
1620 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001621 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1622 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001623 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001624
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001625 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001626 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001627 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001628}
1629
Chad Rosier8a244662013-04-02 20:02:33 +00001630// Inline assembly may use variable names with namespace alias qualifiers.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001631bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1632 StringRef &Identifier,
1633 InlineAsmIdentifierInfo &Info,
1634 bool IsUnevaluatedOperand, SMLoc &End) {
Chad Rosier95ce8892013-04-19 18:39:50 +00001635 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1636 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001637
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001638 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001639 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001640
Chad Rosier8a244662013-04-02 20:02:33 +00001641 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001642
1643 // Advance the token stream until the end of the current token is
1644 // after the end of what the frontend claimed.
1645 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1646 while (true) {
1647 End = Tok.getEndLoc();
1648 getLexer().Lex();
1649
1650 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1651 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001652 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001653
1654 // Create the symbol reference.
1655 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001656 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1657 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001658 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001659 return false;
Chad Rosier8a244662013-04-02 20:02:33 +00001660}
1661
David Majnemeraa34d792013-08-27 21:56:17 +00001662/// \brief Parse intel style segment override.
1663X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1664 SMLoc Start,
1665 unsigned Size) {
1666 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1667 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1668 if (Tok.isNot(AsmToken::Colon))
1669 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1670 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001671
David Majnemeraa34d792013-08-27 21:56:17 +00001672 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001673 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001674 ImmDisp = Tok.getIntVal();
1675 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1676
Chad Rosier1530ba52013-03-27 21:49:56 +00001677 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001678 InstInfo->AsmRewrites->push_back(
1679 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1680
1681 if (getLexer().isNot(AsmToken::LBrac)) {
1682 // An immediate following a 'segment register', 'colon' token sequence can
1683 // be followed by a bracketed expression. If it isn't we know we have our
1684 // final segment override.
1685 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1686 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1687 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1688 Size);
1689 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001690 }
1691
Chad Rosier91c82662012-10-24 17:22:29 +00001692 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001693 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001694
David Majnemeraa34d792013-08-27 21:56:17 +00001695 const MCExpr *Val;
1696 SMLoc End;
1697 if (!isParsingInlineAsm()) {
1698 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001699 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
David Majnemeraa34d792013-08-27 21:56:17 +00001700
1701 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001702 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001703
David Majnemeraa34d792013-08-27 21:56:17 +00001704 InlineAsmIdentifierInfo Info;
1705 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001706 if (ParseIntelIdentifier(Val, Identifier, Info,
1707 /*Unevaluated=*/false, End))
1708 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001709 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1710 /*Scale=*/1, Start, End, Size, Identifier, Info);
1711}
1712
1713/// ParseIntelMemOperand - Parse intel style memory operand.
1714X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1715 unsigned Size) {
1716 const AsmToken &Tok = Parser.getTok();
1717 SMLoc End;
1718
1719 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1720 if (getLexer().is(AsmToken::LBrac))
1721 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1722
Chad Rosier95ce8892013-04-19 18:39:50 +00001723 const MCExpr *Val;
1724 if (!isParsingInlineAsm()) {
1725 if (getParser().parsePrimaryExpr(Val, End))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001726 return ErrorOperand(Tok.getLoc(), "unknown token in expression");
Chad Rosier95ce8892013-04-19 18:39:50 +00001727
1728 return X86Operand::CreateMem(Val, Start, End, Size);
1729 }
1730
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001731 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001732 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001733 if (ParseIntelIdentifier(Val, Identifier, Info,
1734 /*Unevaluated=*/false, End))
1735 return 0;
David Majnemeraa34d792013-08-27 21:56:17 +00001736 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001737 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001738}
1739
Chad Rosier5dcb4662012-10-24 22:21:50 +00001740/// Parse the '.' operator.
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001741bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
Chad Rosiercc541e82013-04-19 15:57:00 +00001742 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001743 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001744 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001745
1746 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001747 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001748 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001749 else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001750 return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001751
1752 // Drop the '.'.
1753 StringRef DotDispStr = Tok.getString().drop_front(1);
1754
Chad Rosier5dcb4662012-10-24 22:21:50 +00001755 // .Imm gets lexed as a real.
1756 if (Tok.is(AsmToken::Real)) {
1757 APInt DotDisp;
1758 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001759 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001760 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001761 unsigned DotDisp;
1762 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1763 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001764 DotDisp))
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001765 return Error(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001766 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001767 } else
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001768 return Error(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001769
Chad Rosier240b7b92012-10-25 21:51:10 +00001770 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1771 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1772 unsigned Len = DotDispStr.size();
1773 unsigned Val = OrigDispVal + DotDispVal;
1774 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1775 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001776 }
1777
Chad Rosiercc541e82013-04-19 15:57:00 +00001778 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001779 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001780}
1781
Chad Rosier91c82662012-10-24 17:22:29 +00001782/// Parse the 'offset' operator. This operator is used to specify the
1783/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001784X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001785 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001786 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001787 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001788
Chad Rosier91c82662012-10-24 17:22:29 +00001789 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001790 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001791 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001792 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001793 if (ParseIntelIdentifier(Val, Identifier, Info,
1794 /*Unevaluated=*/false, End))
1795 return 0;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001796
Chad Rosiere2f03772012-10-26 16:09:20 +00001797 // Don't emit the offset operator.
1798 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1799
Chad Rosier91c82662012-10-24 17:22:29 +00001800 // The offset operator will have an 'r' constraint, thus we need to create
1801 // register operand to ensure proper matching. Just pick a GPR based on
1802 // the size of a pointer.
Craig Topper3c80d622014-01-06 04:55:54 +00001803 unsigned RegNo =
1804 is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001805 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001806 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001807}
1808
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001809enum IntelOperatorKind {
1810 IOK_LENGTH,
1811 IOK_SIZE,
1812 IOK_TYPE
1813};
1814
1815/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1816/// returns the number of elements in an array. It returns the value 1 for
1817/// non-array variables. The SIZE operator returns the size of a C or C++
1818/// variable. A variable's size is the product of its LENGTH and TYPE. The
1819/// TYPE operator returns the size of a C or C++ type or variable. If the
1820/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001821X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001822 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001823 SMLoc TypeLoc = Tok.getLoc();
1824 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001825
Chad Rosier95ce8892013-04-19 18:39:50 +00001826 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001827 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001828 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001829 StringRef Identifier = Tok.getString();
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001830 if (ParseIntelIdentifier(Val, Identifier, Info,
1831 /*Unevaluated=*/true, End))
1832 return 0;
1833
1834 if (!Info.OpDecl)
1835 return ErrorOperand(Start, "unable to lookup expression");
Chad Rosier11c42f22012-10-26 18:04:20 +00001836
Chad Rosierf6675c32013-04-22 17:01:46 +00001837 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001838 switch(OpKind) {
1839 default: llvm_unreachable("Unexpected operand kind!");
1840 case IOK_LENGTH: CVal = Info.Length; break;
1841 case IOK_SIZE: CVal = Info.Size; break;
1842 case IOK_TYPE: CVal = Info.Type; break;
1843 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001844
1845 // Rewrite the type operator and the C or C++ type or variable in terms of an
1846 // immediate. E.g. TYPE foo -> $$4
1847 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001848 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001849
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001850 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001851 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001852}
1853
Devang Patel41b9dde2012-01-17 18:00:18 +00001854X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001855 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001856 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001857
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001858 // Offset, length, type and size operators.
1859 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001860 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001861 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001862 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001863 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001864 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001865 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001866 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001867 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001868 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001869 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001870
David Majnemeraa34d792013-08-27 21:56:17 +00001871 unsigned Size = getIntelMemOperandSize(Tok.getString());
1872 if (Size) {
1873 Parser.Lex(); // Eat operand size (e.g., byte, word).
1874 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1875 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1876 Parser.Lex(); // Eat ptr.
1877 }
1878 Start = Tok.getLoc();
1879
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001880 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001881 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1882 getLexer().is(AsmToken::LParen)) {
1883 AsmToken StartTok = Tok;
1884 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1885 /*AddImmPrefix=*/false);
Benjamin Kramer951b15e2013-12-01 11:47:42 +00001886 if (ParseIntelExpression(SM, End))
1887 return 0;
Chad Rosierbfb70992013-04-17 00:11:46 +00001888
1889 int64_t Imm = SM.getImm();
1890 if (isParsingInlineAsm()) {
1891 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1892 if (StartTok.getString().size() == Len)
1893 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001894 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001895 else
1896 // Otherwise, rewrite the complex expression as a single immediate.
1897 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001898 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001899
1900 if (getLexer().isNot(AsmToken::LBrac)) {
Kevin Enderby36eba252013-12-19 23:16:14 +00001901 // If a directional label (ie. 1f or 2b) was parsed above from
1902 // ParseIntelExpression() then SM.getSym() was set to a pointer to
1903 // to the MCExpr with the directional local symbol and this is a
1904 // memory operand not an immediate operand.
1905 if (SM.getSym())
1906 return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1907
Chad Rosierbfb70992013-04-17 00:11:46 +00001908 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1909 return X86Operand::CreateImm(ImmExpr, Start, End);
1910 }
1911
1912 // Only positive immediates are valid.
1913 if (Imm < 0)
1914 return ErrorOperand(Start, "expected a positive immediate displacement "
1915 "before bracketed expr.");
1916
1917 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001918 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001919 }
1920
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001921 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001922 unsigned RegNo = 0;
1923 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001924 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001925 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001926 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001927 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001928
David Majnemeraa34d792013-08-27 21:56:17 +00001929 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001930 }
1931
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001932 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001933 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001934}
1935
Devang Patel4a6e7782012-01-12 18:03:40 +00001936X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001937 switch (getLexer().getKind()) {
1938 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001939 // Parse a memory operand with no segment register.
1940 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001941 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001942 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001943 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001944 SMLoc Start, End;
1945 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001946 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001947 Error(Start, "%eiz and %riz can only be used as index registers",
1948 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001949 return 0;
1950 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001951
Chris Lattnerb9270732010-04-17 18:56:34 +00001952 // If this is a segment register followed by a ':', then this is the start
1953 // of a memory reference, otherwise this is a normal register reference.
1954 if (getLexer().isNot(AsmToken::Colon))
1955 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001956
Chris Lattnerb9270732010-04-17 18:56:34 +00001957 getParser().Lex(); // Eat the colon.
1958 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001959 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001960 case AsmToken::Dollar: {
1961 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001962 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001963 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001964 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001965 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001966 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001967 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001968 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001969 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001970}
1971
Chris Lattnerb9270732010-04-17 18:56:34 +00001972/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1973/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001974X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001975
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001976 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1977 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001978 // only way to do this without lookahead is to eat the '(' and see what is
1979 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001980 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001981 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001982 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001983 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001984
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001985 // After parsing the base expression we could either have a parenthesized
1986 // memory address or not. If not, return now. If so, eat the (.
1987 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001988 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001989 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001990 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001991 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001992 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001993
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001994 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001995 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001996 } else {
1997 // Okay, we have a '('. We don't know if this is an expression or not, but
1998 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001999 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002000 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002001
Kevin Enderby7d912182009-09-03 17:15:07 +00002002 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002003 // Nothing to do here, fall into the code below with the '(' part of the
2004 // memory operand consumed.
2005 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00002006 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002007
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002008 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002009 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002010 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002011
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002012 // After parsing the base expression we could either have a parenthesized
2013 // memory address or not. If not, return now. If so, eat the (.
2014 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00002015 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002016 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00002017 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00002018 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002019 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002020
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002021 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00002022 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002023 }
2024 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002025
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002026 // If we reached here, then we just ate the ( of the memory operand. Process
2027 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00002028 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
David Woodhouse6dbda442014-01-08 12:58:28 +00002029 SMLoc IndexLoc, BaseLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002030
Chris Lattner0c2538f2010-01-15 18:51:29 +00002031 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002032 SMLoc StartLoc, EndLoc;
David Woodhouse6dbda442014-01-08 12:58:28 +00002033 BaseLoc = Parser.getTok().getLoc();
Benjamin Kramer1930b002011-10-16 12:10:27 +00002034 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002035 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00002036 Error(StartLoc, "eiz and riz can only be used as index registers",
2037 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002038 return 0;
2039 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00002040 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002041
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002042 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002043 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002044 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002045
2046 // Following the comma we should have either an index register, or a scale
2047 // value. We don't support the later form, but we want to parse it
2048 // correctly.
2049 //
2050 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00002051 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00002052 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00002053 SMLoc L;
2054 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002055
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002056 if (getLexer().isNot(AsmToken::RParen)) {
2057 // Parse the scale amount:
2058 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002059 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002060 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002061 "expected comma in scale expression");
2062 return 0;
2063 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00002064 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002065
2066 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002067 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002068
2069 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002070 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00002071 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002072 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00002073 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002074
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002075 // Validate the scale amount.
David Woodhouse6dbda442014-01-08 12:58:28 +00002076 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
2077 ScaleVal != 1) {
2078 Error(Loc, "scale factor in 16-bit address must be 1");
2079 return 0;
2080 }
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002081 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
2082 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
2083 return 0;
2084 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002085 Scale = (unsigned)ScaleVal;
2086 }
2087 }
2088 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002089 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002090 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00002091 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002092
2093 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002094 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002095 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002096
Daniel Dunbar94b84a12010-08-24 19:13:38 +00002097 if (Value != 1)
2098 Warning(Loc, "scale factor without index register is ignored");
2099 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002100 }
2101 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002102
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002103 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002104 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00002105 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002106 return 0;
2107 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002108 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00002109 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00002110
David Woodhouse6dbda442014-01-08 12:58:28 +00002111 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
2112 // and then only in non-64-bit modes. Except for DX, which is a special case
2113 // because an unofficial form of in/out instructions uses it.
2114 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
2115 (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
2116 BaseReg != X86::SI && BaseReg != X86::DI)) &&
2117 BaseReg != X86::DX) {
2118 Error(BaseLoc, "invalid 16-bit base register");
2119 return 0;
2120 }
2121 if (BaseReg == 0 &&
2122 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
2123 Error(IndexLoc, "16-bit memory operand may not include only index register");
2124 return 0;
2125 }
Kevin Enderbybc570f22014-01-23 22:34:42 +00002126
2127 StringRef ErrMsg;
2128 if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
2129 Error(BaseLoc, ErrMsg);
2130 return 0;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00002131 }
2132
Chris Lattner015cfb12010-01-15 19:33:43 +00002133 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
2134 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002135}
2136
Devang Patel4a6e7782012-01-12 18:03:40 +00002137bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00002138ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002139 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00002140 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00002141 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002142
Chris Lattner7e8a99b2010-11-28 20:23:50 +00002143 // FIXME: Hack to recognize setneb as setne.
2144 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
2145 PatchedName != "setb" && PatchedName != "setnb")
2146 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00002147
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002148 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
2149 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002150 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002151 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
2152 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00002153 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002154 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002155 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002156 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00002157 .Case("eq", 0x00)
2158 .Case("lt", 0x01)
2159 .Case("le", 0x02)
2160 .Case("unord", 0x03)
2161 .Case("neq", 0x04)
2162 .Case("nlt", 0x05)
2163 .Case("nle", 0x06)
2164 .Case("ord", 0x07)
2165 /* AVX only from here */
2166 .Case("eq_uq", 0x08)
2167 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00002168 .Case("ngt", 0x0A)
2169 .Case("false", 0x0B)
2170 .Case("neq_oq", 0x0C)
2171 .Case("ge", 0x0D)
2172 .Case("gt", 0x0E)
2173 .Case("true", 0x0F)
2174 .Case("eq_os", 0x10)
2175 .Case("lt_oq", 0x11)
2176 .Case("le_oq", 0x12)
2177 .Case("unord_s", 0x13)
2178 .Case("neq_us", 0x14)
2179 .Case("nlt_uq", 0x15)
2180 .Case("nle_uq", 0x16)
2181 .Case("ord_s", 0x17)
2182 .Case("eq_us", 0x18)
2183 .Case("nge_uq", 0x19)
2184 .Case("ngt_uq", 0x1A)
2185 .Case("false_os", 0x1B)
2186 .Case("neq_os", 0x1C)
2187 .Case("ge_oq", 0x1D)
2188 .Case("gt_oq", 0x1E)
2189 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002190 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00002191 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002192 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
2193 getParser().getContext());
2194 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002195 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002196 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002197 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002198 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002199 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002200 } else {
2201 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00002202 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002203 }
2204 }
2205 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00002206
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00002207 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002208
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002209 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002210 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00002211
Chris Lattner086a83a2010-09-08 05:17:37 +00002212 // Determine whether this is an instruction prefix.
2213 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00002214 Name == "lock" || Name == "rep" ||
2215 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00002216 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00002217 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00002218
2219
Chris Lattner086a83a2010-09-08 05:17:37 +00002220 // This does the actual operand parsing. Don't parse any more if we have a
2221 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
2222 // just want to parse the "lock" as the first instruction and the "incl" as
2223 // the next one.
2224 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00002225
2226 // Parse '*' modifier.
Alp Tokera5b88a52013-12-02 16:06:06 +00002227 if (getLexer().is(AsmToken::Star))
2228 Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
Daniel Dunbar71527c12009-08-11 05:00:25 +00002229
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002230 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002231 if (X86Operand *Op = ParseOperand())
2232 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002233 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002234 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002235 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002236 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00002237
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002238 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00002239 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002240
2241 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00002242 if (X86Operand *Op = ParseOperand())
2243 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00002244 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002245 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002246 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00002247 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002248 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002249
Elena Demikhovsky89529742013-09-12 08:55:00 +00002250 if (STI.getFeatureBits() & X86::FeatureAVX512) {
2251 // Parse mask register {%k1}
2252 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002253 Operands.push_back(X86Operand::CreateToken("{", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002254 if (X86Operand *Op = ParseOperand()) {
2255 Operands.push_back(Op);
2256 if (!getLexer().is(AsmToken::RCurly)) {
2257 SMLoc Loc = getLexer().getLoc();
2258 Parser.eatToEndOfStatement();
2259 return Error(Loc, "Expected } at this point");
2260 }
Alp Tokera5b88a52013-12-02 16:06:06 +00002261 Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002262 } else {
2263 Parser.eatToEndOfStatement();
2264 return true;
2265 }
2266 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +00002267 // TODO: add parsing of broadcasts {1to8}, {1to16}
Elena Demikhovsky89529742013-09-12 08:55:00 +00002268 // Parse "zeroing non-masked" semantic {z}
2269 if (getLexer().is(AsmToken::LCurly)) {
Alp Tokera5b88a52013-12-02 16:06:06 +00002270 Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
Elena Demikhovsky89529742013-09-12 08:55:00 +00002271 if (!getLexer().is(AsmToken::Identifier) || getLexer().getTok().getIdentifier() != "z") {
2272 SMLoc Loc = getLexer().getLoc();
2273 Parser.eatToEndOfStatement();
2274 return Error(Loc, "Expected z at this point");
2275 }
2276 Parser.Lex(); // Eat the z
2277 if (!getLexer().is(AsmToken::RCurly)) {
2278 SMLoc Loc = getLexer().getLoc();
2279 Parser.eatToEndOfStatement();
2280 return Error(Loc, "Expected } at this point");
2281 }
2282 Parser.Lex(); // Eat the }
2283 }
2284 }
2285
Chris Lattnera2a9d162010-09-11 16:18:25 +00002286 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00002287 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002288 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00002289 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00002290 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002291 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002292
Chris Lattner086a83a2010-09-08 05:17:37 +00002293 if (getLexer().is(AsmToken::EndOfStatement))
2294 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00002295 else if (isPrefix && getLexer().is(AsmToken::Slash))
2296 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00002297
Devang Patel7cdb2ff2012-01-30 22:47:12 +00002298 if (ExtraImmOp && isParsingIntelSyntax())
2299 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
2300
Chris Lattnerb6f8e822010-11-06 19:25:43 +00002301 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
2302 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
2303 // documented form in various unofficial manuals, so a lot of code uses it.
2304 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2305 Operands.size() == 3) {
2306 X86Operand &Op = *(X86Operand*)Operands.back();
2307 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2308 isa<MCConstantExpr>(Op.Mem.Disp) &&
2309 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2310 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2311 SMLoc Loc = Op.getEndLoc();
2312 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2313 delete &Op;
2314 }
2315 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002316 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2317 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2318 Operands.size() == 3) {
2319 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2320 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2321 isa<MCConstantExpr>(Op.Mem.Disp) &&
2322 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2323 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2324 SMLoc Loc = Op.getEndLoc();
2325 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2326 delete &Op;
2327 }
2328 }
David Woodhouse4ce66062014-01-22 15:08:55 +00002329
2330 // Append default arguments to "ins[bwld]"
2331 if (Name.startswith("ins") && Operands.size() == 1 &&
2332 (Name == "insb" || Name == "insw" || Name == "insl" ||
2333 Name == "insd" )) {
2334 if (isParsingIntelSyntax()) {
2335 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2336 Operands.push_back(DefaultMemDIOperand(NameLoc));
2337 } else {
2338 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2339 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002340 }
2341 }
2342
David Woodhousec472b812014-01-22 15:08:49 +00002343 // Append default arguments to "outs[bwld]"
2344 if (Name.startswith("outs") && Operands.size() == 1 &&
2345 (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
2346 Name == "outsd" )) {
2347 if (isParsingIntelSyntax()) {
2348 Operands.push_back(DefaultMemSIOperand(NameLoc));
2349 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2350 } else {
2351 Operands.push_back(DefaultMemSIOperand(NameLoc));
2352 Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002353 }
2354 }
2355
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002356 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2357 // values of $SIREG according to the mode. It would be nice if this
2358 // could be achieved with InstAlias in the tables.
2359 if (Name.startswith("lods") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002360 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00002361 Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
2362 Operands.push_back(DefaultMemSIOperand(NameLoc));
2363
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002364 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2365 // values of $DIREG according to the mode. It would be nice if this
2366 // could be achieved with InstAlias in the tables.
2367 if (Name.startswith("stos") && Operands.size() == 1 &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002368 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
David Woodhouseb33c2ef2014-01-22 15:08:21 +00002369 Name == "stosl" || Name == "stosd" || Name == "stosq"))
2370 Operands.push_back(DefaultMemDIOperand(NameLoc));
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002371
David Woodhouse20fe4802014-01-22 15:08:27 +00002372 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2373 // values of $DIREG according to the mode. It would be nice if this
2374 // could be achieved with InstAlias in the tables.
2375 if (Name.startswith("scas") && Operands.size() == 1 &&
2376 (Name == "scas" || Name == "scasb" || Name == "scasw" ||
2377 Name == "scasl" || Name == "scasd" || Name == "scasq"))
2378 Operands.push_back(DefaultMemDIOperand(NameLoc));
2379
David Woodhouse9bbf7ca2014-01-22 15:08:36 +00002380 // Add default SI and DI operands to "cmps[bwlq]".
2381 if (Name.startswith("cmps") &&
2382 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2383 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
2384 if (Operands.size() == 1) {
2385 if (isParsingIntelSyntax()) {
2386 Operands.push_back(DefaultMemSIOperand(NameLoc));
2387 Operands.push_back(DefaultMemDIOperand(NameLoc));
2388 } else {
2389 Operands.push_back(DefaultMemDIOperand(NameLoc));
2390 Operands.push_back(DefaultMemSIOperand(NameLoc));
2391 }
2392 } else if (Operands.size() == 3) {
2393 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2394 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2395 if (!doSrcDstMatch(Op, Op2))
2396 return Error(Op.getStartLoc(),
2397 "mismatching source and destination index registers");
2398 }
2399 }
2400
David Woodhouse6f417de2014-01-22 15:08:42 +00002401 // Add default SI and DI operands to "movs[bwlq]".
2402 if ((Name.startswith("movs") &&
2403 (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2404 Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2405 (Name.startswith("smov") &&
2406 (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2407 Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2408 if (Operands.size() == 1) {
2409 if (Name == "movsd")
2410 Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2411 if (isParsingIntelSyntax()) {
2412 Operands.push_back(DefaultMemDIOperand(NameLoc));
2413 Operands.push_back(DefaultMemSIOperand(NameLoc));
2414 } else {
2415 Operands.push_back(DefaultMemSIOperand(NameLoc));
2416 Operands.push_back(DefaultMemDIOperand(NameLoc));
2417 }
2418 } else if (Operands.size() == 3) {
2419 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2420 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2421 if (!doSrcDstMatch(Op, Op2))
2422 return Error(Op.getStartLoc(),
2423 "mismatching source and destination index registers");
2424 }
2425 }
2426
Chris Lattner4bd21712010-09-15 04:33:27 +00002427 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002428 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002429 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002430 Name.startswith("shl") || Name.startswith("sal") ||
2431 Name.startswith("rcl") || Name.startswith("rcr") ||
2432 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002433 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002434 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002435 // Intel syntax
2436 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2437 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002438 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2439 delete Operands[2];
2440 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002441 }
2442 } else {
2443 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2444 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002445 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2446 delete Operands[1];
2447 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002448 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002449 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002450 }
Chad Rosier51afe632012-06-27 22:34:28 +00002451
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002452 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2453 // instalias with an immediate operand yet.
2454 if (Name == "int" && Operands.size() == 2) {
2455 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2456 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2457 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2458 delete Operands[1];
2459 Operands.erase(Operands.begin() + 1);
2460 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2461 }
2462 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002463
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002464 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002465}
2466
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002467static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2468 bool isCmp) {
2469 MCInst TmpInst;
2470 TmpInst.setOpcode(Opcode);
2471 if (!isCmp)
2472 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2473 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2474 TmpInst.addOperand(Inst.getOperand(0));
2475 Inst = TmpInst;
2476 return true;
2477}
2478
2479static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2480 bool isCmp = false) {
2481 if (!Inst.getOperand(0).isImm() ||
2482 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2483 return false;
2484
2485 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2486}
2487
2488static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2489 bool isCmp = false) {
2490 if (!Inst.getOperand(0).isImm() ||
2491 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2492 return false;
2493
2494 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2495}
2496
2497static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2498 bool isCmp = false) {
2499 if (!Inst.getOperand(0).isImm() ||
2500 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2501 return false;
2502
2503 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2504}
2505
Devang Patel4a6e7782012-01-12 18:03:40 +00002506bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002507processInstruction(MCInst &Inst,
2508 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2509 switch (Inst.getOpcode()) {
2510 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002511 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2512 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2513 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2514 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2515 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2516 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2517 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2518 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2519 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2520 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2521 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2522 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2523 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2524 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2525 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2526 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2527 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2528 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002529 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2530 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2531 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2532 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2533 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2534 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Craig Toppera0e07352013-10-07 05:42:48 +00002535 case X86::VMOVAPDrr:
2536 case X86::VMOVAPDYrr:
2537 case X86::VMOVAPSrr:
2538 case X86::VMOVAPSYrr:
2539 case X86::VMOVDQArr:
2540 case X86::VMOVDQAYrr:
2541 case X86::VMOVDQUrr:
2542 case X86::VMOVDQUYrr:
2543 case X86::VMOVUPDrr:
2544 case X86::VMOVUPDYrr:
2545 case X86::VMOVUPSrr:
2546 case X86::VMOVUPSYrr: {
2547 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2548 !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2549 return false;
2550
2551 unsigned NewOpc;
2552 switch (Inst.getOpcode()) {
2553 default: llvm_unreachable("Invalid opcode");
2554 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
2555 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2556 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
2557 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2558 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
2559 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2560 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
2561 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2562 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
2563 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2564 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
2565 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2566 }
2567 Inst.setOpcode(NewOpc);
2568 return true;
2569 }
2570 case X86::VMOVSDrr:
2571 case X86::VMOVSSrr: {
2572 if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2573 !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2574 return false;
2575 unsigned NewOpc;
2576 switch (Inst.getOpcode()) {
2577 default: llvm_unreachable("Invalid opcode");
2578 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
2579 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
2580 }
2581 Inst.setOpcode(NewOpc);
2582 return true;
2583 }
Devang Patelde47cce2012-01-18 22:42:29 +00002584 }
Devang Patelde47cce2012-01-18 22:42:29 +00002585}
2586
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002587static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002588bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002589MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002590 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002591 MCStreamer &Out, unsigned &ErrorInfo,
2592 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002593 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002594 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2595 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002596 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002597
Chris Lattnera63292a2010-09-29 01:50:45 +00002598 // First, handle aliases that expand to multiple instructions.
2599 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002600 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002601 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002602 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002603 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002604 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002605 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002606 MCInst Inst;
2607 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002608 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002609 if (!MatchingInlineAsm)
David Woodhousee6c13e42014-01-28 23:12:42 +00002610 Out.EmitInstruction(Inst, STI);
Chris Lattnera63292a2010-09-29 01:50:45 +00002611
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002612 const char *Repl =
2613 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002614 .Case("finit", "fninit")
2615 .Case("fsave", "fnsave")
2616 .Case("fstcw", "fnstcw")
2617 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002618 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002619 .Case("fstsw", "fnstsw")
2620 .Case("fstsww", "fnstsw")
2621 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002622 .Default(0);
2623 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002624 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002625 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002626 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002627
Chris Lattner628fbec2010-09-06 21:54:15 +00002628 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002629 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002630
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002631 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002632 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002633 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002634 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002635 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002636 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002637 // Some instructions need post-processing to, for example, tweak which
2638 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002639 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002640 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002641 while (processInstruction(Inst, Operands))
2642 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002643
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002644 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002645 if (!MatchingInlineAsm)
David Woodhousee6c13e42014-01-28 23:12:42 +00002646 Out.EmitInstruction(Inst, STI);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002647 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002648 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002649 case Match_MissingFeature: {
2650 assert(ErrorInfo && "Unknown missing feature!");
2651 // Special case the error message for the very common case where only
2652 // a single subtarget feature is missing.
2653 std::string Msg = "instruction requires:";
2654 unsigned Mask = 1;
2655 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2656 if (ErrorInfo & Mask) {
2657 Msg += " ";
2658 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2659 }
2660 Mask <<= 1;
2661 }
2662 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2663 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002664 case Match_InvalidOperand:
2665 WasOriginallyInvalidOperand = true;
2666 break;
2667 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002668 break;
2669 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002670
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002671 // FIXME: Ideally, we would only attempt suffix matches for things which are
2672 // valid prefixes, and we could just infer the right unambiguous
2673 // type. However, that requires substantially more matcher support than the
2674 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002675
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002676 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002677 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002678 SmallString<16> Tmp;
2679 Tmp += Base;
2680 Tmp += ' ';
2681 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002682
Chris Lattnerfab94132010-11-06 18:28:02 +00002683 // If this instruction starts with an 'f', then it is a floating point stack
2684 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2685 // 80-bit floating point, which use the suffixes s,l,t respectively.
2686 //
2687 // Otherwise, we assume that this may be an integer instruction, which comes
2688 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2689 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002690
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002691 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002692 Tmp[Base.size()] = Suffixes[0];
2693 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002694 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002695 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002696
Chad Rosier2f480a82012-10-12 22:53:36 +00002697 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002698 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002699 // If this returned as a missing feature failure, remember that.
2700 if (Match1 == Match_MissingFeature)
2701 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002702 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002703 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002704 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002705 // If this returned as a missing feature failure, remember that.
2706 if (Match2 == Match_MissingFeature)
2707 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002708 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002709 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002710 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002711 // If this returned as a missing feature failure, remember that.
2712 if (Match3 == Match_MissingFeature)
2713 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002714 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002715 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002716 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002717 // If this returned as a missing feature failure, remember that.
2718 if (Match4 == Match_MissingFeature)
2719 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002720
2721 // Restore the old token.
2722 Op->setTokenValue(Base);
2723
2724 // If exactly one matched, then we treat that as a successful match (and the
2725 // instruction will already have been filled in correctly, since the failing
2726 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002727 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002728 (Match1 == Match_Success) + (Match2 == Match_Success) +
2729 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002730 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002731 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002732 if (!MatchingInlineAsm)
David Woodhousee6c13e42014-01-28 23:12:42 +00002733 Out.EmitInstruction(Inst, STI);
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002734 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002735 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002736 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002737
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002738 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002739
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002740 // If we had multiple suffix matches, then identify this as an ambiguous
2741 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002742 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002743 char MatchChars[4];
2744 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002745 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2746 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2747 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2748 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002749
2750 SmallString<126> Msg;
2751 raw_svector_ostream OS(Msg);
2752 OS << "ambiguous instructions require an explicit suffix (could be ";
2753 for (unsigned i = 0; i != NumMatches; ++i) {
2754 if (i != 0)
2755 OS << ", ";
2756 if (i + 1 == NumMatches)
2757 OS << "or ";
2758 OS << "'" << Base << MatchChars[i] << "'";
2759 }
2760 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002761 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002762 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002763 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002764
Chris Lattner628fbec2010-09-06 21:54:15 +00002765 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002766
Chris Lattner628fbec2010-09-06 21:54:15 +00002767 // If all of the instructions reported an invalid mnemonic, then the original
2768 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002769 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2770 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002771 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002772 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002773 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002774 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002775 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002776 }
2777
2778 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002779 if (ErrorInfo != ~0U) {
2780 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002781 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002782 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002783
Chad Rosier49963552012-10-13 00:26:04 +00002784 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002785 if (Operand->getStartLoc().isValid()) {
2786 SMRange OperandRange = Operand->getLocRange();
2787 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002788 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002789 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002790 }
2791
Chad Rosier3d4bc622012-08-21 19:36:59 +00002792 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002793 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002794 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002795
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002796 // If one instruction matched with a missing feature, report this as a
2797 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002798 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2799 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002800 std::string Msg = "instruction requires:";
2801 unsigned Mask = 1;
2802 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2803 if (ErrorInfoMissingFeature & Mask) {
2804 Msg += " ";
2805 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2806 }
2807 Mask <<= 1;
2808 }
2809 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002810 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002811
Chris Lattner628fbec2010-09-06 21:54:15 +00002812 // If one instruction matched with an invalid operand, report this as an
2813 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002814 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2815 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002816 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002817 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002818 return true;
2819 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002820
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002821 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002822 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002823 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002824 return true;
2825}
2826
2827
Devang Patel4a6e7782012-01-12 18:03:40 +00002828bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002829 StringRef IDVal = DirectiveID.getIdentifier();
2830 if (IDVal == ".word")
2831 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002832 else if (IDVal.startswith(".code"))
2833 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002834 else if (IDVal.startswith(".att_syntax")) {
2835 getParser().setAssemblerDialect(0);
2836 return false;
2837 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002838 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002839 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002840 // FIXME: Handle noprefix
2841 if (Parser.getTok().getString() == "noprefix")
Craig Topper6bf3ed42012-07-18 04:59:16 +00002842 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002843 }
2844 return false;
2845 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002846 return true;
2847}
2848
2849/// ParseDirectiveWord
2850/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002851bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002852 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2853 for (;;) {
2854 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002855 if (getParser().parseExpression(Value))
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002856 return false;
Chad Rosier51afe632012-06-27 22:34:28 +00002857
Eric Christopherbf7bc492013-01-09 03:52:05 +00002858 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002859
Chris Lattner72c0b592010-10-30 17:38:55 +00002860 if (getLexer().is(AsmToken::EndOfStatement))
2861 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002862
Chris Lattner72c0b592010-10-30 17:38:55 +00002863 // FIXME: Improve diagnostic.
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002864 if (getLexer().isNot(AsmToken::Comma)) {
2865 Error(L, "unexpected token in directive");
2866 return false;
2867 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002868 Parser.Lex();
2869 }
2870 }
Chad Rosier51afe632012-06-27 22:34:28 +00002871
Chris Lattner72c0b592010-10-30 17:38:55 +00002872 Parser.Lex();
2873 return false;
2874}
2875
Evan Cheng481ebb02011-07-27 00:38:12 +00002876/// ParseDirectiveCode
Craig Topper3c80d622014-01-06 04:55:54 +00002877/// ::= .code16 | .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002878bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Craig Topper3c80d622014-01-06 04:55:54 +00002879 if (IDVal == ".code16") {
Evan Cheng481ebb02011-07-27 00:38:12 +00002880 Parser.Lex();
Craig Topper3c80d622014-01-06 04:55:54 +00002881 if (!is16BitMode()) {
2882 SwitchMode(X86::Mode16Bit);
2883 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2884 }
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002885 } else if (IDVal == ".code32") {
Craig Topper3c80d622014-01-06 04:55:54 +00002886 Parser.Lex();
2887 if (!is32BitMode()) {
2888 SwitchMode(X86::Mode32Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002889 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2890 }
2891 } else if (IDVal == ".code64") {
2892 Parser.Lex();
2893 if (!is64BitMode()) {
Craig Topper3c80d622014-01-06 04:55:54 +00002894 SwitchMode(X86::Mode64Bit);
Evan Cheng481ebb02011-07-27 00:38:12 +00002895 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2896 }
2897 } else {
Saleem Abdulrasoola6505ca2014-01-13 01:15:39 +00002898 Error(L, "unknown directive " + IDVal);
2899 return false;
Evan Cheng481ebb02011-07-27 00:38:12 +00002900 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002901
Evan Cheng481ebb02011-07-27 00:38:12 +00002902 return false;
2903}
Chris Lattner72c0b592010-10-30 17:38:55 +00002904
Daniel Dunbar71475772009-07-17 20:42:00 +00002905// Force static initialization.
2906extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002907 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2908 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002909}
Daniel Dunbar00331992009-07-29 00:02:19 +00002910
Chris Lattner3e4582a2010-09-06 19:11:01 +00002911#define GET_REGISTER_MATCHER
2912#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002913#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002914#include "X86GenAsmMatcher.inc"