blob: 7eb42c4fa58ad2d2726dec4b06f6c007bece6b94 [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[] = {
38 0, // IC_PLUS
39 0, // IC_MINUS
40 1, // IC_MULTIPLY
41 1, // IC_DIVIDE
42 2, // IC_RPAREN
43 3, // IC_LPAREN
44 0, // IC_IMM
45 0 // IC_REGISTER
46};
47
Devang Patel4a6e7782012-01-12 18:03:40 +000048class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000049 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000050 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000051 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000052private:
Chad Rosier5362af92013-04-16 18:15:40 +000053 enum InfixCalculatorTok {
54 IC_PLUS = 0,
55 IC_MINUS,
56 IC_MULTIPLY,
57 IC_DIVIDE,
58 IC_RPAREN,
59 IC_LPAREN,
60 IC_IMM,
61 IC_REGISTER
62 };
63
64 class InfixCalculator {
65 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
66 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
67 SmallVector<ICToken, 4> PostfixStack;
68
69 public:
70 int64_t popOperand() {
71 assert (!PostfixStack.empty() && "Poped an empty stack!");
72 ICToken Op = PostfixStack.pop_back_val();
73 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
74 && "Expected and immediate or register!");
75 return Op.second;
76 }
77 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
78 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
79 "Unexpected operand!");
80 PostfixStack.push_back(std::make_pair(Op, Val));
81 }
82
Jakub Staszak9c349222013-08-08 15:48:46 +000083 void popOperator() { InfixOperatorStack.pop_back(); }
Chad Rosier5362af92013-04-16 18:15:40 +000084 void pushOperator(InfixCalculatorTok Op) {
85 // Push the new operator if the stack is empty.
86 if (InfixOperatorStack.empty()) {
87 InfixOperatorStack.push_back(Op);
88 return;
89 }
90
91 // Push the new operator if it has a higher precedence than the operator
92 // on the top of the stack or the operator on the top of the stack is a
93 // left parentheses.
94 unsigned Idx = InfixOperatorStack.size() - 1;
95 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
96 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
97 InfixOperatorStack.push_back(Op);
98 return;
99 }
100
101 // The operator on the top of the stack has higher precedence than the
102 // new operator.
103 unsigned ParenCount = 0;
104 while (1) {
105 // Nothing to process.
106 if (InfixOperatorStack.empty())
107 break;
108
109 Idx = InfixOperatorStack.size() - 1;
110 StackOp = InfixOperatorStack[Idx];
111 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
112 break;
113
114 // If we have an even parentheses count and we see a left parentheses,
115 // then stop processing.
116 if (!ParenCount && StackOp == IC_LPAREN)
117 break;
118
119 if (StackOp == IC_RPAREN) {
120 ++ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000121 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000122 } else if (StackOp == IC_LPAREN) {
123 --ParenCount;
Jakub Staszak9c349222013-08-08 15:48:46 +0000124 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000125 } else {
Jakub Staszak9c349222013-08-08 15:48:46 +0000126 InfixOperatorStack.pop_back();
Chad Rosier5362af92013-04-16 18:15:40 +0000127 PostfixStack.push_back(std::make_pair(StackOp, 0));
128 }
129 }
130 // Push the new operator.
131 InfixOperatorStack.push_back(Op);
132 }
133 int64_t execute() {
134 // Push any remaining operators onto the postfix stack.
135 while (!InfixOperatorStack.empty()) {
136 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
137 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
138 PostfixStack.push_back(std::make_pair(StackOp, 0));
139 }
140
141 if (PostfixStack.empty())
142 return 0;
143
144 SmallVector<ICToken, 16> OperandStack;
145 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
146 ICToken Op = PostfixStack[i];
147 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
148 OperandStack.push_back(Op);
149 } else {
150 assert (OperandStack.size() > 1 && "Too few operands.");
151 int64_t Val;
152 ICToken Op2 = OperandStack.pop_back_val();
153 ICToken Op1 = OperandStack.pop_back_val();
154 switch (Op.first) {
155 default:
156 report_fatal_error("Unexpected operator!");
157 break;
158 case IC_PLUS:
159 Val = Op1.second + Op2.second;
160 OperandStack.push_back(std::make_pair(IC_IMM, Val));
161 break;
162 case IC_MINUS:
163 Val = Op1.second - Op2.second;
164 OperandStack.push_back(std::make_pair(IC_IMM, Val));
165 break;
166 case IC_MULTIPLY:
167 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
168 "Multiply operation with an immediate and a register!");
169 Val = Op1.second * Op2.second;
170 OperandStack.push_back(std::make_pair(IC_IMM, Val));
171 break;
172 case IC_DIVIDE:
173 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
174 "Divide operation with an immediate and a register!");
175 assert (Op2.second != 0 && "Division by zero!");
176 Val = Op1.second / Op2.second;
177 OperandStack.push_back(std::make_pair(IC_IMM, Val));
178 break;
179 }
180 }
181 }
182 assert (OperandStack.size() == 1 && "Expected a single result.");
183 return OperandStack.pop_back_val().second;
184 }
185 };
186
187 enum IntelExprState {
188 IES_PLUS,
189 IES_MINUS,
190 IES_MULTIPLY,
191 IES_DIVIDE,
192 IES_LBRAC,
193 IES_RBRAC,
194 IES_LPAREN,
195 IES_RPAREN,
196 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000197 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000198 IES_IDENTIFIER,
199 IES_ERROR
200 };
201
202 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000203 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000204 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000205 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000206 const MCExpr *Sym;
207 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000208 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000209 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000210 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000211 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000212 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000213 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
214 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000215 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000216
217 unsigned getBaseReg() { return BaseReg; }
218 unsigned getIndexReg() { return IndexReg; }
219 unsigned getScale() { return Scale; }
220 const MCExpr *getSym() { return Sym; }
221 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000222 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosieredb1dc82013-05-09 23:48:53 +0000223 bool isValidEndState() {
224 return State == IES_RBRAC || State == IES_INTEGER;
225 }
Chad Rosierbfb70992013-04-17 00:11:46 +0000226 bool getStopOnLBrac() { return StopOnLBrac; }
227 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000228 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000229
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000230 InlineAsmIdentifierInfo &getIdentifierInfo() {
231 return Info;
232 }
233
Chad Rosier5362af92013-04-16 18:15:40 +0000234 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000235 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000236 switch (State) {
237 default:
238 State = IES_ERROR;
239 break;
240 case IES_INTEGER:
241 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000242 case IES_REGISTER:
243 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000244 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000245 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
246 // If we already have a BaseReg, then assume this is the IndexReg with
247 // a scale of 1.
248 if (!BaseReg) {
249 BaseReg = TmpReg;
250 } else {
251 assert (!IndexReg && "BaseReg/IndexReg already set!");
252 IndexReg = TmpReg;
253 Scale = 1;
254 }
255 }
Chad Rosier5362af92013-04-16 18:15:40 +0000256 break;
257 }
Chad Rosier31246272013-04-17 21:01:45 +0000258 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000259 }
260 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000261 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000262 switch (State) {
263 default:
264 State = IES_ERROR;
265 break;
266 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000267 case IES_MULTIPLY:
268 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000269 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000270 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000271 case IES_LBRAC:
272 case IES_RBRAC:
273 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000274 case IES_REGISTER:
275 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000276 // Only push the minus operator if it is not a unary operator.
277 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
278 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
279 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
280 IC.pushOperator(IC_MINUS);
281 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
282 // If we already have a BaseReg, then assume this is the IndexReg with
283 // a scale of 1.
284 if (!BaseReg) {
285 BaseReg = TmpReg;
286 } else {
287 assert (!IndexReg && "BaseReg/IndexReg already set!");
288 IndexReg = TmpReg;
289 Scale = 1;
290 }
Chad Rosier5362af92013-04-16 18:15:40 +0000291 }
Chad Rosier5362af92013-04-16 18:15:40 +0000292 break;
293 }
Chad Rosier31246272013-04-17 21:01:45 +0000294 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000295 }
296 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000297 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000298 switch (State) {
299 default:
300 State = IES_ERROR;
301 break;
302 case IES_PLUS:
303 case IES_LPAREN:
304 State = IES_REGISTER;
305 TmpReg = Reg;
306 IC.pushOperand(IC_REGISTER);
307 break;
Chad Rosier31246272013-04-17 21:01:45 +0000308 case IES_MULTIPLY:
309 // Index Register - Scale * Register
310 if (PrevState == IES_INTEGER) {
311 assert (!IndexReg && "IndexReg already set!");
312 State = IES_REGISTER;
313 IndexReg = Reg;
314 // Get the scale and replace the 'Scale * Register' with '0'.
315 Scale = IC.popOperand();
316 IC.pushOperand(IC_IMM);
317 IC.popOperator();
318 } else {
319 State = IES_ERROR;
320 }
Chad Rosier5362af92013-04-16 18:15:40 +0000321 break;
322 }
Chad Rosier31246272013-04-17 21:01:45 +0000323 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000324 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000325 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000326 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000327 switch (State) {
328 default:
329 State = IES_ERROR;
330 break;
331 case IES_PLUS:
332 case IES_MINUS:
333 State = IES_INTEGER;
334 Sym = SymRef;
335 SymName = SymRefName;
336 IC.pushOperand(IC_IMM);
337 break;
338 }
339 }
340 void onInteger(int64_t TmpInt) {
Chad Rosier31246272013-04-17 21:01:45 +0000341 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000342 switch (State) {
343 default:
344 State = IES_ERROR;
345 break;
346 case IES_PLUS:
347 case IES_MINUS:
Chad Rosier5362af92013-04-16 18:15:40 +0000348 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000349 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000350 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000351 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000352 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
353 // Index Register - Register * Scale
354 assert (!IndexReg && "IndexReg already set!");
355 IndexReg = TmpReg;
356 Scale = TmpInt;
357 // Get the scale and replace the 'Register * Scale' with '0'.
358 IC.popOperator();
359 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
360 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
361 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
362 CurrState == IES_MINUS) {
363 // Unary minus. No need to pop the minus operand because it was never
364 // pushed.
365 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
366 } else {
367 IC.pushOperand(IC_IMM, TmpInt);
368 }
Chad Rosier5362af92013-04-16 18:15:40 +0000369 break;
370 }
Chad Rosier31246272013-04-17 21:01:45 +0000371 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000372 }
373 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000374 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000375 switch (State) {
376 default:
377 State = IES_ERROR;
378 break;
379 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000380 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000381 case IES_RPAREN:
382 State = IES_MULTIPLY;
383 IC.pushOperator(IC_MULTIPLY);
384 break;
385 }
386 }
387 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000388 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000389 switch (State) {
390 default:
391 State = IES_ERROR;
392 break;
393 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000394 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000395 State = IES_DIVIDE;
396 IC.pushOperator(IC_DIVIDE);
397 break;
398 }
399 }
400 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000401 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000402 switch (State) {
403 default:
404 State = IES_ERROR;
405 break;
406 case IES_RBRAC:
407 State = IES_PLUS;
408 IC.pushOperator(IC_PLUS);
409 break;
410 }
411 }
412 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000413 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000414 switch (State) {
415 default:
416 State = IES_ERROR;
417 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000418 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000419 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000420 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000421 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000422 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
423 // If we already have a BaseReg, then assume this is the IndexReg with
424 // a scale of 1.
425 if (!BaseReg) {
426 BaseReg = TmpReg;
427 } else {
428 assert (!IndexReg && "BaseReg/IndexReg already set!");
429 IndexReg = TmpReg;
430 Scale = 1;
431 }
Chad Rosier5362af92013-04-16 18:15:40 +0000432 }
433 break;
434 }
Chad Rosier31246272013-04-17 21:01:45 +0000435 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000436 }
437 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000438 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000439 switch (State) {
440 default:
441 State = IES_ERROR;
442 break;
443 case IES_PLUS:
444 case IES_MINUS:
445 case IES_MULTIPLY:
446 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000447 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000448 // FIXME: We don't handle this type of unary minus, yet.
449 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
450 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
451 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
452 CurrState == IES_MINUS) {
453 State = IES_ERROR;
454 break;
455 }
Chad Rosier5362af92013-04-16 18:15:40 +0000456 State = IES_LPAREN;
457 IC.pushOperator(IC_LPAREN);
458 break;
459 }
Chad Rosier31246272013-04-17 21:01:45 +0000460 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000461 }
462 void onRParen() {
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;
Chad Rosier5362af92013-04-16 18:15:40 +0000468 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000469 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000470 case IES_RPAREN:
471 State = IES_RPAREN;
472 IC.pushOperator(IC_RPAREN);
473 break;
474 }
475 }
476 };
477
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000478 MCAsmParser &getParser() const { return Parser; }
479
480 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
481
Chris Lattnera3a06812011-10-16 04:47:35 +0000482 bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000483 ArrayRef<SMRange> Ranges = None,
Chad Rosier4453e842012-10-12 23:09:25 +0000484 bool MatchingInlineAsm = false) {
485 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000486 return Parser.Error(L, Msg, Ranges);
487 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000488
Devang Patel41b9dde2012-01-17 18:00:18 +0000489 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
490 Error(Loc, Msg);
491 return 0;
492 }
493
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000494 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000495 X86Operand *ParseATTOperand();
496 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000497 X86Operand *ParseIntelOffsetOfOperator();
Chad Rosiercc541e82013-04-19 15:57:00 +0000498 X86Operand *ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000499 X86Operand *ParseIntelOperator(unsigned OpKind);
David Majnemeraa34d792013-08-27 21:56:17 +0000500 X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
501 X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
502 unsigned Size);
Chad Rosier5362af92013-04-16 18:15:40 +0000503 X86Operand *ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000504 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000505 int64_t ImmDisp, unsigned Size);
Chad Rosier95ce8892013-04-19 18:39:50 +0000506 X86Operand *ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
John McCallf73981b2013-05-03 00:15:41 +0000507 InlineAsmIdentifierInfo &Info,
508 bool IsUnevaluatedOperand, SMLoc &End);
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000509
Chris Lattnerb9270732010-04-17 18:56:34 +0000510 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000511
Chad Rosier175d0ae2013-04-12 18:21:18 +0000512 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
513 unsigned BaseReg, unsigned IndexReg,
514 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000515 unsigned Size, StringRef Identifier,
516 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000517
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000518 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000519 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000520
Devang Patelde47cce2012-01-18 22:42:29 +0000521 bool processInstruction(MCInst &Inst,
522 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
523
Chad Rosier49963552012-10-13 00:26:04 +0000524 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000525 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000526 MCStreamer &Out, unsigned &ErrorInfo,
527 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000528
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000529 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000530 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000531 bool isSrcOp(X86Operand &Op);
532
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000533 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
534 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000535 bool isDstOp(X86Operand &Op);
536
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000537 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000538 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000539 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000540 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000541 void SwitchMode() {
542 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
543 setAvailableFeatures(FB);
544 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000545
Chad Rosierc2f055d2013-04-18 16:13:18 +0000546 bool isParsingIntelSyntax() {
547 return getParser().getAssemblerDialect();
548 }
549
Daniel Dunbareefe8612010-07-19 05:44:09 +0000550 /// @name Auto-generated Matcher Functions
551 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000552
Chris Lattner3e4582a2010-09-06 19:11:01 +0000553#define GET_ASSEMBLER_HEADER
554#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000555
Daniel Dunbar00331992009-07-29 00:02:19 +0000556 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000557
558public:
Devang Patel4a6e7782012-01-12 18:03:40 +0000559 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosierf0e87202012-10-25 20:41:34 +0000560 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000561
Daniel Dunbareefe8612010-07-19 05:44:09 +0000562 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000563 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000564 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000565 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000566
Chad Rosierf0e87202012-10-25 20:41:34 +0000567 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
568 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000569 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000570
571 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000572};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000573} // end anonymous namespace
574
Sean Callanan86c11812010-01-23 00:40:33 +0000575/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000576/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000577
Chris Lattner60db0a62010-02-09 00:34:28 +0000578static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000579
580/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000581
Craig Topper6bf3ed42012-07-18 04:59:16 +0000582static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000583 return (( Value <= 0x000000000000007FULL)||
584 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
585 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
586}
587
588static bool isImmSExti32i8Value(uint64_t Value) {
589 return (( Value <= 0x000000000000007FULL)||
590 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
591 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
592}
593
594static bool isImmZExtu32u8Value(uint64_t Value) {
595 return (Value <= 0x00000000000000FFULL);
596}
597
598static bool isImmSExti64i8Value(uint64_t Value) {
599 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000600 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000601}
602
603static bool isImmSExti64i32Value(uint64_t Value) {
604 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000605 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000606}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000607namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000608
609/// X86Operand - Instances of this class represent a parsed X86 machine
610/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000611struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000612 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000613 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000614 Register,
615 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000616 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000617 } Kind;
618
Chris Lattner0c2538f2010-01-15 18:51:29 +0000619 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000620 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000621 StringRef SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000622 void *OpDecl;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000623 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000624
Eric Christopher8996c5d2013-03-15 00:42:55 +0000625 struct TokOp {
626 const char *Data;
627 unsigned Length;
628 };
629
630 struct RegOp {
631 unsigned RegNo;
632 };
633
634 struct ImmOp {
635 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000636 };
637
638 struct MemOp {
639 unsigned SegReg;
640 const MCExpr *Disp;
641 unsigned BaseReg;
642 unsigned IndexReg;
643 unsigned Scale;
644 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000645 };
646
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000647 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000648 struct TokOp Tok;
649 struct RegOp Reg;
650 struct ImmOp Imm;
651 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000652 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000653
Chris Lattner015cfb12010-01-15 19:33:43 +0000654 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000655 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000656
Chad Rosiere81309b2013-04-09 17:53:49 +0000657 StringRef getSymName() { return SymName; }
Chad Rosier732b8372013-04-22 22:04:25 +0000658 void *getOpDecl() { return OpDecl; }
Chad Rosiere81309b2013-04-09 17:53:49 +0000659
Chris Lattner86e61532010-01-15 19:06:59 +0000660 /// getStartLoc - Get the location of the first token of this operand.
661 SMLoc getStartLoc() const { return StartLoc; }
662 /// getEndLoc - Get the location of the last token of this operand.
663 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000664 /// getLocRange - Get the range between the first and last token of this
665 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000666 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000667 /// getOffsetOfLoc - Get the location of the offset operator.
668 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000669
Jim Grosbach602aa902011-07-13 15:34:57 +0000670 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000671
Daniel Dunbare10787e2009-08-07 08:26:05 +0000672 StringRef getToken() const {
673 assert(Kind == Token && "Invalid access!");
674 return StringRef(Tok.Data, Tok.Length);
675 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000676 void setTokenValue(StringRef Value) {
677 assert(Kind == Token && "Invalid access!");
678 Tok.Data = Value.data();
679 Tok.Length = Value.size();
680 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000681
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000682 unsigned getReg() const {
683 assert(Kind == Register && "Invalid access!");
684 return Reg.RegNo;
685 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000686
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000687 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000688 assert(Kind == Immediate && "Invalid access!");
689 return Imm.Val;
690 }
691
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000692 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000693 assert(Kind == Memory && "Invalid access!");
694 return Mem.Disp;
695 }
696 unsigned getMemSegReg() const {
697 assert(Kind == Memory && "Invalid access!");
698 return Mem.SegReg;
699 }
700 unsigned getMemBaseReg() const {
701 assert(Kind == Memory && "Invalid access!");
702 return Mem.BaseReg;
703 }
704 unsigned getMemIndexReg() const {
705 assert(Kind == Memory && "Invalid access!");
706 return Mem.IndexReg;
707 }
708 unsigned getMemScale() const {
709 assert(Kind == Memory && "Invalid access!");
710 return Mem.Scale;
711 }
712
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000713 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000714
715 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000716
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000717 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000718 if (!isImm())
719 return false;
720
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000721 // If this isn't a constant expr, just assume it fits and let relaxation
722 // handle it.
723 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
724 if (!CE)
725 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000726
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000727 // Otherwise, check the value is in a range that makes sense for this
728 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000729 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000730 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000731 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000732 if (!isImm())
733 return false;
734
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000735 // If this isn't a constant expr, just assume it fits and let relaxation
736 // handle it.
737 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
738 if (!CE)
739 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000740
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000741 // Otherwise, check the value is in a range that makes sense for this
742 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000743 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000744 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000745 bool isImmZExtu32u8() const {
746 if (!isImm())
747 return false;
748
749 // If this isn't a constant expr, just assume it fits and let relaxation
750 // handle it.
751 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
752 if (!CE)
753 return true;
754
755 // Otherwise, check the value is in a range that makes sense for this
756 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000757 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000758 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000759 bool isImmSExti64i8() const {
760 if (!isImm())
761 return false;
762
763 // If this isn't a constant expr, just assume it fits and let relaxation
764 // handle it.
765 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
766 if (!CE)
767 return true;
768
769 // Otherwise, check the value is in a range that makes sense for this
770 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000771 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000772 }
773 bool isImmSExti64i32() const {
774 if (!isImm())
775 return false;
776
777 // If this isn't a constant expr, just assume it fits and let relaxation
778 // handle it.
779 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
780 if (!CE)
781 return true;
782
783 // Otherwise, check the value is in a range that makes sense for this
784 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000785 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000786 }
787
Chad Rosier5bca3f92012-10-22 19:50:35 +0000788 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000789 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000790 }
791
Chad Rosiera4bc9432013-01-10 22:10:27 +0000792 bool needAddressOf() const {
793 return AddressOf;
794 }
795
Daniel Dunbare10787e2009-08-07 08:26:05 +0000796 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000797 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000798 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000799 }
Chad Rosier51afe632012-06-27 22:34:28 +0000800 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000801 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000802 }
Chad Rosier51afe632012-06-27 22:34:28 +0000803 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000804 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000805 }
Chad Rosier51afe632012-06-27 22:34:28 +0000806 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000807 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000808 }
Chad Rosier51afe632012-06-27 22:34:28 +0000809 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000810 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000811 }
Chad Rosier51afe632012-06-27 22:34:28 +0000812 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000813 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000814 }
Chad Rosier51afe632012-06-27 22:34:28 +0000815 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000816 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000817 }
Craig Topper8c26c422013-08-25 23:18:05 +0000818 bool isMem512() const {
819 return Kind == Memory && (!Mem.Size || Mem.Size == 512);
820 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000821
Craig Topper01deb5f2012-07-18 04:11:12 +0000822 bool isMemVX32() const {
823 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
824 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
825 }
826 bool isMemVY32() const {
827 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
828 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
829 }
830 bool isMemVX64() const {
831 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
832 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
833 }
834 bool isMemVY64() const {
835 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
836 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
837 }
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000838 bool isMemVZ32() const {
839 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
840 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
841 }
842 bool isMemVZ64() const {
843 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
844 getMemIndexReg() >= X86::ZMM0 && getMemIndexReg() <= X86::ZMM31;
845 }
846
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000847 bool isAbsMem() const {
848 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000849 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000850 }
851
Craig Topper18854172013-08-25 22:23:38 +0000852 bool isMemOffs8() const {
853 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
854 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 8);
855 }
856 bool isMemOffs16() const {
857 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
858 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 16);
859 }
860 bool isMemOffs32() const {
861 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
862 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 32);
863 }
864 bool isMemOffs64() const {
865 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
866 !getMemIndexReg() && getMemScale() == 1 && (!Mem.Size || Mem.Size == 64);
867 }
868
Daniel Dunbare10787e2009-08-07 08:26:05 +0000869 bool isReg() const { return Kind == Register; }
870
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000871 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
872 // Add as immediates when possible.
873 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
874 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
875 else
876 Inst.addOperand(MCOperand::CreateExpr(Expr));
877 }
878
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000879 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000880 assert(N == 1 && "Invalid number of operands!");
881 Inst.addOperand(MCOperand::CreateReg(getReg()));
882 }
883
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000884 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000885 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000886 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000887 }
888
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000889 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000890 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000891 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
892 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
893 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000894 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000895 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
896 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000897
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000898 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
899 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000900 // Add as immediates when possible.
901 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
902 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
903 else
904 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000905 }
906
Craig Topper18854172013-08-25 22:23:38 +0000907 void addMemOffsOperands(MCInst &Inst, unsigned N) const {
908 assert((N == 1) && "Invalid number of operands!");
909 // Add as immediates when possible.
910 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
911 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
912 else
913 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
914 }
915
Chris Lattner528d00b2010-01-15 19:28:38 +0000916 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000917 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000918 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000919 Res->Tok.Data = Str.data();
920 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000921 return Res;
922 }
923
Chad Rosier91c82662012-10-24 17:22:29 +0000924 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000925 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000926 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +0000927 StringRef SymName = StringRef(),
928 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +0000929 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000930 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000931 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000932 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000933 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000934 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000935 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000936 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000937
Chad Rosierf3c04f62013-03-19 21:58:18 +0000938 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000939 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000940 Res->Imm.Val = Val;
941 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000942 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000943
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000944 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000945 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +0000946 unsigned Size = 0, StringRef SymName = StringRef(),
947 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000948 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
949 Res->Mem.SegReg = 0;
950 Res->Mem.Disp = Disp;
951 Res->Mem.BaseReg = 0;
952 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000953 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000954 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +0000955 Res->SymName = SymName;
956 Res->OpDecl = OpDecl;
957 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000958 return Res;
959 }
960
961 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000962 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
963 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +0000964 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000965 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +0000966 StringRef SymName = StringRef(),
967 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000968 // We should never just have a displacement, that should be parsed as an
969 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000970 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
971
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000972 // The scale should always be one of {1,2,4,8}.
973 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000974 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000975 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000976 Res->Mem.SegReg = SegReg;
977 Res->Mem.Disp = Disp;
978 Res->Mem.BaseReg = BaseReg;
979 Res->Mem.IndexReg = IndexReg;
980 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +0000981 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +0000982 Res->SymName = SymName;
983 Res->OpDecl = OpDecl;
984 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000985 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000986 }
987};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000988
Chris Lattner4eb9df02009-07-29 06:33:53 +0000989} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000990
Devang Patel4a6e7782012-01-12 18:03:40 +0000991bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000992 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000993
994 return (Op.isMem() &&
995 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
996 isa<MCConstantExpr>(Op.Mem.Disp) &&
997 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
998 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
999}
1000
Devang Patel4a6e7782012-01-12 18:03:40 +00001001bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001002 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001003
Chad Rosier51afe632012-06-27 22:34:28 +00001004 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +00001005 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001006 isa<MCConstantExpr>(Op.Mem.Disp) &&
1007 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1008 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
1009}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001010
Devang Patel4a6e7782012-01-12 18:03:40 +00001011bool X86AsmParser::ParseRegister(unsigned &RegNo,
1012 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001013 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001014 const AsmToken &PercentTok = Parser.getTok();
1015 StartLoc = PercentTok.getLoc();
1016
1017 // If we encounter a %, ignore it. This code handles registers with and
1018 // without the prefix, unprefixed registers can occur in cfi directives.
1019 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001020 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001021
Sean Callanan936b0d32010-01-19 21:44:56 +00001022 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001023 EndLoc = Tok.getEndLoc();
1024
Devang Patelce6a2ca2012-01-20 22:32:05 +00001025 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001026 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001027 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001028 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001029 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001030
Kevin Enderby7d912182009-09-03 17:15:07 +00001031 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001032
Chris Lattner1261b812010-09-22 04:11:10 +00001033 // If the match failed, try the register name as lowercase.
1034 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001035 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001036
Evan Chengeda1d4f2011-07-27 23:22:03 +00001037 if (!is64BitMode()) {
1038 // FIXME: This should be done using Requires<In32BitMode> and
1039 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1040 // checked.
1041 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1042 // REX prefix.
1043 if (RegNo == X86::RIZ ||
1044 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1045 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1046 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001047 return Error(StartLoc, "register %"
1048 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001049 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001050 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001051
Chris Lattner1261b812010-09-22 04:11:10 +00001052 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1053 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001054 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001055 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001056
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001057 // Check to see if we have '(4)' after %st.
1058 if (getLexer().isNot(AsmToken::LParen))
1059 return false;
1060 // Lex the paren.
1061 getParser().Lex();
1062
1063 const AsmToken &IntTok = Parser.getTok();
1064 if (IntTok.isNot(AsmToken::Integer))
1065 return Error(IntTok.getLoc(), "expected stack index");
1066 switch (IntTok.getIntVal()) {
1067 case 0: RegNo = X86::ST0; break;
1068 case 1: RegNo = X86::ST1; break;
1069 case 2: RegNo = X86::ST2; break;
1070 case 3: RegNo = X86::ST3; break;
1071 case 4: RegNo = X86::ST4; break;
1072 case 5: RegNo = X86::ST5; break;
1073 case 6: RegNo = X86::ST6; break;
1074 case 7: RegNo = X86::ST7; break;
1075 default: return Error(IntTok.getLoc(), "invalid stack index");
1076 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001077
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001078 if (getParser().Lex().isNot(AsmToken::RParen))
1079 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001080
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001081 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001082 Parser.Lex(); // Eat ')'
1083 return false;
1084 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001085
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001086 EndLoc = Parser.getTok().getEndLoc();
1087
Chris Lattner80486622010-06-24 07:29:18 +00001088 // If this is "db[0-7]", match it as an alias
1089 // for dr[0-7].
1090 if (RegNo == 0 && Tok.getString().size() == 3 &&
1091 Tok.getString().startswith("db")) {
1092 switch (Tok.getString()[2]) {
1093 case '0': RegNo = X86::DR0; break;
1094 case '1': RegNo = X86::DR1; break;
1095 case '2': RegNo = X86::DR2; break;
1096 case '3': RegNo = X86::DR3; break;
1097 case '4': RegNo = X86::DR4; break;
1098 case '5': RegNo = X86::DR5; break;
1099 case '6': RegNo = X86::DR6; break;
1100 case '7': RegNo = X86::DR7; break;
1101 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001102
Chris Lattner80486622010-06-24 07:29:18 +00001103 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001104 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001105 Parser.Lex(); // Eat it.
1106 return false;
1107 }
1108 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001109
Devang Patelce6a2ca2012-01-20 22:32:05 +00001110 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001111 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001112 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001113 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001114 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001115
Sean Callanana83fd7d2010-01-19 20:27:46 +00001116 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001117 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001118}
1119
Devang Patel4a6e7782012-01-12 18:03:40 +00001120X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001121 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001122 return ParseIntelOperand();
1123 return ParseATTOperand();
1124}
1125
Devang Patel41b9dde2012-01-17 18:00:18 +00001126/// getIntelMemOperandSize - Return intel memory operand size.
1127static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001128 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001129 .Cases("BYTE", "byte", 8)
1130 .Cases("WORD", "word", 16)
1131 .Cases("DWORD", "dword", 32)
1132 .Cases("QWORD", "qword", 64)
1133 .Cases("XWORD", "xword", 80)
1134 .Cases("XMMWORD", "xmmword", 128)
1135 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001136 .Default(0);
1137 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001138}
1139
Chad Rosier175d0ae2013-04-12 18:21:18 +00001140X86Operand *
1141X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1142 unsigned BaseReg, unsigned IndexReg,
1143 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001144 unsigned Size, StringRef Identifier,
1145 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001146 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001147 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1148 // reference. We need an 'r' constraint here, so we need to create register
1149 // operand to ensure proper matching. Just pick a GPR based on the size of
1150 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001151 if (!Info.IsVarDecl) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001152 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1153 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001154 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001155 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001156 if (!Size) {
1157 Size = Info.Type * 8; // Size is in terms of bits in this context.
1158 if (Size)
1159 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1160 /*Len=*/0, Size));
1161 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001162 }
1163
Chad Rosier7ca135b2013-03-19 21:11:56 +00001164 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001165 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001166 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001167 BaseReg = BaseReg ? BaseReg : 1;
1168 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001169 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001170}
1171
Chad Rosierd383db52013-04-12 20:20:54 +00001172static void
1173RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1174 StringRef SymName, int64_t ImmDisp,
1175 int64_t FinalImmDisp, SMLoc &BracLoc,
1176 SMLoc &StartInBrac, SMLoc &End) {
1177 // Remove the '[' and ']' from the IR string.
1178 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1179 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1180
1181 // If ImmDisp is non-zero, then we parsed a displacement before the
1182 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1183 // If ImmDisp doesn't match the displacement computed by the state machine
1184 // then we have an additional displacement in the bracketed expression.
1185 if (ImmDisp != FinalImmDisp) {
1186 if (ImmDisp) {
1187 // We have an immediate displacement before the bracketed expression.
1188 // Adjust this to match the final immediate displacement.
1189 bool Found = false;
1190 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1191 E = AsmRewrites->end(); I != E; ++I) {
1192 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1193 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001194 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1195 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001196 (*I).Kind = AOK_Imm;
1197 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1198 (*I).Val = FinalImmDisp;
1199 Found = true;
1200 break;
1201 }
1202 }
1203 assert (Found && "Unable to rewrite ImmDisp.");
Duncan Sands0480b9b2013-05-13 07:50:47 +00001204 (void)Found;
Chad Rosierd383db52013-04-12 20:20:54 +00001205 } else {
1206 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001207 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001208 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001209 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001210 }
1211 }
1212 // Remove all the ImmPrefix rewrites within the brackets.
1213 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1214 E = AsmRewrites->end(); I != E; ++I) {
1215 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1216 continue;
1217 if ((*I).Kind == AOK_ImmPrefix)
1218 (*I).Kind = AOK_Delete;
1219 }
1220 const char *SymLocPtr = SymName.data();
1221 // Skip everything before the symbol.
1222 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1223 assert(Len > 0 && "Expected a non-negative length.");
1224 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1225 }
1226 // Skip everything after the symbol.
1227 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1228 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1229 assert(Len > 0 && "Expected a non-negative length.");
1230 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1231 }
1232}
1233
Chad Rosier5362af92013-04-16 18:15:40 +00001234X86Operand *
1235X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001236 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001237
Chad Rosier5c118fd2013-01-14 22:31:35 +00001238 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001239 while (!Done) {
1240 bool UpdateLocLex = true;
1241
1242 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1243 // identifier. Don't try an parse it as a register.
1244 if (Tok.getString().startswith("."))
1245 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001246
1247 // If we're parsing an immediate expression, we don't expect a '['.
1248 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1249 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001250
1251 switch (getLexer().getKind()) {
1252 default: {
1253 if (SM.isValidEndState()) {
1254 Done = true;
1255 break;
1256 }
1257 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1258 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001259 case AsmToken::EndOfStatement: {
1260 Done = true;
1261 break;
1262 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001263 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001264 // This could be a register or a symbolic displacement.
1265 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001266 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001267 SMLoc IdentLoc = Tok.getLoc();
1268 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001269 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001270 SM.onRegister(TmpReg);
1271 UpdateLocLex = false;
1272 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001273 } else {
1274 if (!isParsingInlineAsm()) {
1275 if (getParser().parsePrimaryExpr(Val, End))
1276 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1277 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001278 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
John McCallf73981b2013-05-03 00:15:41 +00001279 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info,
1280 /*Unevaluated*/ false, End))
Chad Rosier95ce8892013-04-19 18:39:50 +00001281 return Err;
1282 }
1283 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001284 UpdateLocLex = false;
1285 break;
1286 }
1287 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1288 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001289 case AsmToken::Integer:
Chad Rosierbfb70992013-04-17 00:11:46 +00001290 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001291 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1292 Tok.getLoc()));
1293 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001294 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001295 case AsmToken::Plus: SM.onPlus(); break;
1296 case AsmToken::Minus: SM.onMinus(); break;
1297 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001298 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001299 case AsmToken::LBrac: SM.onLBrac(); break;
1300 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001301 case AsmToken::LParen: SM.onLParen(); break;
1302 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001303 }
Chad Rosier31246272013-04-17 21:01:45 +00001304 if (SM.hadError())
1305 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1306
Chad Rosier5c118fd2013-01-14 22:31:35 +00001307 if (!Done && UpdateLocLex) {
1308 End = Tok.getLoc();
1309 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001310 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001311 }
Chad Rosier5362af92013-04-16 18:15:40 +00001312 return 0;
1313}
1314
1315X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001316 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001317 unsigned Size) {
1318 const AsmToken &Tok = Parser.getTok();
1319 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1320 if (getLexer().isNot(AsmToken::LBrac))
1321 return ErrorOperand(BracLoc, "Expected '[' token!");
1322 Parser.Lex(); // Eat '['
1323
1324 SMLoc StartInBrac = Tok.getLoc();
1325 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1326 // may have already parsed an immediate displacement before the bracketed
1327 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001328 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Chad Rosier5362af92013-04-16 18:15:40 +00001329 if (X86Operand *Err = ParseIntelExpression(SM, End))
1330 return Err;
Devang Patel41b9dde2012-01-17 18:00:18 +00001331
Chad Rosier175d0ae2013-04-12 18:21:18 +00001332 const MCExpr *Disp;
1333 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001334 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001335 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001336 if (isParsingInlineAsm())
1337 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001338 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001339 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001340 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001341 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001342 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001343 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001344
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001345 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001346 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001347 const MCExpr *NewDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001348 if (X86Operand *Err = ParseIntelDotOperator(Disp, NewDisp))
1349 return Err;
Chad Rosier911c1f32012-10-25 17:37:43 +00001350
Chad Rosier70f47592013-04-10 20:07:47 +00001351 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001352 Parser.Lex(); // Eat the field.
1353 Disp = NewDisp;
1354 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001355
Chad Rosier5c118fd2013-01-14 22:31:35 +00001356 int BaseReg = SM.getBaseReg();
1357 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001358 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001359 if (!isParsingInlineAsm()) {
1360 // handle [-42]
1361 if (!BaseReg && !IndexReg) {
1362 if (!SegReg)
1363 return X86Operand::CreateMem(Disp, Start, End, Size);
1364 else
1365 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1366 }
1367 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1368 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001369 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001370
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001371 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001372 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001373 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001374}
1375
Chad Rosier8a244662013-04-02 20:02:33 +00001376// Inline assembly may use variable names with namespace alias qualifiers.
Chad Rosier95ce8892013-04-19 18:39:50 +00001377X86Operand *X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1378 StringRef &Identifier,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001379 InlineAsmIdentifierInfo &Info,
John McCallf73981b2013-05-03 00:15:41 +00001380 bool IsUnevaluatedOperand,
Chad Rosier95ce8892013-04-19 18:39:50 +00001381 SMLoc &End) {
1382 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1383 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001384
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001385 StringRef LineBuf(Identifier.data());
John McCallf73981b2013-05-03 00:15:41 +00001386 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001387
Chad Rosier8a244662013-04-02 20:02:33 +00001388 const AsmToken &Tok = Parser.getTok();
John McCallf73981b2013-05-03 00:15:41 +00001389
1390 // Advance the token stream until the end of the current token is
1391 // after the end of what the frontend claimed.
1392 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1393 while (true) {
1394 End = Tok.getEndLoc();
1395 getLexer().Lex();
1396
1397 assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1398 if (End.getPointer() == EndPtr) break;
Chad Rosier8a244662013-04-02 20:02:33 +00001399 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001400
1401 // Create the symbol reference.
1402 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001403 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1404 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001405 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Chad Rosier8a244662013-04-02 20:02:33 +00001406 return 0;
1407}
1408
David Majnemeraa34d792013-08-27 21:56:17 +00001409/// \brief Parse intel style segment override.
1410X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1411 SMLoc Start,
1412 unsigned Size) {
1413 assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1414 const AsmToken &Tok = Parser.getTok(); // Eat colon.
1415 if (Tok.isNot(AsmToken::Colon))
1416 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1417 Parser.Lex(); // Eat ':'
Devang Patel41b9dde2012-01-17 18:00:18 +00001418
David Majnemeraa34d792013-08-27 21:56:17 +00001419 int64_t ImmDisp = 0;
Chad Rosier1530ba52013-03-27 21:49:56 +00001420 if (getLexer().is(AsmToken::Integer)) {
David Majnemeraa34d792013-08-27 21:56:17 +00001421 ImmDisp = Tok.getIntVal();
1422 AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1423
Chad Rosier1530ba52013-03-27 21:49:56 +00001424 if (isParsingInlineAsm())
David Majnemeraa34d792013-08-27 21:56:17 +00001425 InstInfo->AsmRewrites->push_back(
1426 AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1427
1428 if (getLexer().isNot(AsmToken::LBrac)) {
1429 // An immediate following a 'segment register', 'colon' token sequence can
1430 // be followed by a bracketed expression. If it isn't we know we have our
1431 // final segment override.
1432 const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1433 return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1434 /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1435 Size);
1436 }
Chad Rosier1530ba52013-03-27 21:49:56 +00001437 }
1438
Chad Rosier91c82662012-10-24 17:22:29 +00001439 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001440 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001441
David Majnemeraa34d792013-08-27 21:56:17 +00001442 const MCExpr *Val;
1443 SMLoc End;
1444 if (!isParsingInlineAsm()) {
1445 if (getParser().parsePrimaryExpr(Val, End))
1446 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1447
1448 return X86Operand::CreateMem(Val, Start, End, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001449 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001450
David Majnemeraa34d792013-08-27 21:56:17 +00001451 InlineAsmIdentifierInfo Info;
1452 StringRef Identifier = Tok.getString();
1453 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info,
1454 /*Unevaluated*/ false, End))
1455 return Err;
1456 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1457 /*Scale=*/1, Start, End, Size, Identifier, Info);
1458}
1459
1460/// ParseIntelMemOperand - Parse intel style memory operand.
1461X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1462 unsigned Size) {
1463 const AsmToken &Tok = Parser.getTok();
1464 SMLoc End;
1465
1466 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1467 if (getLexer().is(AsmToken::LBrac))
1468 return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1469
Chad Rosier95ce8892013-04-19 18:39:50 +00001470 const MCExpr *Val;
1471 if (!isParsingInlineAsm()) {
1472 if (getParser().parsePrimaryExpr(Val, End))
1473 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1474
1475 return X86Operand::CreateMem(Val, Start, End, Size);
1476 }
1477
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001478 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001479 StringRef Identifier = Tok.getString();
John McCallf73981b2013-05-03 00:15:41 +00001480 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info,
1481 /*Unevaluated*/ false, End))
Chad Rosier8a244662013-04-02 20:02:33 +00001482 return Err;
David Majnemeraa34d792013-08-27 21:56:17 +00001483 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001484 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001485}
1486
Chad Rosier5dcb4662012-10-24 22:21:50 +00001487/// Parse the '.' operator.
Chad Rosiercc541e82013-04-19 15:57:00 +00001488X86Operand *X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1489 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001490 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001491 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001492
1493 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001494 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001495 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001496 else
1497 return ErrorOperand(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001498
1499 // Drop the '.'.
1500 StringRef DotDispStr = Tok.getString().drop_front(1);
1501
Chad Rosier5dcb4662012-10-24 22:21:50 +00001502 // .Imm gets lexed as a real.
1503 if (Tok.is(AsmToken::Real)) {
1504 APInt DotDisp;
1505 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001506 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001507 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001508 unsigned DotDisp;
1509 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1510 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001511 DotDisp))
1512 return ErrorOperand(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001513 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001514 } else
1515 return ErrorOperand(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001516
Chad Rosier240b7b92012-10-25 21:51:10 +00001517 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1518 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1519 unsigned Len = DotDispStr.size();
1520 unsigned Val = OrigDispVal + DotDispVal;
1521 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1522 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001523 }
1524
Chad Rosiercc541e82013-04-19 15:57:00 +00001525 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1526 return 0;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001527}
1528
Chad Rosier91c82662012-10-24 17:22:29 +00001529/// Parse the 'offset' operator. This operator is used to specify the
1530/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001531X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001532 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001533 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001534 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001535
Chad Rosier91c82662012-10-24 17:22:29 +00001536 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001537 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001538 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001539 StringRef Identifier = Tok.getString();
John McCallf73981b2013-05-03 00:15:41 +00001540 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info,
1541 /*Unevaluated*/ false, End))
Chad Rosierae7ecd62013-04-11 23:37:34 +00001542 return Err;
1543
Chad Rosiere2f03772012-10-26 16:09:20 +00001544 // Don't emit the offset operator.
1545 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1546
Chad Rosier91c82662012-10-24 17:22:29 +00001547 // The offset operator will have an 'r' constraint, thus we need to create
1548 // register operand to ensure proper matching. Just pick a GPR based on
1549 // the size of a pointer.
1550 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001551 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001552 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001553}
1554
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001555enum IntelOperatorKind {
1556 IOK_LENGTH,
1557 IOK_SIZE,
1558 IOK_TYPE
1559};
1560
1561/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1562/// returns the number of elements in an array. It returns the value 1 for
1563/// non-array variables. The SIZE operator returns the size of a C or C++
1564/// variable. A variable's size is the product of its LENGTH and TYPE. The
1565/// TYPE operator returns the size of a C or C++ type or variable. If the
1566/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001567X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001568 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001569 SMLoc TypeLoc = Tok.getLoc();
1570 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001571
Chad Rosier95ce8892013-04-19 18:39:50 +00001572 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001573 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001574 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001575 StringRef Identifier = Tok.getString();
John McCallf73981b2013-05-03 00:15:41 +00001576 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info,
1577 /*Unevaluated*/ true, End))
Chad Rosierb67f8052013-04-11 23:57:04 +00001578 return Err;
Chad Rosier11c42f22012-10-26 18:04:20 +00001579
Chad Rosierf6675c32013-04-22 17:01:46 +00001580 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001581 switch(OpKind) {
1582 default: llvm_unreachable("Unexpected operand kind!");
1583 case IOK_LENGTH: CVal = Info.Length; break;
1584 case IOK_SIZE: CVal = Info.Size; break;
1585 case IOK_TYPE: CVal = Info.Type; break;
1586 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001587
1588 // Rewrite the type operator and the C or C++ type or variable in terms of an
1589 // immediate. E.g. TYPE foo -> $$4
1590 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001591 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001592
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001593 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001594 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001595}
1596
Devang Patel41b9dde2012-01-17 18:00:18 +00001597X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001598 const AsmToken &Tok = Parser.getTok();
David Majnemeraa34d792013-08-27 21:56:17 +00001599 SMLoc Start, End;
Chad Rosier91c82662012-10-24 17:22:29 +00001600
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001601 // Offset, length, type and size operators.
1602 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001603 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001604 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001605 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001606 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001607 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001608 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001609 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001610 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001611 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001612 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001613
David Majnemeraa34d792013-08-27 21:56:17 +00001614 unsigned Size = getIntelMemOperandSize(Tok.getString());
1615 if (Size) {
1616 Parser.Lex(); // Eat operand size (e.g., byte, word).
1617 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1618 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1619 Parser.Lex(); // Eat ptr.
1620 }
1621 Start = Tok.getLoc();
1622
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001623 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001624 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1625 getLexer().is(AsmToken::LParen)) {
1626 AsmToken StartTok = Tok;
1627 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1628 /*AddImmPrefix=*/false);
1629 if (X86Operand *Err = ParseIntelExpression(SM, End))
1630 return Err;
1631
1632 int64_t Imm = SM.getImm();
1633 if (isParsingInlineAsm()) {
1634 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1635 if (StartTok.getString().size() == Len)
1636 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001637 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001638 else
1639 // Otherwise, rewrite the complex expression as a single immediate.
1640 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001641 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001642
1643 if (getLexer().isNot(AsmToken::LBrac)) {
1644 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1645 return X86Operand::CreateImm(ImmExpr, Start, End);
1646 }
1647
1648 // Only positive immediates are valid.
1649 if (Imm < 0)
1650 return ErrorOperand(Start, "expected a positive immediate displacement "
1651 "before bracketed expr.");
1652
1653 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
David Majnemeraa34d792013-08-27 21:56:17 +00001654 return ParseIntelMemOperand(Imm, Start, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001655 }
1656
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001657 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001658 unsigned RegNo = 0;
1659 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001660 // If this is a segment register followed by a ':', then this is the start
David Majnemeraa34d792013-08-27 21:56:17 +00001661 // of a segment override, otherwise this is a normal register reference.
Chad Rosier0397edd2012-10-04 23:59:38 +00001662 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001663 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001664
David Majnemeraa34d792013-08-27 21:56:17 +00001665 return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001666 }
1667
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001668 // Memory operand.
David Majnemeraa34d792013-08-27 21:56:17 +00001669 return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
Devang Patel46831de2012-01-12 01:36:43 +00001670}
1671
Devang Patel4a6e7782012-01-12 18:03:40 +00001672X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001673 switch (getLexer().getKind()) {
1674 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001675 // Parse a memory operand with no segment register.
1676 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001677 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001678 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001679 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001680 SMLoc Start, End;
1681 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001682 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001683 Error(Start, "%eiz and %riz can only be used as index registers",
1684 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001685 return 0;
1686 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001687
Chris Lattnerb9270732010-04-17 18:56:34 +00001688 // If this is a segment register followed by a ':', then this is the start
1689 // of a memory reference, otherwise this is a normal register reference.
1690 if (getLexer().isNot(AsmToken::Colon))
1691 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001692
Chris Lattnerb9270732010-04-17 18:56:34 +00001693 getParser().Lex(); // Eat the colon.
1694 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001695 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001696 case AsmToken::Dollar: {
1697 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001698 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001699 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001700 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001701 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001702 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001703 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001704 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001705 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001706}
1707
Chris Lattnerb9270732010-04-17 18:56:34 +00001708/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1709/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001710X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001711
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001712 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1713 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001714 // only way to do this without lookahead is to eat the '(' and see what is
1715 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001716 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001717 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001718 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001719 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001720
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001721 // After parsing the base expression we could either have a parenthesized
1722 // memory address or not. If not, return now. If so, eat the (.
1723 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001724 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001725 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001726 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001727 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001728 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001729
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001730 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001731 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001732 } else {
1733 // Okay, we have a '('. We don't know if this is an expression or not, but
1734 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001735 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001736 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001737
Kevin Enderby7d912182009-09-03 17:15:07 +00001738 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001739 // Nothing to do here, fall into the code below with the '(' part of the
1740 // memory operand consumed.
1741 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001742 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001743
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001744 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001745 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001746 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001747
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001748 // After parsing the base expression we could either have a parenthesized
1749 // memory address or not. If not, return now. If so, eat the (.
1750 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001751 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001752 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001753 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001754 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001755 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001756
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001757 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001758 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001759 }
1760 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001761
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001762 // If we reached here, then we just ate the ( of the memory operand. Process
1763 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001764 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001765 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001766
Chris Lattner0c2538f2010-01-15 18:51:29 +00001767 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001768 SMLoc StartLoc, EndLoc;
1769 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001770 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001771 Error(StartLoc, "eiz and riz can only be used as index registers",
1772 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001773 return 0;
1774 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001775 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001776
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001777 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001778 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001779 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001780
1781 // Following the comma we should have either an index register, or a scale
1782 // value. We don't support the later form, but we want to parse it
1783 // correctly.
1784 //
1785 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001786 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001787 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001788 SMLoc L;
1789 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001790
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001791 if (getLexer().isNot(AsmToken::RParen)) {
1792 // Parse the scale amount:
1793 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001794 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001795 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001796 "expected comma in scale expression");
1797 return 0;
1798 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001799 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001800
1801 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001802 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001803
1804 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001805 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001806 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001807 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001808 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001809
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001810 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001811 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1812 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1813 return 0;
1814 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001815 Scale = (unsigned)ScaleVal;
1816 }
1817 }
1818 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001819 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001820 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001821 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001822
1823 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001824 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001825 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001826
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001827 if (Value != 1)
1828 Warning(Loc, "scale factor without index register is ignored");
1829 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001830 }
1831 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001832
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001833 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001834 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001835 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001836 return 0;
1837 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001838 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001839 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001840
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001841 // If we have both a base register and an index register make sure they are
1842 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001843 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001844 if (BaseReg != 0 && IndexReg != 0) {
1845 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001846 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1847 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001848 IndexReg != X86::RIZ) {
1849 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1850 return 0;
1851 }
1852 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001853 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1854 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001855 IndexReg != X86::EIZ){
1856 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1857 return 0;
1858 }
1859 }
1860
Chris Lattner015cfb12010-01-15 19:33:43 +00001861 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1862 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001863}
1864
Devang Patel4a6e7782012-01-12 18:03:40 +00001865bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001866ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001867 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001868 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001869 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001870
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001871 // FIXME: Hack to recognize setneb as setne.
1872 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1873 PatchedName != "setb" && PatchedName != "setnb")
1874 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001875
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001876 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1877 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001878 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001879 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1880 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001881 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001882 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001883 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001884 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001885 .Case("eq", 0x00)
1886 .Case("lt", 0x01)
1887 .Case("le", 0x02)
1888 .Case("unord", 0x03)
1889 .Case("neq", 0x04)
1890 .Case("nlt", 0x05)
1891 .Case("nle", 0x06)
1892 .Case("ord", 0x07)
1893 /* AVX only from here */
1894 .Case("eq_uq", 0x08)
1895 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001896 .Case("ngt", 0x0A)
1897 .Case("false", 0x0B)
1898 .Case("neq_oq", 0x0C)
1899 .Case("ge", 0x0D)
1900 .Case("gt", 0x0E)
1901 .Case("true", 0x0F)
1902 .Case("eq_os", 0x10)
1903 .Case("lt_oq", 0x11)
1904 .Case("le_oq", 0x12)
1905 .Case("unord_s", 0x13)
1906 .Case("neq_us", 0x14)
1907 .Case("nlt_uq", 0x15)
1908 .Case("nle_uq", 0x16)
1909 .Case("ord_s", 0x17)
1910 .Case("eq_us", 0x18)
1911 .Case("nge_uq", 0x19)
1912 .Case("ngt_uq", 0x1A)
1913 .Case("false_os", 0x1B)
1914 .Case("neq_os", 0x1C)
1915 .Case("ge_oq", 0x1D)
1916 .Case("gt_oq", 0x1E)
1917 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001918 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001919 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001920 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1921 getParser().getContext());
1922 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001923 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001924 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001925 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001926 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001927 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001928 } else {
1929 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001930 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001931 }
1932 }
1933 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001934
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001935 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001936
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001937 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001938 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001939
Chris Lattner086a83a2010-09-08 05:17:37 +00001940 // Determine whether this is an instruction prefix.
1941 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001942 Name == "lock" || Name == "rep" ||
1943 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001944 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001945 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001946
1947
Chris Lattner086a83a2010-09-08 05:17:37 +00001948 // This does the actual operand parsing. Don't parse any more if we have a
1949 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1950 // just want to parse the "lock" as the first instruction and the "incl" as
1951 // the next one.
1952 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001953
1954 // Parse '*' modifier.
1955 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001956 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001957 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001958 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001959 }
1960
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001961 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001962 if (X86Operand *Op = ParseOperand())
1963 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001964 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001965 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001966 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001967 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001968
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001969 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001970 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001971
1972 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001973 if (X86Operand *Op = ParseOperand())
1974 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001975 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001976 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001977 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001978 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001979 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001980
Chris Lattnera2a9d162010-09-11 16:18:25 +00001981 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00001982 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001983 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00001984 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00001985 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001986 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001987
Chris Lattner086a83a2010-09-08 05:17:37 +00001988 if (getLexer().is(AsmToken::EndOfStatement))
1989 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00001990 else if (isPrefix && getLexer().is(AsmToken::Slash))
1991 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001992
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001993 if (ExtraImmOp && isParsingIntelSyntax())
1994 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1995
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001996 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1997 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1998 // documented form in various unofficial manuals, so a lot of code uses it.
1999 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
2000 Operands.size() == 3) {
2001 X86Operand &Op = *(X86Operand*)Operands.back();
2002 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2003 isa<MCConstantExpr>(Op.Mem.Disp) &&
2004 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2005 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2006 SMLoc Loc = Op.getEndLoc();
2007 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2008 delete &Op;
2009 }
2010 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00002011 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
2012 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
2013 Operands.size() == 3) {
2014 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2015 if (Op.isMem() && Op.Mem.SegReg == 0 &&
2016 isa<MCConstantExpr>(Op.Mem.Disp) &&
2017 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
2018 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
2019 SMLoc Loc = Op.getEndLoc();
2020 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2021 delete &Op;
2022 }
2023 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002024 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
2025 if (Name.startswith("ins") && Operands.size() == 3 &&
2026 (Name == "insb" || Name == "insw" || Name == "insl")) {
2027 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2028 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2029 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
2030 Operands.pop_back();
2031 Operands.pop_back();
2032 delete &Op;
2033 delete &Op2;
2034 }
2035 }
2036
2037 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
2038 if (Name.startswith("outs") && Operands.size() == 3 &&
2039 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
2040 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2041 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2042 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2043 Operands.pop_back();
2044 Operands.pop_back();
2045 delete &Op;
2046 delete &Op2;
2047 }
2048 }
2049
2050 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2051 if (Name.startswith("movs") && Operands.size() == 3 &&
2052 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002053 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002054 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2055 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2056 if (isSrcOp(Op) && isDstOp(Op2)) {
2057 Operands.pop_back();
2058 Operands.pop_back();
2059 delete &Op;
2060 delete &Op2;
2061 }
2062 }
2063 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2064 if (Name.startswith("lods") && Operands.size() == 3 &&
2065 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002066 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002067 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2068 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2069 if (isSrcOp(*Op1) && Op2->isReg()) {
2070 const char *ins;
2071 unsigned reg = Op2->getReg();
2072 bool isLods = Name == "lods";
2073 if (reg == X86::AL && (isLods || Name == "lodsb"))
2074 ins = "lodsb";
2075 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2076 ins = "lodsw";
2077 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2078 ins = "lodsl";
2079 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2080 ins = "lodsq";
2081 else
2082 ins = NULL;
2083 if (ins != NULL) {
2084 Operands.pop_back();
2085 Operands.pop_back();
2086 delete Op1;
2087 delete Op2;
2088 if (Name != ins)
2089 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2090 }
2091 }
2092 }
2093 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2094 if (Name.startswith("stos") && Operands.size() == 3 &&
2095 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002096 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002097 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2098 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2099 if (isDstOp(*Op2) && Op1->isReg()) {
2100 const char *ins;
2101 unsigned reg = Op1->getReg();
2102 bool isStos = Name == "stos";
2103 if (reg == X86::AL && (isStos || Name == "stosb"))
2104 ins = "stosb";
2105 else if (reg == X86::AX && (isStos || Name == "stosw"))
2106 ins = "stosw";
2107 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2108 ins = "stosl";
2109 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2110 ins = "stosq";
2111 else
2112 ins = NULL;
2113 if (ins != NULL) {
2114 Operands.pop_back();
2115 Operands.pop_back();
2116 delete Op1;
2117 delete Op2;
2118 if (Name != ins)
2119 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2120 }
2121 }
2122 }
2123
Chris Lattner4bd21712010-09-15 04:33:27 +00002124 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002125 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002126 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002127 Name.startswith("shl") || Name.startswith("sal") ||
2128 Name.startswith("rcl") || Name.startswith("rcr") ||
2129 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002130 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002131 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002132 // Intel syntax
2133 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2134 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002135 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2136 delete Operands[2];
2137 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002138 }
2139 } else {
2140 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2141 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002142 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2143 delete Operands[1];
2144 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002145 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002146 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002147 }
Chad Rosier51afe632012-06-27 22:34:28 +00002148
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002149 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2150 // instalias with an immediate operand yet.
2151 if (Name == "int" && Operands.size() == 2) {
2152 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2153 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2154 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2155 delete Operands[1];
2156 Operands.erase(Operands.begin() + 1);
2157 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2158 }
2159 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002160
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002161 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002162}
2163
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002164static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2165 bool isCmp) {
2166 MCInst TmpInst;
2167 TmpInst.setOpcode(Opcode);
2168 if (!isCmp)
2169 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2170 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2171 TmpInst.addOperand(Inst.getOperand(0));
2172 Inst = TmpInst;
2173 return true;
2174}
2175
2176static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2177 bool isCmp = false) {
2178 if (!Inst.getOperand(0).isImm() ||
2179 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2180 return false;
2181
2182 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2183}
2184
2185static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2186 bool isCmp = false) {
2187 if (!Inst.getOperand(0).isImm() ||
2188 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2189 return false;
2190
2191 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2192}
2193
2194static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2195 bool isCmp = false) {
2196 if (!Inst.getOperand(0).isImm() ||
2197 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2198 return false;
2199
2200 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2201}
2202
Devang Patel4a6e7782012-01-12 18:03:40 +00002203bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002204processInstruction(MCInst &Inst,
2205 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2206 switch (Inst.getOpcode()) {
2207 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002208 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2209 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2210 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2211 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2212 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2213 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2214 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2215 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2216 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2217 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2218 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2219 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2220 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2221 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2222 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2223 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2224 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2225 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002226 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2227 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2228 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2229 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2230 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2231 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelde47cce2012-01-18 22:42:29 +00002232 }
Devang Patelde47cce2012-01-18 22:42:29 +00002233}
2234
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002235static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002236bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002237MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002238 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002239 MCStreamer &Out, unsigned &ErrorInfo,
2240 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002241 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002242 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2243 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00002244 ArrayRef<SMRange> EmptyRanges = None;
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002245
Chris Lattnera63292a2010-09-29 01:50:45 +00002246 // First, handle aliases that expand to multiple instructions.
2247 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002248 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002249 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002250 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002251 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002252 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002253 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002254 MCInst Inst;
2255 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002256 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002257 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002258 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002259
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002260 const char *Repl =
2261 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002262 .Case("finit", "fninit")
2263 .Case("fsave", "fnsave")
2264 .Case("fstcw", "fnstcw")
2265 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002266 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002267 .Case("fstsw", "fnstsw")
2268 .Case("fstsww", "fnstsw")
2269 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002270 .Default(0);
2271 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002272 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002273 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002274 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002275
Chris Lattner628fbec2010-09-06 21:54:15 +00002276 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002277 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002278
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002279 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002280 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002281 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002282 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002283 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002284 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002285 // Some instructions need post-processing to, for example, tweak which
2286 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002287 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002288 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002289 while (processInstruction(Inst, Operands))
2290 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002291
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002292 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002293 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002294 Out.EmitInstruction(Inst);
2295 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002296 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002297 case Match_MissingFeature: {
2298 assert(ErrorInfo && "Unknown missing feature!");
2299 // Special case the error message for the very common case where only
2300 // a single subtarget feature is missing.
2301 std::string Msg = "instruction requires:";
2302 unsigned Mask = 1;
2303 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2304 if (ErrorInfo & Mask) {
2305 Msg += " ";
2306 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2307 }
2308 Mask <<= 1;
2309 }
2310 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2311 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002312 case Match_InvalidOperand:
2313 WasOriginallyInvalidOperand = true;
2314 break;
2315 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002316 break;
2317 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002318
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002319 // FIXME: Ideally, we would only attempt suffix matches for things which are
2320 // valid prefixes, and we could just infer the right unambiguous
2321 // type. However, that requires substantially more matcher support than the
2322 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002323
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002324 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002325 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002326 SmallString<16> Tmp;
2327 Tmp += Base;
2328 Tmp += ' ';
2329 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002330
Chris Lattnerfab94132010-11-06 18:28:02 +00002331 // If this instruction starts with an 'f', then it is a floating point stack
2332 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2333 // 80-bit floating point, which use the suffixes s,l,t respectively.
2334 //
2335 // Otherwise, we assume that this may be an integer instruction, which comes
2336 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2337 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002338
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002339 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002340 Tmp[Base.size()] = Suffixes[0];
2341 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002342 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002343 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002344
Chad Rosier2f480a82012-10-12 22:53:36 +00002345 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002346 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002347 // If this returned as a missing feature failure, remember that.
2348 if (Match1 == Match_MissingFeature)
2349 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002350 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002351 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002352 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002353 // If this returned as a missing feature failure, remember that.
2354 if (Match2 == Match_MissingFeature)
2355 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002356 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002357 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002358 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002359 // If this returned as a missing feature failure, remember that.
2360 if (Match3 == Match_MissingFeature)
2361 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002362 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002363 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
Chad Rosierc8569cb2013-05-10 18:24:17 +00002364 MatchingInlineAsm, isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002365 // If this returned as a missing feature failure, remember that.
2366 if (Match4 == Match_MissingFeature)
2367 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002368
2369 // Restore the old token.
2370 Op->setTokenValue(Base);
2371
2372 // If exactly one matched, then we treat that as a successful match (and the
2373 // instruction will already have been filled in correctly, since the failing
2374 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002375 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002376 (Match1 == Match_Success) + (Match2 == Match_Success) +
2377 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002378 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002379 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002380 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002381 Out.EmitInstruction(Inst);
2382 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002383 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002384 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002385
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002386 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002387
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002388 // If we had multiple suffix matches, then identify this as an ambiguous
2389 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002390 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002391 char MatchChars[4];
2392 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002393 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2394 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2395 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2396 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002397
2398 SmallString<126> Msg;
2399 raw_svector_ostream OS(Msg);
2400 OS << "ambiguous instructions require an explicit suffix (could be ";
2401 for (unsigned i = 0; i != NumMatches; ++i) {
2402 if (i != 0)
2403 OS << ", ";
2404 if (i + 1 == NumMatches)
2405 OS << "or ";
2406 OS << "'" << Base << MatchChars[i] << "'";
2407 }
2408 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002409 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002410 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002411 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002412
Chris Lattner628fbec2010-09-06 21:54:15 +00002413 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002414
Chris Lattner628fbec2010-09-06 21:54:15 +00002415 // If all of the instructions reported an invalid mnemonic, then the original
2416 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002417 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2418 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002419 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002420 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002421 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002422 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002423 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002424 }
2425
2426 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002427 if (ErrorInfo != ~0U) {
2428 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002429 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002430 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002431
Chad Rosier49963552012-10-13 00:26:04 +00002432 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002433 if (Operand->getStartLoc().isValid()) {
2434 SMRange OperandRange = Operand->getLocRange();
2435 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002436 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002437 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002438 }
2439
Chad Rosier3d4bc622012-08-21 19:36:59 +00002440 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002441 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002442 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002443
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002444 // If one instruction matched with a missing feature, report this as a
2445 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002446 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2447 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002448 std::string Msg = "instruction requires:";
2449 unsigned Mask = 1;
2450 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2451 if (ErrorInfoMissingFeature & Mask) {
2452 Msg += " ";
2453 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2454 }
2455 Mask <<= 1;
2456 }
2457 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002458 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002459
Chris Lattner628fbec2010-09-06 21:54:15 +00002460 // If one instruction matched with an invalid operand, report this as an
2461 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002462 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2463 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002464 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002465 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002466 return true;
2467 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002468
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002469 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002470 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002471 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002472 return true;
2473}
2474
2475
Devang Patel4a6e7782012-01-12 18:03:40 +00002476bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002477 StringRef IDVal = DirectiveID.getIdentifier();
2478 if (IDVal == ".word")
2479 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002480 else if (IDVal.startswith(".code"))
2481 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002482 else if (IDVal.startswith(".att_syntax")) {
2483 getParser().setAssemblerDialect(0);
2484 return false;
2485 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002486 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002487 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2488 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002489 // FIXME : Handle noprefix
2490 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002491 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002492 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002493 }
2494 return false;
2495 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002496 return true;
2497}
2498
2499/// ParseDirectiveWord
2500/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002501bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002502 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2503 for (;;) {
2504 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002505 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002506 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002507
Eric Christopherbf7bc492013-01-09 03:52:05 +00002508 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002509
Chris Lattner72c0b592010-10-30 17:38:55 +00002510 if (getLexer().is(AsmToken::EndOfStatement))
2511 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002512
Chris Lattner72c0b592010-10-30 17:38:55 +00002513 // FIXME: Improve diagnostic.
2514 if (getLexer().isNot(AsmToken::Comma))
2515 return Error(L, "unexpected token in directive");
2516 Parser.Lex();
2517 }
2518 }
Chad Rosier51afe632012-06-27 22:34:28 +00002519
Chris Lattner72c0b592010-10-30 17:38:55 +00002520 Parser.Lex();
2521 return false;
2522}
2523
Evan Cheng481ebb02011-07-27 00:38:12 +00002524/// ParseDirectiveCode
2525/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002526bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002527 if (IDVal == ".code32") {
2528 Parser.Lex();
2529 if (is64BitMode()) {
2530 SwitchMode();
2531 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2532 }
2533 } else if (IDVal == ".code64") {
2534 Parser.Lex();
2535 if (!is64BitMode()) {
2536 SwitchMode();
2537 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2538 }
2539 } else {
2540 return Error(L, "unexpected directive " + IDVal);
2541 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002542
Evan Cheng481ebb02011-07-27 00:38:12 +00002543 return false;
2544}
Chris Lattner72c0b592010-10-30 17:38:55 +00002545
Daniel Dunbar71475772009-07-17 20:42:00 +00002546// Force static initialization.
2547extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002548 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2549 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002550}
Daniel Dunbar00331992009-07-29 00:02:19 +00002551
Chris Lattner3e4582a2010-09-06 19:11:01 +00002552#define GET_REGISTER_MATCHER
2553#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002554#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002555#include "X86GenAsmMatcher.inc"