blob: fef5cfe6212e5259308f28277572fdde78730bbc [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"
Chris Lattner1261b812010-09-22 04:11:10 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000016#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCParser/MCAsmLexer.h"
20#include "llvm/MC/MCParser/MCAsmParser.h"
21#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCStreamer.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000027#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000028#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000029#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000030
Daniel Dunbar71475772009-07-17 20:42:00 +000031using namespace llvm;
32
33namespace {
Benjamin Kramerb60210e2009-07-31 11:35:26 +000034struct X86Operand;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000035
Chad Rosier5362af92013-04-16 18:15:40 +000036static const char OpPrecedence[] = {
37 0, // IC_PLUS
38 0, // IC_MINUS
39 1, // IC_MULTIPLY
40 1, // IC_DIVIDE
41 2, // IC_RPAREN
42 3, // IC_LPAREN
43 0, // IC_IMM
44 0 // IC_REGISTER
45};
46
Devang Patel4a6e7782012-01-12 18:03:40 +000047class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000048 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000049 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000050 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000051private:
Chad Rosier5362af92013-04-16 18:15:40 +000052 enum InfixCalculatorTok {
53 IC_PLUS = 0,
54 IC_MINUS,
55 IC_MULTIPLY,
56 IC_DIVIDE,
57 IC_RPAREN,
58 IC_LPAREN,
59 IC_IMM,
60 IC_REGISTER
61 };
62
63 class InfixCalculator {
64 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
65 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
66 SmallVector<ICToken, 4> PostfixStack;
67
68 public:
69 int64_t popOperand() {
70 assert (!PostfixStack.empty() && "Poped an empty stack!");
71 ICToken Op = PostfixStack.pop_back_val();
72 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
73 && "Expected and immediate or register!");
74 return Op.second;
75 }
76 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
77 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
78 "Unexpected operand!");
79 PostfixStack.push_back(std::make_pair(Op, Val));
80 }
81
82 void popOperator() { InfixOperatorStack.pop_back_val(); }
83 void pushOperator(InfixCalculatorTok Op) {
84 // Push the new operator if the stack is empty.
85 if (InfixOperatorStack.empty()) {
86 InfixOperatorStack.push_back(Op);
87 return;
88 }
89
90 // Push the new operator if it has a higher precedence than the operator
91 // on the top of the stack or the operator on the top of the stack is a
92 // left parentheses.
93 unsigned Idx = InfixOperatorStack.size() - 1;
94 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
95 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
96 InfixOperatorStack.push_back(Op);
97 return;
98 }
99
100 // The operator on the top of the stack has higher precedence than the
101 // new operator.
102 unsigned ParenCount = 0;
103 while (1) {
104 // Nothing to process.
105 if (InfixOperatorStack.empty())
106 break;
107
108 Idx = InfixOperatorStack.size() - 1;
109 StackOp = InfixOperatorStack[Idx];
110 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
111 break;
112
113 // If we have an even parentheses count and we see a left parentheses,
114 // then stop processing.
115 if (!ParenCount && StackOp == IC_LPAREN)
116 break;
117
118 if (StackOp == IC_RPAREN) {
119 ++ParenCount;
120 InfixOperatorStack.pop_back_val();
121 } else if (StackOp == IC_LPAREN) {
122 --ParenCount;
123 InfixOperatorStack.pop_back_val();
124 } else {
125 InfixOperatorStack.pop_back_val();
126 PostfixStack.push_back(std::make_pair(StackOp, 0));
127 }
128 }
129 // Push the new operator.
130 InfixOperatorStack.push_back(Op);
131 }
132 int64_t execute() {
133 // Push any remaining operators onto the postfix stack.
134 while (!InfixOperatorStack.empty()) {
135 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
136 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
137 PostfixStack.push_back(std::make_pair(StackOp, 0));
138 }
139
140 if (PostfixStack.empty())
141 return 0;
142
143 SmallVector<ICToken, 16> OperandStack;
144 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
145 ICToken Op = PostfixStack[i];
146 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
147 OperandStack.push_back(Op);
148 } else {
149 assert (OperandStack.size() > 1 && "Too few operands.");
150 int64_t Val;
151 ICToken Op2 = OperandStack.pop_back_val();
152 ICToken Op1 = OperandStack.pop_back_val();
153 switch (Op.first) {
154 default:
155 report_fatal_error("Unexpected operator!");
156 break;
157 case IC_PLUS:
158 Val = Op1.second + Op2.second;
159 OperandStack.push_back(std::make_pair(IC_IMM, Val));
160 break;
161 case IC_MINUS:
162 Val = Op1.second - Op2.second;
163 OperandStack.push_back(std::make_pair(IC_IMM, Val));
164 break;
165 case IC_MULTIPLY:
166 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
167 "Multiply operation with an immediate and a register!");
168 Val = Op1.second * Op2.second;
169 OperandStack.push_back(std::make_pair(IC_IMM, Val));
170 break;
171 case IC_DIVIDE:
172 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
173 "Divide operation with an immediate and a register!");
174 assert (Op2.second != 0 && "Division by zero!");
175 Val = Op1.second / Op2.second;
176 OperandStack.push_back(std::make_pair(IC_IMM, Val));
177 break;
178 }
179 }
180 }
181 assert (OperandStack.size() == 1 && "Expected a single result.");
182 return OperandStack.pop_back_val().second;
183 }
184 };
185
186 enum IntelExprState {
187 IES_PLUS,
188 IES_MINUS,
189 IES_MULTIPLY,
190 IES_DIVIDE,
191 IES_LBRAC,
192 IES_RBRAC,
193 IES_LPAREN,
194 IES_RPAREN,
195 IES_REGISTER,
Chad Rosier5362af92013-04-16 18:15:40 +0000196 IES_INTEGER,
Chad Rosier5362af92013-04-16 18:15:40 +0000197 IES_IDENTIFIER,
198 IES_ERROR
199 };
200
201 class IntelExprStateMachine {
Chad Rosier31246272013-04-17 21:01:45 +0000202 IntelExprState State, PrevState;
Chad Rosier5362af92013-04-16 18:15:40 +0000203 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierbfb70992013-04-17 00:11:46 +0000204 int64_t Imm;
Chad Rosier5362af92013-04-16 18:15:40 +0000205 const MCExpr *Sym;
206 StringRef SymName;
Chad Rosierbfb70992013-04-17 00:11:46 +0000207 bool StopOnLBrac, AddImmPrefix;
Chad Rosier5362af92013-04-16 18:15:40 +0000208 InfixCalculator IC;
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000209 InlineAsmIdentifierInfo Info;
Chad Rosier5362af92013-04-16 18:15:40 +0000210 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000211 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000212 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
213 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000214 AddImmPrefix(addimmprefix) { Info.clear(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000215
216 unsigned getBaseReg() { return BaseReg; }
217 unsigned getIndexReg() { return IndexReg; }
218 unsigned getScale() { return Scale; }
219 const MCExpr *getSym() { return Sym; }
220 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000221 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000222 bool isValidEndState() { return State == IES_RBRAC; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000223 bool getStopOnLBrac() { return StopOnLBrac; }
224 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000225 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000226
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000227 InlineAsmIdentifierInfo &getIdentifierInfo() {
228 return Info;
229 }
230
Chad Rosier5362af92013-04-16 18:15:40 +0000231 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000232 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000233 switch (State) {
234 default:
235 State = IES_ERROR;
236 break;
237 case IES_INTEGER:
238 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000239 case IES_REGISTER:
240 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000241 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000242 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
243 // If we already have a BaseReg, then assume this is the IndexReg with
244 // a scale of 1.
245 if (!BaseReg) {
246 BaseReg = TmpReg;
247 } else {
248 assert (!IndexReg && "BaseReg/IndexReg already set!");
249 IndexReg = TmpReg;
250 Scale = 1;
251 }
252 }
Chad Rosier5362af92013-04-16 18:15:40 +0000253 break;
254 }
Chad Rosier31246272013-04-17 21:01:45 +0000255 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000256 }
257 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000258 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000259 switch (State) {
260 default:
261 State = IES_ERROR;
262 break;
263 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000264 case IES_MULTIPLY:
265 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000266 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000267 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000268 case IES_LBRAC:
269 case IES_RBRAC:
270 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000271 case IES_REGISTER:
272 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000273 // Only push the minus operator if it is not a unary operator.
274 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
275 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
276 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
277 IC.pushOperator(IC_MINUS);
278 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
279 // If we already have a BaseReg, then assume this is the IndexReg with
280 // a scale of 1.
281 if (!BaseReg) {
282 BaseReg = TmpReg;
283 } else {
284 assert (!IndexReg && "BaseReg/IndexReg already set!");
285 IndexReg = TmpReg;
286 Scale = 1;
287 }
Chad Rosier5362af92013-04-16 18:15:40 +0000288 }
Chad Rosier5362af92013-04-16 18:15:40 +0000289 break;
290 }
Chad Rosier31246272013-04-17 21:01:45 +0000291 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000292 }
293 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000294 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000295 switch (State) {
296 default:
297 State = IES_ERROR;
298 break;
299 case IES_PLUS:
300 case IES_LPAREN:
301 State = IES_REGISTER;
302 TmpReg = Reg;
303 IC.pushOperand(IC_REGISTER);
304 break;
Chad Rosier31246272013-04-17 21:01:45 +0000305 case IES_MULTIPLY:
306 // Index Register - Scale * Register
307 if (PrevState == IES_INTEGER) {
308 assert (!IndexReg && "IndexReg already set!");
309 State = IES_REGISTER;
310 IndexReg = Reg;
311 // Get the scale and replace the 'Scale * Register' with '0'.
312 Scale = IC.popOperand();
313 IC.pushOperand(IC_IMM);
314 IC.popOperator();
315 } else {
316 State = IES_ERROR;
317 }
Chad Rosier5362af92013-04-16 18:15:40 +0000318 break;
319 }
Chad Rosier31246272013-04-17 21:01:45 +0000320 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000321 }
Chad Rosier95ce8892013-04-19 18:39:50 +0000322 void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000323 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000324 switch (State) {
325 default:
326 State = IES_ERROR;
327 break;
328 case IES_PLUS:
329 case IES_MINUS:
330 State = IES_INTEGER;
331 Sym = SymRef;
332 SymName = SymRefName;
333 IC.pushOperand(IC_IMM);
334 break;
335 }
336 }
337 void onInteger(int64_t TmpInt) {
Chad Rosier31246272013-04-17 21:01:45 +0000338 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000339 switch (State) {
340 default:
341 State = IES_ERROR;
342 break;
343 case IES_PLUS:
344 case IES_MINUS:
Chad Rosier5362af92013-04-16 18:15:40 +0000345 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000346 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000347 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000348 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000349 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
350 // Index Register - Register * Scale
351 assert (!IndexReg && "IndexReg already set!");
352 IndexReg = TmpReg;
353 Scale = TmpInt;
354 // Get the scale and replace the 'Register * Scale' with '0'.
355 IC.popOperator();
356 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
357 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
358 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
359 CurrState == IES_MINUS) {
360 // Unary minus. No need to pop the minus operand because it was never
361 // pushed.
362 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
363 } else {
364 IC.pushOperand(IC_IMM, TmpInt);
365 }
Chad Rosier5362af92013-04-16 18:15:40 +0000366 break;
367 }
Chad Rosier31246272013-04-17 21:01:45 +0000368 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000369 }
370 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000371 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000372 switch (State) {
373 default:
374 State = IES_ERROR;
375 break;
376 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000377 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000378 case IES_RPAREN:
379 State = IES_MULTIPLY;
380 IC.pushOperator(IC_MULTIPLY);
381 break;
382 }
383 }
384 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000385 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000386 switch (State) {
387 default:
388 State = IES_ERROR;
389 break;
390 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000391 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000392 State = IES_DIVIDE;
393 IC.pushOperator(IC_DIVIDE);
394 break;
395 }
396 }
397 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000398 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000399 switch (State) {
400 default:
401 State = IES_ERROR;
402 break;
403 case IES_RBRAC:
404 State = IES_PLUS;
405 IC.pushOperator(IC_PLUS);
406 break;
407 }
408 }
409 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000410 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000411 switch (State) {
412 default:
413 State = IES_ERROR;
414 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000415 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000416 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000417 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000418 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000419 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
420 // If we already have a BaseReg, then assume this is the IndexReg with
421 // a scale of 1.
422 if (!BaseReg) {
423 BaseReg = TmpReg;
424 } else {
425 assert (!IndexReg && "BaseReg/IndexReg already set!");
426 IndexReg = TmpReg;
427 Scale = 1;
428 }
Chad Rosier5362af92013-04-16 18:15:40 +0000429 }
430 break;
431 }
Chad Rosier31246272013-04-17 21:01:45 +0000432 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000433 }
434 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000435 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000436 switch (State) {
437 default:
438 State = IES_ERROR;
439 break;
440 case IES_PLUS:
441 case IES_MINUS:
442 case IES_MULTIPLY:
443 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000444 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000445 // FIXME: We don't handle this type of unary minus, yet.
446 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
447 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
448 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
449 CurrState == IES_MINUS) {
450 State = IES_ERROR;
451 break;
452 }
Chad Rosier5362af92013-04-16 18:15:40 +0000453 State = IES_LPAREN;
454 IC.pushOperator(IC_LPAREN);
455 break;
456 }
Chad Rosier31246272013-04-17 21:01:45 +0000457 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000458 }
459 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000460 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000461 switch (State) {
462 default:
463 State = IES_ERROR;
464 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000465 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000466 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000467 case IES_RPAREN:
468 State = IES_RPAREN;
469 IC.pushOperator(IC_RPAREN);
470 break;
471 }
472 }
473 };
474
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000475 MCAsmParser &getParser() const { return Parser; }
476
477 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
478
Chris Lattnera3a06812011-10-16 04:47:35 +0000479 bool Error(SMLoc L, const Twine &Msg,
Chad Rosier3d4bc622012-08-21 19:36:59 +0000480 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier4453e842012-10-12 23:09:25 +0000481 bool MatchingInlineAsm = false) {
482 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000483 return Parser.Error(L, Msg, Ranges);
484 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000485
Devang Patel41b9dde2012-01-17 18:00:18 +0000486 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
487 Error(Loc, Msg);
488 return 0;
489 }
490
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000491 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000492 X86Operand *ParseATTOperand();
493 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000494 X86Operand *ParseIntelOffsetOfOperator();
Chad Rosiercc541e82013-04-19 15:57:00 +0000495 X86Operand *ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000496 X86Operand *ParseIntelOperator(unsigned OpKind);
Chad Rosier6241c1a2013-04-17 21:14:38 +0000497 X86Operand *ParseIntelMemOperand(unsigned SegReg, int64_t ImmDisp,
Chad Rosier1530ba52013-03-27 21:49:56 +0000498 SMLoc StartLoc);
Chad Rosier5362af92013-04-16 18:15:40 +0000499 X86Operand *ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000500 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000501 int64_t ImmDisp, unsigned Size);
Chad Rosier95ce8892013-04-19 18:39:50 +0000502 X86Operand *ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000503 InlineAsmIdentifierInfo &Info, SMLoc &End);
504
Chris Lattnerb9270732010-04-17 18:56:34 +0000505 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000506
Chad Rosier175d0ae2013-04-12 18:21:18 +0000507 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
508 unsigned BaseReg, unsigned IndexReg,
509 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +0000510 unsigned Size, StringRef Identifier,
511 InlineAsmIdentifierInfo &Info);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000512
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000513 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000514 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000515
Devang Patelde47cce2012-01-18 22:42:29 +0000516 bool processInstruction(MCInst &Inst,
517 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
518
Chad Rosier49963552012-10-13 00:26:04 +0000519 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000520 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000521 MCStreamer &Out, unsigned &ErrorInfo,
522 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000523
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000524 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000525 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000526 bool isSrcOp(X86Operand &Op);
527
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000528 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
529 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000530 bool isDstOp(X86Operand &Op);
531
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000532 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000533 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000534 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000535 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000536 void SwitchMode() {
537 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
538 setAvailableFeatures(FB);
539 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000540
Chad Rosierc2f055d2013-04-18 16:13:18 +0000541 bool isParsingIntelSyntax() {
542 return getParser().getAssemblerDialect();
543 }
544
Daniel Dunbareefe8612010-07-19 05:44:09 +0000545 /// @name Auto-generated Matcher Functions
546 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000547
Chris Lattner3e4582a2010-09-06 19:11:01 +0000548#define GET_ASSEMBLER_HEADER
549#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000550
Daniel Dunbar00331992009-07-29 00:02:19 +0000551 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000552
553public:
Devang Patel4a6e7782012-01-12 18:03:40 +0000554 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosierf0e87202012-10-25 20:41:34 +0000555 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000556
Daniel Dunbareefe8612010-07-19 05:44:09 +0000557 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000558 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000559 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000560 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000561
Chad Rosierf0e87202012-10-25 20:41:34 +0000562 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
563 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000564 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000565
566 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000567};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000568} // end anonymous namespace
569
Sean Callanan86c11812010-01-23 00:40:33 +0000570/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000571/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000572
Chris Lattner60db0a62010-02-09 00:34:28 +0000573static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000574
575/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000576
Craig Topper6bf3ed42012-07-18 04:59:16 +0000577static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000578 return (( Value <= 0x000000000000007FULL)||
579 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
580 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
581}
582
583static bool isImmSExti32i8Value(uint64_t Value) {
584 return (( Value <= 0x000000000000007FULL)||
585 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
586 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
587}
588
589static bool isImmZExtu32u8Value(uint64_t Value) {
590 return (Value <= 0x00000000000000FFULL);
591}
592
593static bool isImmSExti64i8Value(uint64_t Value) {
594 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000595 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000596}
597
598static bool isImmSExti64i32Value(uint64_t Value) {
599 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000600 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000601}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000602namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000603
604/// X86Operand - Instances of this class represent a parsed X86 machine
605/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000606struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000607 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000608 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000609 Register,
610 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000611 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000612 } Kind;
613
Chris Lattner0c2538f2010-01-15 18:51:29 +0000614 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000615 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000616 StringRef SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000617 void *OpDecl;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000618 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000619
Eric Christopher8996c5d2013-03-15 00:42:55 +0000620 struct TokOp {
621 const char *Data;
622 unsigned Length;
623 };
624
625 struct RegOp {
626 unsigned RegNo;
627 };
628
629 struct ImmOp {
630 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000631 };
632
633 struct MemOp {
634 unsigned SegReg;
635 const MCExpr *Disp;
636 unsigned BaseReg;
637 unsigned IndexReg;
638 unsigned Scale;
639 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000640 };
641
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000642 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000643 struct TokOp Tok;
644 struct RegOp Reg;
645 struct ImmOp Imm;
646 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000647 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000648
Chris Lattner015cfb12010-01-15 19:33:43 +0000649 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000650 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000651
Chad Rosiere81309b2013-04-09 17:53:49 +0000652 StringRef getSymName() { return SymName; }
Chad Rosier732b8372013-04-22 22:04:25 +0000653 void *getOpDecl() { return OpDecl; }
Chad Rosiere81309b2013-04-09 17:53:49 +0000654
Chris Lattner86e61532010-01-15 19:06:59 +0000655 /// getStartLoc - Get the location of the first token of this operand.
656 SMLoc getStartLoc() const { return StartLoc; }
657 /// getEndLoc - Get the location of the last token of this operand.
658 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000659 /// getLocRange - Get the range between the first and last token of this
660 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000661 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000662 /// getOffsetOfLoc - Get the location of the offset operator.
663 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000664
Jim Grosbach602aa902011-07-13 15:34:57 +0000665 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000666
Daniel Dunbare10787e2009-08-07 08:26:05 +0000667 StringRef getToken() const {
668 assert(Kind == Token && "Invalid access!");
669 return StringRef(Tok.Data, Tok.Length);
670 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000671 void setTokenValue(StringRef Value) {
672 assert(Kind == Token && "Invalid access!");
673 Tok.Data = Value.data();
674 Tok.Length = Value.size();
675 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000676
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000677 unsigned getReg() const {
678 assert(Kind == Register && "Invalid access!");
679 return Reg.RegNo;
680 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000681
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000682 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000683 assert(Kind == Immediate && "Invalid access!");
684 return Imm.Val;
685 }
686
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000687 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000688 assert(Kind == Memory && "Invalid access!");
689 return Mem.Disp;
690 }
691 unsigned getMemSegReg() const {
692 assert(Kind == Memory && "Invalid access!");
693 return Mem.SegReg;
694 }
695 unsigned getMemBaseReg() const {
696 assert(Kind == Memory && "Invalid access!");
697 return Mem.BaseReg;
698 }
699 unsigned getMemIndexReg() const {
700 assert(Kind == Memory && "Invalid access!");
701 return Mem.IndexReg;
702 }
703 unsigned getMemScale() const {
704 assert(Kind == Memory && "Invalid access!");
705 return Mem.Scale;
706 }
707
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000708 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000709
710 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000711
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000712 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000713 if (!isImm())
714 return false;
715
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000716 // If this isn't a constant expr, just assume it fits and let relaxation
717 // handle it.
718 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
719 if (!CE)
720 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000721
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000722 // Otherwise, check the value is in a range that makes sense for this
723 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000724 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000725 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000726 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000727 if (!isImm())
728 return false;
729
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000730 // If this isn't a constant expr, just assume it fits and let relaxation
731 // handle it.
732 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
733 if (!CE)
734 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000735
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000736 // Otherwise, check the value is in a range that makes sense for this
737 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000738 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000739 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000740 bool isImmZExtu32u8() const {
741 if (!isImm())
742 return false;
743
744 // If this isn't a constant expr, just assume it fits and let relaxation
745 // handle it.
746 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
747 if (!CE)
748 return true;
749
750 // Otherwise, check the value is in a range that makes sense for this
751 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000752 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000753 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000754 bool isImmSExti64i8() const {
755 if (!isImm())
756 return false;
757
758 // If this isn't a constant expr, just assume it fits and let relaxation
759 // handle it.
760 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
761 if (!CE)
762 return true;
763
764 // Otherwise, check the value is in a range that makes sense for this
765 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000766 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000767 }
768 bool isImmSExti64i32() const {
769 if (!isImm())
770 return false;
771
772 // If this isn't a constant expr, just assume it fits and let relaxation
773 // handle it.
774 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
775 if (!CE)
776 return true;
777
778 // Otherwise, check the value is in a range that makes sense for this
779 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000780 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000781 }
782
Chad Rosier5bca3f92012-10-22 19:50:35 +0000783 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000784 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000785 }
786
Chad Rosiera4bc9432013-01-10 22:10:27 +0000787 bool needAddressOf() const {
788 return AddressOf;
789 }
790
Daniel Dunbare10787e2009-08-07 08:26:05 +0000791 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000792 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000793 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000794 }
Chad Rosier51afe632012-06-27 22:34:28 +0000795 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000796 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000797 }
Chad Rosier51afe632012-06-27 22:34:28 +0000798 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000799 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000800 }
Chad Rosier51afe632012-06-27 22:34:28 +0000801 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000802 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000803 }
Chad Rosier51afe632012-06-27 22:34:28 +0000804 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000805 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000806 }
Chad Rosier51afe632012-06-27 22:34:28 +0000807 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000808 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000809 }
Chad Rosier51afe632012-06-27 22:34:28 +0000810 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000811 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000812 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000813
Craig Topper01deb5f2012-07-18 04:11:12 +0000814 bool isMemVX32() const {
815 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
816 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
817 }
818 bool isMemVY32() const {
819 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
820 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
821 }
822 bool isMemVX64() const {
823 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
824 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
825 }
826 bool isMemVY64() const {
827 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
828 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
829 }
830
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000831 bool isAbsMem() const {
832 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000833 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000834 }
835
Daniel Dunbare10787e2009-08-07 08:26:05 +0000836 bool isReg() const { return Kind == Register; }
837
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000838 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
839 // Add as immediates when possible.
840 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
841 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
842 else
843 Inst.addOperand(MCOperand::CreateExpr(Expr));
844 }
845
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000846 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000847 assert(N == 1 && "Invalid number of operands!");
848 Inst.addOperand(MCOperand::CreateReg(getReg()));
849 }
850
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000851 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000852 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000853 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000854 }
855
Chad Rosier51afe632012-06-27 22:34:28 +0000856 void addMem8Operands(MCInst &Inst, unsigned N) const {
857 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000858 }
Chad Rosier51afe632012-06-27 22:34:28 +0000859 void addMem16Operands(MCInst &Inst, unsigned N) const {
860 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000861 }
Chad Rosier51afe632012-06-27 22:34:28 +0000862 void addMem32Operands(MCInst &Inst, unsigned N) const {
863 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000864 }
Chad Rosier51afe632012-06-27 22:34:28 +0000865 void addMem64Operands(MCInst &Inst, unsigned N) const {
866 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000867 }
Chad Rosier51afe632012-06-27 22:34:28 +0000868 void addMem80Operands(MCInst &Inst, unsigned N) const {
869 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000870 }
Chad Rosier51afe632012-06-27 22:34:28 +0000871 void addMem128Operands(MCInst &Inst, unsigned N) const {
872 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000873 }
Chad Rosier51afe632012-06-27 22:34:28 +0000874 void addMem256Operands(MCInst &Inst, unsigned N) const {
875 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000876 }
Craig Topper01deb5f2012-07-18 04:11:12 +0000877 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
878 addMemOperands(Inst, N);
879 }
880 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
881 addMemOperands(Inst, N);
882 }
883 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
884 addMemOperands(Inst, N);
885 }
886 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
887 addMemOperands(Inst, N);
888 }
Devang Patelfc6be102012-01-12 01:51:42 +0000889
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000890 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000891 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000892 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
893 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
894 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000895 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000896 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
897 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000898
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000899 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
900 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000901 // Add as immediates when possible.
902 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
903 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
904 else
905 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000906 }
907
Chris Lattner528d00b2010-01-15 19:28:38 +0000908 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000909 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000910 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000911 Res->Tok.Data = Str.data();
912 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000913 return Res;
914 }
915
Chad Rosier91c82662012-10-24 17:22:29 +0000916 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000917 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000918 SMLoc OffsetOfLoc = SMLoc(),
Chad Rosier732b8372013-04-22 22:04:25 +0000919 StringRef SymName = StringRef(),
920 void *OpDecl = 0) {
Chris Lattner86e61532010-01-15 19:06:59 +0000921 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000922 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000923 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000924 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000925 Res->SymName = SymName;
Chad Rosier732b8372013-04-22 22:04:25 +0000926 Res->OpDecl = OpDecl;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000927 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000928 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000929
Chad Rosierf3c04f62013-03-19 21:58:18 +0000930 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000931 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000932 Res->Imm.Val = Val;
933 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000934 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000935
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000936 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000937 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier732b8372013-04-22 22:04:25 +0000938 unsigned Size = 0, StringRef SymName = StringRef(),
939 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000940 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
941 Res->Mem.SegReg = 0;
942 Res->Mem.Disp = Disp;
943 Res->Mem.BaseReg = 0;
944 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000945 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000946 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +0000947 Res->SymName = SymName;
948 Res->OpDecl = OpDecl;
949 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000950 return Res;
951 }
952
953 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000954 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
955 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +0000956 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000957 unsigned Size = 0,
Chad Rosier732b8372013-04-22 22:04:25 +0000958 StringRef SymName = StringRef(),
959 void *OpDecl = 0) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000960 // We should never just have a displacement, that should be parsed as an
961 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000962 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
963
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000964 // The scale should always be one of {1,2,4,8}.
965 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000966 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000967 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000968 Res->Mem.SegReg = SegReg;
969 Res->Mem.Disp = Disp;
970 Res->Mem.BaseReg = BaseReg;
971 Res->Mem.IndexReg = IndexReg;
972 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +0000973 Res->Mem.Size = Size;
Chad Rosier732b8372013-04-22 22:04:25 +0000974 Res->SymName = SymName;
975 Res->OpDecl = OpDecl;
976 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000977 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000978 }
979};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000980
Chris Lattner4eb9df02009-07-29 06:33:53 +0000981} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000982
Devang Patel4a6e7782012-01-12 18:03:40 +0000983bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000984 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000985
986 return (Op.isMem() &&
987 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
988 isa<MCConstantExpr>(Op.Mem.Disp) &&
989 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
990 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
991}
992
Devang Patel4a6e7782012-01-12 18:03:40 +0000993bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000994 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000995
Chad Rosier51afe632012-06-27 22:34:28 +0000996 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000997 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000998 isa<MCConstantExpr>(Op.Mem.Disp) &&
999 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1000 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
1001}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001002
Devang Patel4a6e7782012-01-12 18:03:40 +00001003bool X86AsmParser::ParseRegister(unsigned &RegNo,
1004 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +00001005 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +00001006 const AsmToken &PercentTok = Parser.getTok();
1007 StartLoc = PercentTok.getLoc();
1008
1009 // If we encounter a %, ignore it. This code handles registers with and
1010 // without the prefix, unprefixed registers can occur in cfi directives.
1011 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +00001012 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +00001013
Sean Callanan936b0d32010-01-19 21:44:56 +00001014 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001015 EndLoc = Tok.getEndLoc();
1016
Devang Patelce6a2ca2012-01-20 22:32:05 +00001017 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001018 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001019 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001020 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001021 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001022
Kevin Enderby7d912182009-09-03 17:15:07 +00001023 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001024
Chris Lattner1261b812010-09-22 04:11:10 +00001025 // If the match failed, try the register name as lowercase.
1026 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001027 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001028
Evan Chengeda1d4f2011-07-27 23:22:03 +00001029 if (!is64BitMode()) {
1030 // FIXME: This should be done using Requires<In32BitMode> and
1031 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1032 // checked.
1033 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1034 // REX prefix.
1035 if (RegNo == X86::RIZ ||
1036 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1037 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1038 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001039 return Error(StartLoc, "register %"
1040 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001041 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001042 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001043
Chris Lattner1261b812010-09-22 04:11:10 +00001044 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1045 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001046 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001047 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001048
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001049 // Check to see if we have '(4)' after %st.
1050 if (getLexer().isNot(AsmToken::LParen))
1051 return false;
1052 // Lex the paren.
1053 getParser().Lex();
1054
1055 const AsmToken &IntTok = Parser.getTok();
1056 if (IntTok.isNot(AsmToken::Integer))
1057 return Error(IntTok.getLoc(), "expected stack index");
1058 switch (IntTok.getIntVal()) {
1059 case 0: RegNo = X86::ST0; break;
1060 case 1: RegNo = X86::ST1; break;
1061 case 2: RegNo = X86::ST2; break;
1062 case 3: RegNo = X86::ST3; break;
1063 case 4: RegNo = X86::ST4; break;
1064 case 5: RegNo = X86::ST5; break;
1065 case 6: RegNo = X86::ST6; break;
1066 case 7: RegNo = X86::ST7; break;
1067 default: return Error(IntTok.getLoc(), "invalid stack index");
1068 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001069
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001070 if (getParser().Lex().isNot(AsmToken::RParen))
1071 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001072
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001073 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001074 Parser.Lex(); // Eat ')'
1075 return false;
1076 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001077
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001078 EndLoc = Parser.getTok().getEndLoc();
1079
Chris Lattner80486622010-06-24 07:29:18 +00001080 // If this is "db[0-7]", match it as an alias
1081 // for dr[0-7].
1082 if (RegNo == 0 && Tok.getString().size() == 3 &&
1083 Tok.getString().startswith("db")) {
1084 switch (Tok.getString()[2]) {
1085 case '0': RegNo = X86::DR0; break;
1086 case '1': RegNo = X86::DR1; break;
1087 case '2': RegNo = X86::DR2; break;
1088 case '3': RegNo = X86::DR3; break;
1089 case '4': RegNo = X86::DR4; break;
1090 case '5': RegNo = X86::DR5; break;
1091 case '6': RegNo = X86::DR6; break;
1092 case '7': RegNo = X86::DR7; break;
1093 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001094
Chris Lattner80486622010-06-24 07:29:18 +00001095 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001096 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001097 Parser.Lex(); // Eat it.
1098 return false;
1099 }
1100 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001101
Devang Patelce6a2ca2012-01-20 22:32:05 +00001102 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001103 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001104 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001105 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001106 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001107
Sean Callanana83fd7d2010-01-19 20:27:46 +00001108 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001109 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001110}
1111
Devang Patel4a6e7782012-01-12 18:03:40 +00001112X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001113 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001114 return ParseIntelOperand();
1115 return ParseATTOperand();
1116}
1117
Devang Patel41b9dde2012-01-17 18:00:18 +00001118/// getIntelMemOperandSize - Return intel memory operand size.
1119static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001120 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001121 .Cases("BYTE", "byte", 8)
1122 .Cases("WORD", "word", 16)
1123 .Cases("DWORD", "dword", 32)
1124 .Cases("QWORD", "qword", 64)
1125 .Cases("XWORD", "xword", 80)
1126 .Cases("XMMWORD", "xmmword", 128)
1127 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001128 .Default(0);
1129 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001130}
1131
Chad Rosier175d0ae2013-04-12 18:21:18 +00001132X86Operand *
1133X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1134 unsigned BaseReg, unsigned IndexReg,
1135 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001136 unsigned Size, StringRef Identifier,
1137 InlineAsmIdentifierInfo &Info){
Chad Rosier65dd0392013-04-22 22:38:35 +00001138 if (isa<MCSymbolRefExpr>(Disp)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001139 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1140 // reference. We need an 'r' constraint here, so we need to create register
1141 // operand to ensure proper matching. Just pick a GPR based on the size of
1142 // a pointer.
Chad Rosierf6675c32013-04-22 17:01:46 +00001143 if (!Info.IsVarDecl) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001144 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1145 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001146 SMLoc(), Identifier, Info.OpDecl);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001147 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001148 if (!Size) {
1149 Size = Info.Type * 8; // Size is in terms of bits in this context.
1150 if (Size)
1151 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1152 /*Len=*/0, Size));
1153 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001154 }
1155
Chad Rosier7ca135b2013-03-19 21:11:56 +00001156 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001157 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001158 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001159 BaseReg = BaseReg ? BaseReg : 1;
1160 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosier732b8372013-04-22 22:04:25 +00001161 End, Size, Identifier, Info.OpDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001162}
1163
Chad Rosierd383db52013-04-12 20:20:54 +00001164static void
1165RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1166 StringRef SymName, int64_t ImmDisp,
1167 int64_t FinalImmDisp, SMLoc &BracLoc,
1168 SMLoc &StartInBrac, SMLoc &End) {
1169 // Remove the '[' and ']' from the IR string.
1170 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1171 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1172
1173 // If ImmDisp is non-zero, then we parsed a displacement before the
1174 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1175 // If ImmDisp doesn't match the displacement computed by the state machine
1176 // then we have an additional displacement in the bracketed expression.
1177 if (ImmDisp != FinalImmDisp) {
1178 if (ImmDisp) {
1179 // We have an immediate displacement before the bracketed expression.
1180 // Adjust this to match the final immediate displacement.
1181 bool Found = false;
1182 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1183 E = AsmRewrites->end(); I != E; ++I) {
1184 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1185 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001186 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1187 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001188 (*I).Kind = AOK_Imm;
1189 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1190 (*I).Val = FinalImmDisp;
1191 Found = true;
1192 break;
1193 }
1194 }
1195 assert (Found && "Unable to rewrite ImmDisp.");
1196 } else {
1197 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001198 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001199 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001200 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001201 }
1202 }
1203 // Remove all the ImmPrefix rewrites within the brackets.
1204 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1205 E = AsmRewrites->end(); I != E; ++I) {
1206 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1207 continue;
1208 if ((*I).Kind == AOK_ImmPrefix)
1209 (*I).Kind = AOK_Delete;
1210 }
1211 const char *SymLocPtr = SymName.data();
1212 // Skip everything before the symbol.
1213 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1214 assert(Len > 0 && "Expected a non-negative length.");
1215 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1216 }
1217 // Skip everything after the symbol.
1218 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1219 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1220 assert(Len > 0 && "Expected a non-negative length.");
1221 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1222 }
1223}
1224
Chad Rosier5362af92013-04-16 18:15:40 +00001225X86Operand *
1226X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001227 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001228
Chad Rosier5c118fd2013-01-14 22:31:35 +00001229 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001230 while (!Done) {
1231 bool UpdateLocLex = true;
1232
1233 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1234 // identifier. Don't try an parse it as a register.
1235 if (Tok.getString().startswith("."))
1236 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001237
1238 // If we're parsing an immediate expression, we don't expect a '['.
1239 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1240 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001241
1242 switch (getLexer().getKind()) {
1243 default: {
1244 if (SM.isValidEndState()) {
1245 Done = true;
1246 break;
1247 }
1248 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1249 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001250 case AsmToken::EndOfStatement: {
1251 Done = true;
1252 break;
1253 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001254 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001255 // This could be a register or a symbolic displacement.
1256 unsigned TmpReg;
Chad Rosier95ce8892013-04-19 18:39:50 +00001257 const MCExpr *Val;
Chad Rosier152749c2013-04-12 18:54:20 +00001258 SMLoc IdentLoc = Tok.getLoc();
1259 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001260 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001261 SM.onRegister(TmpReg);
1262 UpdateLocLex = false;
1263 break;
Chad Rosier95ce8892013-04-19 18:39:50 +00001264 } else {
1265 if (!isParsingInlineAsm()) {
1266 if (getParser().parsePrimaryExpr(Val, End))
1267 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1268 } else {
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001269 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1270 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info, End))
Chad Rosier95ce8892013-04-19 18:39:50 +00001271 return Err;
1272 }
1273 SM.onIdentifierExpr(Val, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001274 UpdateLocLex = false;
1275 break;
1276 }
1277 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1278 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001279 case AsmToken::Integer:
Chad Rosierbfb70992013-04-17 00:11:46 +00001280 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001281 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1282 Tok.getLoc()));
1283 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001284 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001285 case AsmToken::Plus: SM.onPlus(); break;
1286 case AsmToken::Minus: SM.onMinus(); break;
1287 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001288 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001289 case AsmToken::LBrac: SM.onLBrac(); break;
1290 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001291 case AsmToken::LParen: SM.onLParen(); break;
1292 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001293 }
Chad Rosier31246272013-04-17 21:01:45 +00001294 if (SM.hadError())
1295 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1296
Chad Rosier5c118fd2013-01-14 22:31:35 +00001297 if (!Done && UpdateLocLex) {
1298 End = Tok.getLoc();
1299 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001300 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001301 }
Chad Rosier5362af92013-04-16 18:15:40 +00001302 return 0;
1303}
1304
1305X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001306 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001307 unsigned Size) {
1308 const AsmToken &Tok = Parser.getTok();
1309 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1310 if (getLexer().isNot(AsmToken::LBrac))
1311 return ErrorOperand(BracLoc, "Expected '[' token!");
1312 Parser.Lex(); // Eat '['
1313
1314 SMLoc StartInBrac = Tok.getLoc();
1315 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1316 // may have already parsed an immediate displacement before the bracketed
1317 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001318 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Chad Rosier5362af92013-04-16 18:15:40 +00001319 if (X86Operand *Err = ParseIntelExpression(SM, End))
1320 return Err;
Devang Patel41b9dde2012-01-17 18:00:18 +00001321
Chad Rosier175d0ae2013-04-12 18:21:18 +00001322 const MCExpr *Disp;
1323 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001324 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001325 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001326 if (isParsingInlineAsm())
1327 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001328 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001329 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001330 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001331 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001332 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001333 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001334
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001335 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001336 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001337 const MCExpr *NewDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001338 if (X86Operand *Err = ParseIntelDotOperator(Disp, NewDisp))
1339 return Err;
Chad Rosier911c1f32012-10-25 17:37:43 +00001340
Chad Rosier70f47592013-04-10 20:07:47 +00001341 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001342 Parser.Lex(); // Eat the field.
1343 Disp = NewDisp;
1344 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001345
Chad Rosier5c118fd2013-01-14 22:31:35 +00001346 int BaseReg = SM.getBaseReg();
1347 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001348 int Scale = SM.getScale();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001349 if (!isParsingInlineAsm()) {
1350 // handle [-42]
1351 if (!BaseReg && !IndexReg) {
1352 if (!SegReg)
1353 return X86Operand::CreateMem(Disp, Start, End, Size);
1354 else
1355 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1356 }
1357 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1358 End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001359 }
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001360
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001361 InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
Chad Rosiere8f9bfd2013-04-19 19:29:50 +00001362 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001363 End, Size, SM.getSymName(), Info);
Devang Patel41b9dde2012-01-17 18:00:18 +00001364}
1365
Chad Rosier8a244662013-04-02 20:02:33 +00001366// Inline assembly may use variable names with namespace alias qualifiers.
Chad Rosier95ce8892013-04-19 18:39:50 +00001367X86Operand *X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1368 StringRef &Identifier,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001369 InlineAsmIdentifierInfo &Info,
Chad Rosier95ce8892013-04-19 18:39:50 +00001370 SMLoc &End) {
1371 assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1372 Val = 0;
Chad Rosier8a244662013-04-02 20:02:33 +00001373
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001374 StringRef LineBuf(Identifier.data());
1375 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info);
1376 unsigned BufLen = LineBuf.size();
1377 assert (BufLen && "Expected a non-zero length identifier.");
1378
1379 // Advance the token stream based on what the frontend parsed.
Chad Rosier8a244662013-04-02 20:02:33 +00001380 const AsmToken &Tok = Parser.getTok();
Chad Rosierce031892013-04-11 23:24:15 +00001381 AsmToken IdentEnd = Tok;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001382 while (BufLen > 0) {
1383 IdentEnd = Tok;
1384 BufLen -= Tok.getString().size();
1385 getLexer().Lex(); // Consume the token.
Chad Rosier8a244662013-04-02 20:02:33 +00001386 }
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001387 if (BufLen != 0)
1388 return ErrorOperand(IdentEnd.getLoc(),
1389 "Frontend parser mismatch with asm lexer!");
Chad Rosierf6675c32013-04-22 17:01:46 +00001390 End = IdentEnd.getEndLoc();
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001391
1392 // Create the symbol reference.
1393 Identifier = LineBuf;
Chad Rosier8a244662013-04-02 20:02:33 +00001394 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1395 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Chad Rosier95ce8892013-04-19 18:39:50 +00001396 Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
Chad Rosier8a244662013-04-02 20:02:33 +00001397 return 0;
1398}
1399
Devang Patel41b9dde2012-01-17 18:00:18 +00001400/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001401X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001402 int64_t ImmDisp,
Chad Rosier1530ba52013-03-27 21:49:56 +00001403 SMLoc Start) {
Devang Patel41b9dde2012-01-17 18:00:18 +00001404 const AsmToken &Tok = Parser.getTok();
Chad Rosier91c82662012-10-24 17:22:29 +00001405 SMLoc End;
Devang Patel41b9dde2012-01-17 18:00:18 +00001406
1407 unsigned Size = getIntelMemOperandSize(Tok.getString());
1408 if (Size) {
Chad Rosier103fe732013-04-19 17:31:39 +00001409 Parser.Lex(); // Eat operand size (e.g., byte, word).
1410 if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1411 return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1412 Parser.Lex(); // Eat ptr.
Devang Patel41b9dde2012-01-17 18:00:18 +00001413 }
1414
Chad Rosier1530ba52013-03-27 21:49:56 +00001415 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1416 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier1530ba52013-03-27 21:49:56 +00001417 if (isParsingInlineAsm())
1418 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
Chad Rosier70f47592013-04-10 20:07:47 +00001419 Tok.getLoc()));
Chad Rosier6241c1a2013-04-17 21:14:38 +00001420 int64_t ImmDisp = Tok.getIntVal();
Chad Rosier1530ba52013-03-27 21:49:56 +00001421 Parser.Lex(); // Eat the integer.
1422 if (getLexer().isNot(AsmToken::LBrac))
1423 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001424 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Chad Rosier1530ba52013-03-27 21:49:56 +00001425 }
1426
Chad Rosier91c82662012-10-24 17:22:29 +00001427 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001428 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001429
1430 if (!ParseRegister(SegReg, Start, End)) {
1431 // Handel SegReg : [ ... ]
1432 if (getLexer().isNot(AsmToken::Colon))
1433 return ErrorOperand(Start, "Expected ':' token!");
1434 Parser.Lex(); // Eat :
1435 if (getLexer().isNot(AsmToken::LBrac))
1436 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001437 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001438 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001439
Chad Rosier95ce8892013-04-19 18:39:50 +00001440 const MCExpr *Val;
1441 if (!isParsingInlineAsm()) {
1442 if (getParser().parsePrimaryExpr(Val, End))
1443 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1444
1445 return X86Operand::CreateMem(Val, Start, End, Size);
1446 }
1447
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001448 InlineAsmIdentifierInfo Info;
Chad Rosierce031892013-04-11 23:24:15 +00001449 StringRef Identifier = Tok.getString();
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001450 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info, End))
Chad Rosier8a244662013-04-02 20:02:33 +00001451 return Err;
Chad Rosier95ce8892013-04-19 18:39:50 +00001452 return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001453 /*Scale=*/1, Start, End, Size, Identifier, Info);
Chad Rosier91c82662012-10-24 17:22:29 +00001454}
1455
Chad Rosier5dcb4662012-10-24 22:21:50 +00001456/// Parse the '.' operator.
Chad Rosiercc541e82013-04-19 15:57:00 +00001457X86Operand *X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1458 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001459 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001460 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001461
1462 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001463 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001464 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001465 else
1466 return ErrorOperand(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001467
1468 // Drop the '.'.
1469 StringRef DotDispStr = Tok.getString().drop_front(1);
1470
Chad Rosier5dcb4662012-10-24 22:21:50 +00001471 // .Imm gets lexed as a real.
1472 if (Tok.is(AsmToken::Real)) {
1473 APInt DotDisp;
1474 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001475 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001476 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001477 unsigned DotDisp;
1478 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1479 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001480 DotDisp))
1481 return ErrorOperand(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001482 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001483 } else
1484 return ErrorOperand(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001485
Chad Rosier240b7b92012-10-25 21:51:10 +00001486 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1487 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1488 unsigned Len = DotDispStr.size();
1489 unsigned Val = OrigDispVal + DotDispVal;
1490 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1491 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001492 }
1493
Chad Rosiercc541e82013-04-19 15:57:00 +00001494 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1495 return 0;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001496}
1497
Chad Rosier91c82662012-10-24 17:22:29 +00001498/// Parse the 'offset' operator. This operator is used to specify the
1499/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001500X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001501 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001502 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001503 Parser.Lex(); // Eat offset.
Chad Rosier91c82662012-10-24 17:22:29 +00001504
Chad Rosier91c82662012-10-24 17:22:29 +00001505 const MCExpr *Val;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001506 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001507 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001508 StringRef Identifier = Tok.getString();
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001509 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info, End))
Chad Rosierae7ecd62013-04-11 23:37:34 +00001510 return Err;
1511
Chad Rosiere2f03772012-10-26 16:09:20 +00001512 // Don't emit the offset operator.
1513 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1514
Chad Rosier91c82662012-10-24 17:22:29 +00001515 // The offset operator will have an 'r' constraint, thus we need to create
1516 // register operand to ensure proper matching. Just pick a GPR based on
1517 // the size of a pointer.
1518 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001519 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosier732b8372013-04-22 22:04:25 +00001520 OffsetOfLoc, Identifier, Info.OpDecl);
Devang Patel41b9dde2012-01-17 18:00:18 +00001521}
1522
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001523enum IntelOperatorKind {
1524 IOK_LENGTH,
1525 IOK_SIZE,
1526 IOK_TYPE
1527};
1528
1529/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1530/// returns the number of elements in an array. It returns the value 1 for
1531/// non-array variables. The SIZE operator returns the size of a C or C++
1532/// variable. A variable's size is the product of its LENGTH and TYPE. The
1533/// TYPE operator returns the size of a C or C++ type or variable. If the
1534/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001535X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001536 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001537 SMLoc TypeLoc = Tok.getLoc();
1538 Parser.Lex(); // Eat operator.
Chad Rosier11c42f22012-10-26 18:04:20 +00001539
Chad Rosier95ce8892013-04-19 18:39:50 +00001540 const MCExpr *Val = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001541 InlineAsmIdentifierInfo Info;
Chad Rosier18785852013-04-09 20:58:48 +00001542 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001543 StringRef Identifier = Tok.getString();
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001544 if (X86Operand *Err = ParseIntelIdentifier(Val, Identifier, Info, End))
Chad Rosierb67f8052013-04-11 23:57:04 +00001545 return Err;
Chad Rosier11c42f22012-10-26 18:04:20 +00001546
Chad Rosierf6675c32013-04-22 17:01:46 +00001547 unsigned CVal = 0;
Chad Rosiercb78f0d2013-04-22 19:42:15 +00001548 switch(OpKind) {
1549 default: llvm_unreachable("Unexpected operand kind!");
1550 case IOK_LENGTH: CVal = Info.Length; break;
1551 case IOK_SIZE: CVal = Info.Size; break;
1552 case IOK_TYPE: CVal = Info.Type; break;
1553 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001554
1555 // Rewrite the type operator and the C or C++ type or variable in terms of an
1556 // immediate. E.g. TYPE foo -> $$4
1557 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001558 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001559
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001560 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001561 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001562}
1563
Devang Patel41b9dde2012-01-17 18:00:18 +00001564X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001565 const AsmToken &Tok = Parser.getTok();
1566 SMLoc Start = Tok.getLoc(), End;
Chad Rosier91c82662012-10-24 17:22:29 +00001567
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001568 // Offset, length, type and size operators.
1569 if (isParsingInlineAsm()) {
Chad Rosier99e54642013-04-19 17:32:29 +00001570 StringRef AsmTokStr = Tok.getString();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001571 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001572 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001573 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001574 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001575 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001576 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001577 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001578 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001579 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001580
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001581 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001582 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1583 getLexer().is(AsmToken::LParen)) {
1584 AsmToken StartTok = Tok;
1585 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1586 /*AddImmPrefix=*/false);
1587 if (X86Operand *Err = ParseIntelExpression(SM, End))
1588 return Err;
1589
1590 int64_t Imm = SM.getImm();
1591 if (isParsingInlineAsm()) {
1592 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1593 if (StartTok.getString().size() == Len)
1594 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001595 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001596 else
1597 // Otherwise, rewrite the complex expression as a single immediate.
1598 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001599 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001600
1601 if (getLexer().isNot(AsmToken::LBrac)) {
1602 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1603 return X86Operand::CreateImm(ImmExpr, Start, End);
1604 }
1605
1606 // Only positive immediates are valid.
1607 if (Imm < 0)
1608 return ErrorOperand(Start, "expected a positive immediate displacement "
1609 "before bracketed expr.");
1610
1611 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1612 return ParseIntelMemOperand(/*SegReg=*/0, Imm, Start);
Devang Patel41b9dde2012-01-17 18:00:18 +00001613 }
1614
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001615 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001616 unsigned RegNo = 0;
1617 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001618 // If this is a segment register followed by a ':', then this is the start
1619 // of a memory reference, otherwise this is a normal register reference.
1620 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001621 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001622
1623 getParser().Lex(); // Eat the colon.
Chad Rosier1530ba52013-03-27 21:49:56 +00001624 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001625 }
1626
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001627 // Memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001628 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001629}
1630
Devang Patel4a6e7782012-01-12 18:03:40 +00001631X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001632 switch (getLexer().getKind()) {
1633 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001634 // Parse a memory operand with no segment register.
1635 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001636 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001637 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001638 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001639 SMLoc Start, End;
1640 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001641 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001642 Error(Start, "%eiz and %riz can only be used as index registers",
1643 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001644 return 0;
1645 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001646
Chris Lattnerb9270732010-04-17 18:56:34 +00001647 // If this is a segment register followed by a ':', then this is the start
1648 // of a memory reference, otherwise this is a normal register reference.
1649 if (getLexer().isNot(AsmToken::Colon))
1650 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001651
Chris Lattnerb9270732010-04-17 18:56:34 +00001652 getParser().Lex(); // Eat the colon.
1653 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001654 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001655 case AsmToken::Dollar: {
1656 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001657 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001658 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001659 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001660 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001661 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001662 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001663 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001664 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001665}
1666
Chris Lattnerb9270732010-04-17 18:56:34 +00001667/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1668/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001669X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001670
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001671 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1672 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001673 // only way to do this without lookahead is to eat the '(' and see what is
1674 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001675 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001676 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001677 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001678 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001679
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001680 // After parsing the base expression we could either have a parenthesized
1681 // memory address or not. If not, return now. If so, eat the (.
1682 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001683 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001684 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001685 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001686 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001687 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001688
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001689 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001690 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001691 } else {
1692 // Okay, we have a '('. We don't know if this is an expression or not, but
1693 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001694 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001695 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001696
Kevin Enderby7d912182009-09-03 17:15:07 +00001697 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001698 // Nothing to do here, fall into the code below with the '(' part of the
1699 // memory operand consumed.
1700 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001701 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001702
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001703 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001704 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001705 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001706
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001707 // After parsing the base expression we could either have a parenthesized
1708 // memory address or not. If not, return now. If so, eat the (.
1709 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001710 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001711 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001712 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001713 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001714 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001715
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001716 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001717 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001718 }
1719 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001720
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001721 // If we reached here, then we just ate the ( of the memory operand. Process
1722 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001723 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001724 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001725
Chris Lattner0c2538f2010-01-15 18:51:29 +00001726 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001727 SMLoc StartLoc, EndLoc;
1728 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001729 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001730 Error(StartLoc, "eiz and riz can only be used as index registers",
1731 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001732 return 0;
1733 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001734 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001735
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001736 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001737 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001738 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001739
1740 // Following the comma we should have either an index register, or a scale
1741 // value. We don't support the later form, but we want to parse it
1742 // correctly.
1743 //
1744 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001745 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001746 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001747 SMLoc L;
1748 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001749
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001750 if (getLexer().isNot(AsmToken::RParen)) {
1751 // Parse the scale amount:
1752 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001753 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001754 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001755 "expected comma in scale expression");
1756 return 0;
1757 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001758 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001759
1760 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001761 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001762
1763 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001764 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001765 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001766 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001767 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001768
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001769 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001770 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1771 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1772 return 0;
1773 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001774 Scale = (unsigned)ScaleVal;
1775 }
1776 }
1777 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001778 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001779 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001780 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001781
1782 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001783 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001784 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001785
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001786 if (Value != 1)
1787 Warning(Loc, "scale factor without index register is ignored");
1788 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001789 }
1790 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001791
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001792 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001793 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001794 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001795 return 0;
1796 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001797 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001798 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001799
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001800 // If we have both a base register and an index register make sure they are
1801 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001802 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001803 if (BaseReg != 0 && IndexReg != 0) {
1804 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001805 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1806 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001807 IndexReg != X86::RIZ) {
1808 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1809 return 0;
1810 }
1811 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001812 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1813 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001814 IndexReg != X86::EIZ){
1815 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1816 return 0;
1817 }
1818 }
1819
Chris Lattner015cfb12010-01-15 19:33:43 +00001820 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1821 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001822}
1823
Devang Patel4a6e7782012-01-12 18:03:40 +00001824bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001825ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001826 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001827 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001828 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001829
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001830 // FIXME: Hack to recognize setneb as setne.
1831 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1832 PatchedName != "setb" && PatchedName != "setnb")
1833 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001834
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001835 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1836 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001837 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001838 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1839 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001840 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001841 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001842 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001843 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001844 .Case("eq", 0x00)
1845 .Case("lt", 0x01)
1846 .Case("le", 0x02)
1847 .Case("unord", 0x03)
1848 .Case("neq", 0x04)
1849 .Case("nlt", 0x05)
1850 .Case("nle", 0x06)
1851 .Case("ord", 0x07)
1852 /* AVX only from here */
1853 .Case("eq_uq", 0x08)
1854 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001855 .Case("ngt", 0x0A)
1856 .Case("false", 0x0B)
1857 .Case("neq_oq", 0x0C)
1858 .Case("ge", 0x0D)
1859 .Case("gt", 0x0E)
1860 .Case("true", 0x0F)
1861 .Case("eq_os", 0x10)
1862 .Case("lt_oq", 0x11)
1863 .Case("le_oq", 0x12)
1864 .Case("unord_s", 0x13)
1865 .Case("neq_us", 0x14)
1866 .Case("nlt_uq", 0x15)
1867 .Case("nle_uq", 0x16)
1868 .Case("ord_s", 0x17)
1869 .Case("eq_us", 0x18)
1870 .Case("nge_uq", 0x19)
1871 .Case("ngt_uq", 0x1A)
1872 .Case("false_os", 0x1B)
1873 .Case("neq_os", 0x1C)
1874 .Case("ge_oq", 0x1D)
1875 .Case("gt_oq", 0x1E)
1876 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001877 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001878 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001879 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1880 getParser().getContext());
1881 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001882 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001883 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001884 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001885 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001886 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001887 } else {
1888 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001889 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001890 }
1891 }
1892 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001893
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001894 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001895
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001896 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001897 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001898
Chris Lattner086a83a2010-09-08 05:17:37 +00001899 // Determine whether this is an instruction prefix.
1900 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001901 Name == "lock" || Name == "rep" ||
1902 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001903 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001904 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001905
1906
Chris Lattner086a83a2010-09-08 05:17:37 +00001907 // This does the actual operand parsing. Don't parse any more if we have a
1908 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1909 // just want to parse the "lock" as the first instruction and the "incl" as
1910 // the next one.
1911 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001912
1913 // Parse '*' modifier.
1914 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001915 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001916 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001917 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001918 }
1919
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001920 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001921 if (X86Operand *Op = ParseOperand())
1922 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001923 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001924 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001925 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001926 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001927
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001928 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001929 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001930
1931 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001932 if (X86Operand *Op = ParseOperand())
1933 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001934 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001935 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001936 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001937 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001938 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001939
Chris Lattnera2a9d162010-09-11 16:18:25 +00001940 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00001941 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001942 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00001943 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00001944 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001945 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001946
Chris Lattner086a83a2010-09-08 05:17:37 +00001947 if (getLexer().is(AsmToken::EndOfStatement))
1948 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00001949 else if (isPrefix && getLexer().is(AsmToken::Slash))
1950 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001951
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001952 if (ExtraImmOp && isParsingIntelSyntax())
1953 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1954
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001955 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1956 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1957 // documented form in various unofficial manuals, so a lot of code uses it.
1958 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1959 Operands.size() == 3) {
1960 X86Operand &Op = *(X86Operand*)Operands.back();
1961 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1962 isa<MCConstantExpr>(Op.Mem.Disp) &&
1963 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1964 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1965 SMLoc Loc = Op.getEndLoc();
1966 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1967 delete &Op;
1968 }
1969 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00001970 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1971 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1972 Operands.size() == 3) {
1973 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1974 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1975 isa<MCConstantExpr>(Op.Mem.Disp) &&
1976 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1977 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1978 SMLoc Loc = Op.getEndLoc();
1979 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1980 delete &Op;
1981 }
1982 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001983 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1984 if (Name.startswith("ins") && Operands.size() == 3 &&
1985 (Name == "insb" || Name == "insw" || Name == "insl")) {
1986 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1987 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1988 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1989 Operands.pop_back();
1990 Operands.pop_back();
1991 delete &Op;
1992 delete &Op2;
1993 }
1994 }
1995
1996 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1997 if (Name.startswith("outs") && Operands.size() == 3 &&
1998 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1999 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2000 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2001 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2002 Operands.pop_back();
2003 Operands.pop_back();
2004 delete &Op;
2005 delete &Op2;
2006 }
2007 }
2008
2009 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2010 if (Name.startswith("movs") && Operands.size() == 3 &&
2011 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002012 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002013 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2014 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2015 if (isSrcOp(Op) && isDstOp(Op2)) {
2016 Operands.pop_back();
2017 Operands.pop_back();
2018 delete &Op;
2019 delete &Op2;
2020 }
2021 }
2022 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2023 if (Name.startswith("lods") && Operands.size() == 3 &&
2024 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002025 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002026 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2027 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2028 if (isSrcOp(*Op1) && Op2->isReg()) {
2029 const char *ins;
2030 unsigned reg = Op2->getReg();
2031 bool isLods = Name == "lods";
2032 if (reg == X86::AL && (isLods || Name == "lodsb"))
2033 ins = "lodsb";
2034 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2035 ins = "lodsw";
2036 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2037 ins = "lodsl";
2038 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2039 ins = "lodsq";
2040 else
2041 ins = NULL;
2042 if (ins != NULL) {
2043 Operands.pop_back();
2044 Operands.pop_back();
2045 delete Op1;
2046 delete Op2;
2047 if (Name != ins)
2048 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2049 }
2050 }
2051 }
2052 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2053 if (Name.startswith("stos") && Operands.size() == 3 &&
2054 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002055 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002056 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2057 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2058 if (isDstOp(*Op2) && Op1->isReg()) {
2059 const char *ins;
2060 unsigned reg = Op1->getReg();
2061 bool isStos = Name == "stos";
2062 if (reg == X86::AL && (isStos || Name == "stosb"))
2063 ins = "stosb";
2064 else if (reg == X86::AX && (isStos || Name == "stosw"))
2065 ins = "stosw";
2066 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2067 ins = "stosl";
2068 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2069 ins = "stosq";
2070 else
2071 ins = NULL;
2072 if (ins != NULL) {
2073 Operands.pop_back();
2074 Operands.pop_back();
2075 delete Op1;
2076 delete Op2;
2077 if (Name != ins)
2078 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2079 }
2080 }
2081 }
2082
Chris Lattner4bd21712010-09-15 04:33:27 +00002083 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002084 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002085 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002086 Name.startswith("shl") || Name.startswith("sal") ||
2087 Name.startswith("rcl") || Name.startswith("rcr") ||
2088 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002089 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002090 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002091 // Intel syntax
2092 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2093 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002094 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2095 delete Operands[2];
2096 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002097 }
2098 } else {
2099 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2100 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002101 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2102 delete Operands[1];
2103 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002104 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002105 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002106 }
Chad Rosier51afe632012-06-27 22:34:28 +00002107
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002108 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2109 // instalias with an immediate operand yet.
2110 if (Name == "int" && Operands.size() == 2) {
2111 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2112 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2113 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2114 delete Operands[1];
2115 Operands.erase(Operands.begin() + 1);
2116 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2117 }
2118 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002119
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002120 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002121}
2122
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002123static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2124 bool isCmp) {
2125 MCInst TmpInst;
2126 TmpInst.setOpcode(Opcode);
2127 if (!isCmp)
2128 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2129 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2130 TmpInst.addOperand(Inst.getOperand(0));
2131 Inst = TmpInst;
2132 return true;
2133}
2134
2135static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2136 bool isCmp = false) {
2137 if (!Inst.getOperand(0).isImm() ||
2138 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2139 return false;
2140
2141 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2142}
2143
2144static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2145 bool isCmp = false) {
2146 if (!Inst.getOperand(0).isImm() ||
2147 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2148 return false;
2149
2150 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2151}
2152
2153static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2154 bool isCmp = false) {
2155 if (!Inst.getOperand(0).isImm() ||
2156 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2157 return false;
2158
2159 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2160}
2161
Devang Patel4a6e7782012-01-12 18:03:40 +00002162bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002163processInstruction(MCInst &Inst,
2164 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2165 switch (Inst.getOpcode()) {
2166 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002167 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2168 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2169 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2170 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2171 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2172 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2173 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2174 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2175 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2176 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2177 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2178 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2179 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2180 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2181 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2182 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2183 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2184 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002185 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2186 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2187 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2188 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2189 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2190 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelde47cce2012-01-18 22:42:29 +00002191 }
Devang Patelde47cce2012-01-18 22:42:29 +00002192}
2193
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002194static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002195bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002196MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002197 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002198 MCStreamer &Out, unsigned &ErrorInfo,
2199 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002200 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002201 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2202 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosier3d4bc622012-08-21 19:36:59 +00002203 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002204
Chris Lattnera63292a2010-09-29 01:50:45 +00002205 // First, handle aliases that expand to multiple instructions.
2206 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002207 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002208 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002209 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002210 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002211 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002212 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002213 MCInst Inst;
2214 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002215 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002216 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002217 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002218
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002219 const char *Repl =
2220 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002221 .Case("finit", "fninit")
2222 .Case("fsave", "fnsave")
2223 .Case("fstcw", "fnstcw")
2224 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002225 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002226 .Case("fstsw", "fnstsw")
2227 .Case("fstsww", "fnstsw")
2228 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002229 .Default(0);
2230 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002231 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002232 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002233 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002234
Chris Lattner628fbec2010-09-06 21:54:15 +00002235 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002236 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002237
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002238 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002239 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002240 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002241 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002242 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002243 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002244 // Some instructions need post-processing to, for example, tweak which
2245 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002246 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002247 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002248 while (processInstruction(Inst, Operands))
2249 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002250
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002251 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002252 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002253 Out.EmitInstruction(Inst);
2254 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002255 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002256 case Match_MissingFeature: {
2257 assert(ErrorInfo && "Unknown missing feature!");
2258 // Special case the error message for the very common case where only
2259 // a single subtarget feature is missing.
2260 std::string Msg = "instruction requires:";
2261 unsigned Mask = 1;
2262 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2263 if (ErrorInfo & Mask) {
2264 Msg += " ";
2265 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2266 }
2267 Mask <<= 1;
2268 }
2269 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2270 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002271 case Match_InvalidOperand:
2272 WasOriginallyInvalidOperand = true;
2273 break;
2274 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002275 break;
2276 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002277
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002278 // FIXME: Ideally, we would only attempt suffix matches for things which are
2279 // valid prefixes, and we could just infer the right unambiguous
2280 // type. However, that requires substantially more matcher support than the
2281 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002282
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002283 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002284 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002285 SmallString<16> Tmp;
2286 Tmp += Base;
2287 Tmp += ' ';
2288 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002289
Chris Lattnerfab94132010-11-06 18:28:02 +00002290 // If this instruction starts with an 'f', then it is a floating point stack
2291 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2292 // 80-bit floating point, which use the suffixes s,l,t respectively.
2293 //
2294 // Otherwise, we assume that this may be an integer instruction, which comes
2295 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2296 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002297
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002298 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002299 Tmp[Base.size()] = Suffixes[0];
2300 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002301 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002302 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002303
Chad Rosier2f480a82012-10-12 22:53:36 +00002304 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2305 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002306 // If this returned as a missing feature failure, remember that.
2307 if (Match1 == Match_MissingFeature)
2308 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002309 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002310 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2311 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002312 // If this returned as a missing feature failure, remember that.
2313 if (Match2 == Match_MissingFeature)
2314 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002315 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002316 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2317 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002318 // If this returned as a missing feature failure, remember that.
2319 if (Match3 == Match_MissingFeature)
2320 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002321 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002322 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2323 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002324 // If this returned as a missing feature failure, remember that.
2325 if (Match4 == Match_MissingFeature)
2326 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002327
2328 // Restore the old token.
2329 Op->setTokenValue(Base);
2330
2331 // If exactly one matched, then we treat that as a successful match (and the
2332 // instruction will already have been filled in correctly, since the failing
2333 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002334 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002335 (Match1 == Match_Success) + (Match2 == Match_Success) +
2336 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002337 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002338 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002339 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002340 Out.EmitInstruction(Inst);
2341 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002342 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002343 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002344
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002345 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002346
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002347 // If we had multiple suffix matches, then identify this as an ambiguous
2348 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002349 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002350 char MatchChars[4];
2351 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002352 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2353 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2354 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2355 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002356
2357 SmallString<126> Msg;
2358 raw_svector_ostream OS(Msg);
2359 OS << "ambiguous instructions require an explicit suffix (could be ";
2360 for (unsigned i = 0; i != NumMatches; ++i) {
2361 if (i != 0)
2362 OS << ", ";
2363 if (i + 1 == NumMatches)
2364 OS << "or ";
2365 OS << "'" << Base << MatchChars[i] << "'";
2366 }
2367 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002368 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002369 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002370 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002371
Chris Lattner628fbec2010-09-06 21:54:15 +00002372 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002373
Chris Lattner628fbec2010-09-06 21:54:15 +00002374 // If all of the instructions reported an invalid mnemonic, then the original
2375 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002376 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2377 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002378 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002379 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002380 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002381 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002382 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002383 }
2384
2385 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002386 if (ErrorInfo != ~0U) {
2387 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002388 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002389 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002390
Chad Rosier49963552012-10-13 00:26:04 +00002391 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002392 if (Operand->getStartLoc().isValid()) {
2393 SMRange OperandRange = Operand->getLocRange();
2394 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002395 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002396 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002397 }
2398
Chad Rosier3d4bc622012-08-21 19:36:59 +00002399 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002400 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002401 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002402
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002403 // If one instruction matched with a missing feature, report this as a
2404 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002405 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2406 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002407 std::string Msg = "instruction requires:";
2408 unsigned Mask = 1;
2409 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2410 if (ErrorInfoMissingFeature & Mask) {
2411 Msg += " ";
2412 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2413 }
2414 Mask <<= 1;
2415 }
2416 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002417 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002418
Chris Lattner628fbec2010-09-06 21:54:15 +00002419 // If one instruction matched with an invalid operand, report this as an
2420 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002421 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2422 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002423 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002424 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002425 return true;
2426 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002427
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002428 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002429 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002430 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002431 return true;
2432}
2433
2434
Devang Patel4a6e7782012-01-12 18:03:40 +00002435bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002436 StringRef IDVal = DirectiveID.getIdentifier();
2437 if (IDVal == ".word")
2438 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002439 else if (IDVal.startswith(".code"))
2440 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002441 else if (IDVal.startswith(".att_syntax")) {
2442 getParser().setAssemblerDialect(0);
2443 return false;
2444 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002445 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002446 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2447 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002448 // FIXME : Handle noprefix
2449 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002450 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002451 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002452 }
2453 return false;
2454 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002455 return true;
2456}
2457
2458/// ParseDirectiveWord
2459/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002460bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002461 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2462 for (;;) {
2463 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002464 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002465 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002466
Eric Christopherbf7bc492013-01-09 03:52:05 +00002467 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002468
Chris Lattner72c0b592010-10-30 17:38:55 +00002469 if (getLexer().is(AsmToken::EndOfStatement))
2470 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002471
Chris Lattner72c0b592010-10-30 17:38:55 +00002472 // FIXME: Improve diagnostic.
2473 if (getLexer().isNot(AsmToken::Comma))
2474 return Error(L, "unexpected token in directive");
2475 Parser.Lex();
2476 }
2477 }
Chad Rosier51afe632012-06-27 22:34:28 +00002478
Chris Lattner72c0b592010-10-30 17:38:55 +00002479 Parser.Lex();
2480 return false;
2481}
2482
Evan Cheng481ebb02011-07-27 00:38:12 +00002483/// ParseDirectiveCode
2484/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002485bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002486 if (IDVal == ".code32") {
2487 Parser.Lex();
2488 if (is64BitMode()) {
2489 SwitchMode();
2490 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2491 }
2492 } else if (IDVal == ".code64") {
2493 Parser.Lex();
2494 if (!is64BitMode()) {
2495 SwitchMode();
2496 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2497 }
2498 } else {
2499 return Error(L, "unexpected directive " + IDVal);
2500 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002501
Evan Cheng481ebb02011-07-27 00:38:12 +00002502 return false;
2503}
Chris Lattner72c0b592010-10-30 17:38:55 +00002504
Daniel Dunbar71475772009-07-17 20:42:00 +00002505// Force static initialization.
2506extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002507 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2508 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002509}
Daniel Dunbar00331992009-07-29 00:02:19 +00002510
Chris Lattner3e4582a2010-09-06 19:11:01 +00002511#define GET_REGISTER_MATCHER
2512#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002513#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002514#include "X86GenAsmMatcher.inc"