blob: 4418a3c5e221c7197a50e08248e389bba507ab4e [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;
209 public:
Chad Rosierbfb70992013-04-17 00:11:46 +0000210 IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
Chad Rosier31246272013-04-17 21:01:45 +0000211 State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
212 Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
213 AddImmPrefix(addimmprefix) {}
Chad Rosier5362af92013-04-16 18:15:40 +0000214
215 unsigned getBaseReg() { return BaseReg; }
216 unsigned getIndexReg() { return IndexReg; }
217 unsigned getScale() { return Scale; }
218 const MCExpr *getSym() { return Sym; }
219 StringRef getSymName() { return SymName; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000220 int64_t getImm() { return Imm + IC.execute(); }
Chad Rosier5362af92013-04-16 18:15:40 +0000221 bool isValidEndState() { return State == IES_RBRAC; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000222 bool getStopOnLBrac() { return StopOnLBrac; }
223 bool getAddImmPrefix() { return AddImmPrefix; }
Chad Rosier31246272013-04-17 21:01:45 +0000224 bool hadError() { return State == IES_ERROR; }
Chad Rosierbfb70992013-04-17 00:11:46 +0000225
Chad Rosier5362af92013-04-16 18:15:40 +0000226 void onPlus() {
Chad Rosier31246272013-04-17 21:01:45 +0000227 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000228 switch (State) {
229 default:
230 State = IES_ERROR;
231 break;
232 case IES_INTEGER:
233 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000234 case IES_REGISTER:
235 State = IES_PLUS;
Chad Rosier5362af92013-04-16 18:15:40 +0000236 IC.pushOperator(IC_PLUS);
Chad Rosier31246272013-04-17 21:01:45 +0000237 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
238 // If we already have a BaseReg, then assume this is the IndexReg with
239 // a scale of 1.
240 if (!BaseReg) {
241 BaseReg = TmpReg;
242 } else {
243 assert (!IndexReg && "BaseReg/IndexReg already set!");
244 IndexReg = TmpReg;
245 Scale = 1;
246 }
247 }
Chad Rosier5362af92013-04-16 18:15:40 +0000248 break;
249 }
Chad Rosier31246272013-04-17 21:01:45 +0000250 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000251 }
252 void onMinus() {
Chad Rosier31246272013-04-17 21:01:45 +0000253 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000254 switch (State) {
255 default:
256 State = IES_ERROR;
257 break;
258 case IES_PLUS:
Chad Rosier31246272013-04-17 21:01:45 +0000259 case IES_MULTIPLY:
260 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000261 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000262 case IES_RPAREN:
Chad Rosier31246272013-04-17 21:01:45 +0000263 case IES_LBRAC:
264 case IES_RBRAC:
265 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000266 case IES_REGISTER:
267 State = IES_MINUS;
Chad Rosier31246272013-04-17 21:01:45 +0000268 // Only push the minus operator if it is not a unary operator.
269 if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
270 CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
271 CurrState == IES_LPAREN || CurrState == IES_LBRAC))
272 IC.pushOperator(IC_MINUS);
273 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
274 // If we already have a BaseReg, then assume this is the IndexReg with
275 // a scale of 1.
276 if (!BaseReg) {
277 BaseReg = TmpReg;
278 } else {
279 assert (!IndexReg && "BaseReg/IndexReg already set!");
280 IndexReg = TmpReg;
281 Scale = 1;
282 }
Chad Rosier5362af92013-04-16 18:15:40 +0000283 }
Chad Rosier5362af92013-04-16 18:15:40 +0000284 break;
285 }
Chad Rosier31246272013-04-17 21:01:45 +0000286 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000287 }
288 void onRegister(unsigned Reg) {
Chad Rosier31246272013-04-17 21:01:45 +0000289 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000290 switch (State) {
291 default:
292 State = IES_ERROR;
293 break;
294 case IES_PLUS:
295 case IES_LPAREN:
296 State = IES_REGISTER;
297 TmpReg = Reg;
298 IC.pushOperand(IC_REGISTER);
299 break;
Chad Rosier31246272013-04-17 21:01:45 +0000300 case IES_MULTIPLY:
301 // Index Register - Scale * Register
302 if (PrevState == IES_INTEGER) {
303 assert (!IndexReg && "IndexReg already set!");
304 State = IES_REGISTER;
305 IndexReg = Reg;
306 // Get the scale and replace the 'Scale * Register' with '0'.
307 Scale = IC.popOperand();
308 IC.pushOperand(IC_IMM);
309 IC.popOperator();
310 } else {
311 State = IES_ERROR;
312 }
Chad Rosier5362af92013-04-16 18:15:40 +0000313 break;
314 }
Chad Rosier31246272013-04-17 21:01:45 +0000315 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000316 }
317 void onDispExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosierdb003992013-04-18 16:28:19 +0000318 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000319 switch (State) {
320 default:
321 State = IES_ERROR;
322 break;
323 case IES_PLUS:
324 case IES_MINUS:
325 State = IES_INTEGER;
326 Sym = SymRef;
327 SymName = SymRefName;
328 IC.pushOperand(IC_IMM);
329 break;
330 }
331 }
332 void onInteger(int64_t TmpInt) {
Chad Rosier31246272013-04-17 21:01:45 +0000333 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000334 switch (State) {
335 default:
336 State = IES_ERROR;
337 break;
338 case IES_PLUS:
339 case IES_MINUS:
Chad Rosier5362af92013-04-16 18:15:40 +0000340 case IES_DIVIDE:
Chad Rosier31246272013-04-17 21:01:45 +0000341 case IES_MULTIPLY:
Chad Rosier5362af92013-04-16 18:15:40 +0000342 case IES_LPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000343 State = IES_INTEGER;
Chad Rosier31246272013-04-17 21:01:45 +0000344 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
345 // Index Register - Register * Scale
346 assert (!IndexReg && "IndexReg already set!");
347 IndexReg = TmpReg;
348 Scale = TmpInt;
349 // Get the scale and replace the 'Register * Scale' with '0'.
350 IC.popOperator();
351 } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
352 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
353 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
354 CurrState == IES_MINUS) {
355 // Unary minus. No need to pop the minus operand because it was never
356 // pushed.
357 IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
358 } else {
359 IC.pushOperand(IC_IMM, TmpInt);
360 }
Chad Rosier5362af92013-04-16 18:15:40 +0000361 break;
362 }
Chad Rosier31246272013-04-17 21:01:45 +0000363 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000364 }
365 void onStar() {
Chad Rosierdb003992013-04-18 16:28:19 +0000366 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000367 switch (State) {
368 default:
369 State = IES_ERROR;
370 break;
371 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000372 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000373 case IES_RPAREN:
374 State = IES_MULTIPLY;
375 IC.pushOperator(IC_MULTIPLY);
376 break;
377 }
378 }
379 void onDivide() {
Chad Rosierdb003992013-04-18 16:28:19 +0000380 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000381 switch (State) {
382 default:
383 State = IES_ERROR;
384 break;
385 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000386 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000387 State = IES_DIVIDE;
388 IC.pushOperator(IC_DIVIDE);
389 break;
390 }
391 }
392 void onLBrac() {
Chad Rosierdb003992013-04-18 16:28:19 +0000393 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000394 switch (State) {
395 default:
396 State = IES_ERROR;
397 break;
398 case IES_RBRAC:
399 State = IES_PLUS;
400 IC.pushOperator(IC_PLUS);
401 break;
402 }
403 }
404 void onRBrac() {
Chad Rosier31246272013-04-17 21:01:45 +0000405 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000406 switch (State) {
407 default:
408 State = IES_ERROR;
409 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000410 case IES_INTEGER:
Chad Rosier5362af92013-04-16 18:15:40 +0000411 case IES_REGISTER:
Chad Rosier31246272013-04-17 21:01:45 +0000412 case IES_RPAREN:
Chad Rosier5362af92013-04-16 18:15:40 +0000413 State = IES_RBRAC;
Chad Rosier31246272013-04-17 21:01:45 +0000414 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
415 // If we already have a BaseReg, then assume this is the IndexReg with
416 // a scale of 1.
417 if (!BaseReg) {
418 BaseReg = TmpReg;
419 } else {
420 assert (!IndexReg && "BaseReg/IndexReg already set!");
421 IndexReg = TmpReg;
422 Scale = 1;
423 }
Chad Rosier5362af92013-04-16 18:15:40 +0000424 }
425 break;
426 }
Chad Rosier31246272013-04-17 21:01:45 +0000427 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000428 }
429 void onLParen() {
Chad Rosier31246272013-04-17 21:01:45 +0000430 IntelExprState CurrState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000431 switch (State) {
432 default:
433 State = IES_ERROR;
434 break;
435 case IES_PLUS:
436 case IES_MINUS:
437 case IES_MULTIPLY:
438 case IES_DIVIDE:
Chad Rosier5362af92013-04-16 18:15:40 +0000439 case IES_LPAREN:
Chad Rosierdb003992013-04-18 16:28:19 +0000440 // FIXME: We don't handle this type of unary minus, yet.
441 if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
442 PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
443 PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
444 CurrState == IES_MINUS) {
445 State = IES_ERROR;
446 break;
447 }
Chad Rosier5362af92013-04-16 18:15:40 +0000448 State = IES_LPAREN;
449 IC.pushOperator(IC_LPAREN);
450 break;
451 }
Chad Rosier31246272013-04-17 21:01:45 +0000452 PrevState = CurrState;
Chad Rosier5362af92013-04-16 18:15:40 +0000453 }
454 void onRParen() {
Chad Rosierdb003992013-04-18 16:28:19 +0000455 PrevState = State;
Chad Rosier5362af92013-04-16 18:15:40 +0000456 switch (State) {
457 default:
458 State = IES_ERROR;
459 break;
Chad Rosier5362af92013-04-16 18:15:40 +0000460 case IES_INTEGER:
Chad Rosier31246272013-04-17 21:01:45 +0000461 case IES_REGISTER:
Chad Rosier5362af92013-04-16 18:15:40 +0000462 case IES_RPAREN:
463 State = IES_RPAREN;
464 IC.pushOperator(IC_RPAREN);
465 break;
466 }
467 }
468 };
469
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000470 MCAsmParser &getParser() const { return Parser; }
471
472 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
473
Chris Lattnera3a06812011-10-16 04:47:35 +0000474 bool Error(SMLoc L, const Twine &Msg,
Chad Rosier3d4bc622012-08-21 19:36:59 +0000475 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier4453e842012-10-12 23:09:25 +0000476 bool MatchingInlineAsm = false) {
477 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +0000478 return Parser.Error(L, Msg, Ranges);
479 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000480
Devang Patel41b9dde2012-01-17 18:00:18 +0000481 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
482 Error(Loc, Msg);
483 return 0;
484 }
485
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000486 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +0000487 X86Operand *ParseATTOperand();
488 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000489 X86Operand *ParseIntelOffsetOfOperator();
Chad Rosiercc541e82013-04-19 15:57:00 +0000490 X86Operand *ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
Chad Rosier10d1d1c2013-04-09 20:44:09 +0000491 X86Operand *ParseIntelOperator(unsigned OpKind);
Chad Rosier6241c1a2013-04-17 21:14:38 +0000492 X86Operand *ParseIntelMemOperand(unsigned SegReg, int64_t ImmDisp,
Chad Rosier1530ba52013-03-27 21:49:56 +0000493 SMLoc StartLoc);
Chad Rosier5362af92013-04-16 18:15:40 +0000494 X86Operand *ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
Chad Rosiere9902d82013-04-12 19:51:49 +0000495 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +0000496 int64_t ImmDisp, unsigned Size);
Chad Rosier8a244662013-04-02 20:02:33 +0000497 X86Operand *ParseIntelVarWithQualifier(const MCExpr *&Disp,
Chad Rosierce031892013-04-11 23:24:15 +0000498 StringRef &Identifier);
Chris Lattnerb9270732010-04-17 18:56:34 +0000499 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000500
Chad Rosier175d0ae2013-04-12 18:21:18 +0000501 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
502 unsigned BaseReg, unsigned IndexReg,
503 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiere9902d82013-04-12 19:51:49 +0000504 unsigned Size, StringRef SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +0000505
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000506 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +0000507 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000508
Devang Patelde47cce2012-01-18 22:42:29 +0000509 bool processInstruction(MCInst &Inst,
510 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
511
Chad Rosier49963552012-10-13 00:26:04 +0000512 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +0000513 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +0000514 MCStreamer &Out, unsigned &ErrorInfo,
515 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +0000516
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000517 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000518 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000519 bool isSrcOp(X86Operand &Op);
520
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000521 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
522 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000523 bool isDstOp(X86Operand &Op);
524
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000525 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +0000526 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000527 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000528 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000529 void SwitchMode() {
530 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
531 setAvailableFeatures(FB);
532 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000533
Chad Rosierc2f055d2013-04-18 16:13:18 +0000534 bool isParsingIntelSyntax() {
535 return getParser().getAssemblerDialect();
536 }
537
Daniel Dunbareefe8612010-07-19 05:44:09 +0000538 /// @name Auto-generated Matcher Functions
539 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000540
Chris Lattner3e4582a2010-09-06 19:11:01 +0000541#define GET_ASSEMBLER_HEADER
542#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000543
Daniel Dunbar00331992009-07-29 00:02:19 +0000544 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000545
546public:
Devang Patel4a6e7782012-01-12 18:03:40 +0000547 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosierf0e87202012-10-25 20:41:34 +0000548 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000549
Daniel Dunbareefe8612010-07-19 05:44:09 +0000550 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000551 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000552 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000553 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000554
Chad Rosierf0e87202012-10-25 20:41:34 +0000555 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
556 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000557 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000558
559 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000560};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000561} // end anonymous namespace
562
Sean Callanan86c11812010-01-23 00:40:33 +0000563/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000564/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000565
Chris Lattner60db0a62010-02-09 00:34:28 +0000566static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000567
568/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000569
Craig Topper6bf3ed42012-07-18 04:59:16 +0000570static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000571 return (( Value <= 0x000000000000007FULL)||
572 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
573 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
574}
575
576static bool isImmSExti32i8Value(uint64_t Value) {
577 return (( Value <= 0x000000000000007FULL)||
578 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
579 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
580}
581
582static bool isImmZExtu32u8Value(uint64_t Value) {
583 return (Value <= 0x00000000000000FFULL);
584}
585
586static bool isImmSExti64i8Value(uint64_t Value) {
587 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000588 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000589}
590
591static bool isImmSExti64i32Value(uint64_t Value) {
592 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000593 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000594}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000595namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000596
597/// X86Operand - Instances of this class represent a parsed X86 machine
598/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000599struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000600 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000601 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000602 Register,
603 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000604 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000605 } Kind;
606
Chris Lattner0c2538f2010-01-15 18:51:29 +0000607 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000608 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000609 StringRef SymName;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000610 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000611
Eric Christopher8996c5d2013-03-15 00:42:55 +0000612 struct TokOp {
613 const char *Data;
614 unsigned Length;
615 };
616
617 struct RegOp {
618 unsigned RegNo;
619 };
620
621 struct ImmOp {
622 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000623 };
624
625 struct MemOp {
626 unsigned SegReg;
627 const MCExpr *Disp;
628 unsigned BaseReg;
629 unsigned IndexReg;
630 unsigned Scale;
631 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000632 };
633
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000634 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000635 struct TokOp Tok;
636 struct RegOp Reg;
637 struct ImmOp Imm;
638 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000639 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000640
Chris Lattner015cfb12010-01-15 19:33:43 +0000641 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000642 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000643
Chad Rosiere81309b2013-04-09 17:53:49 +0000644 StringRef getSymName() { return SymName; }
645
Chris Lattner86e61532010-01-15 19:06:59 +0000646 /// getStartLoc - Get the location of the first token of this operand.
647 SMLoc getStartLoc() const { return StartLoc; }
648 /// getEndLoc - Get the location of the last token of this operand.
649 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000650 /// getLocRange - Get the range between the first and last token of this
651 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000652 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000653 /// getOffsetOfLoc - Get the location of the offset operator.
654 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000655
Jim Grosbach602aa902011-07-13 15:34:57 +0000656 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000657
Daniel Dunbare10787e2009-08-07 08:26:05 +0000658 StringRef getToken() const {
659 assert(Kind == Token && "Invalid access!");
660 return StringRef(Tok.Data, Tok.Length);
661 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000662 void setTokenValue(StringRef Value) {
663 assert(Kind == Token && "Invalid access!");
664 Tok.Data = Value.data();
665 Tok.Length = Value.size();
666 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000667
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000668 unsigned getReg() const {
669 assert(Kind == Register && "Invalid access!");
670 return Reg.RegNo;
671 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000672
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000673 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000674 assert(Kind == Immediate && "Invalid access!");
675 return Imm.Val;
676 }
677
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000678 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000679 assert(Kind == Memory && "Invalid access!");
680 return Mem.Disp;
681 }
682 unsigned getMemSegReg() const {
683 assert(Kind == Memory && "Invalid access!");
684 return Mem.SegReg;
685 }
686 unsigned getMemBaseReg() const {
687 assert(Kind == Memory && "Invalid access!");
688 return Mem.BaseReg;
689 }
690 unsigned getMemIndexReg() const {
691 assert(Kind == Memory && "Invalid access!");
692 return Mem.IndexReg;
693 }
694 unsigned getMemScale() const {
695 assert(Kind == Memory && "Invalid access!");
696 return Mem.Scale;
697 }
698
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000699 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000700
701 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000702
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000703 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000704 if (!isImm())
705 return false;
706
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000707 // If this isn't a constant expr, just assume it fits and let relaxation
708 // handle it.
709 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
710 if (!CE)
711 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000712
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000713 // Otherwise, check the value is in a range that makes sense for this
714 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000715 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000716 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000717 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000718 if (!isImm())
719 return false;
720
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000721 // If this isn't a constant expr, just assume it fits and let relaxation
722 // handle it.
723 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
724 if (!CE)
725 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000726
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000727 // Otherwise, check the value is in a range that makes sense for this
728 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000729 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000730 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000731 bool isImmZExtu32u8() const {
732 if (!isImm())
733 return false;
734
735 // If this isn't a constant expr, just assume it fits and let relaxation
736 // handle it.
737 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
738 if (!CE)
739 return true;
740
741 // Otherwise, check the value is in a range that makes sense for this
742 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000743 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000744 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000745 bool isImmSExti64i8() const {
746 if (!isImm())
747 return false;
748
749 // If this isn't a constant expr, just assume it fits and let relaxation
750 // handle it.
751 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
752 if (!CE)
753 return true;
754
755 // Otherwise, check the value is in a range that makes sense for this
756 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000757 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000758 }
759 bool isImmSExti64i32() const {
760 if (!isImm())
761 return false;
762
763 // If this isn't a constant expr, just assume it fits and let relaxation
764 // handle it.
765 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
766 if (!CE)
767 return true;
768
769 // Otherwise, check the value is in a range that makes sense for this
770 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000771 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000772 }
773
Chad Rosier5bca3f92012-10-22 19:50:35 +0000774 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000775 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000776 }
777
Chad Rosiera4bc9432013-01-10 22:10:27 +0000778 bool needAddressOf() const {
779 return AddressOf;
780 }
781
Daniel Dunbare10787e2009-08-07 08:26:05 +0000782 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000783 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000784 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000785 }
Chad Rosier51afe632012-06-27 22:34:28 +0000786 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000787 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000788 }
Chad Rosier51afe632012-06-27 22:34:28 +0000789 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000790 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000791 }
Chad Rosier51afe632012-06-27 22:34:28 +0000792 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000793 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000794 }
Chad Rosier51afe632012-06-27 22:34:28 +0000795 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000796 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000797 }
Chad Rosier51afe632012-06-27 22:34:28 +0000798 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000799 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000800 }
Chad Rosier51afe632012-06-27 22:34:28 +0000801 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000802 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000803 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000804
Craig Topper01deb5f2012-07-18 04:11:12 +0000805 bool isMemVX32() const {
806 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
807 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
808 }
809 bool isMemVY32() const {
810 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
811 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
812 }
813 bool isMemVX64() const {
814 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
815 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
816 }
817 bool isMemVY64() const {
818 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
819 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
820 }
821
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000822 bool isAbsMem() const {
823 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000824 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000825 }
826
Daniel Dunbare10787e2009-08-07 08:26:05 +0000827 bool isReg() const { return Kind == Register; }
828
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000829 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
830 // Add as immediates when possible.
831 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
832 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
833 else
834 Inst.addOperand(MCOperand::CreateExpr(Expr));
835 }
836
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000837 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000838 assert(N == 1 && "Invalid number of operands!");
839 Inst.addOperand(MCOperand::CreateReg(getReg()));
840 }
841
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000842 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000843 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000844 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000845 }
846
Chad Rosier51afe632012-06-27 22:34:28 +0000847 void addMem8Operands(MCInst &Inst, unsigned N) const {
848 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000849 }
Chad Rosier51afe632012-06-27 22:34:28 +0000850 void addMem16Operands(MCInst &Inst, unsigned N) const {
851 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000852 }
Chad Rosier51afe632012-06-27 22:34:28 +0000853 void addMem32Operands(MCInst &Inst, unsigned N) const {
854 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000855 }
Chad Rosier51afe632012-06-27 22:34:28 +0000856 void addMem64Operands(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 addMem80Operands(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 addMem128Operands(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 addMem256Operands(MCInst &Inst, unsigned N) const {
866 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000867 }
Craig Topper01deb5f2012-07-18 04:11:12 +0000868 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
869 addMemOperands(Inst, N);
870 }
871 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
872 addMemOperands(Inst, N);
873 }
874 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
875 addMemOperands(Inst, N);
876 }
877 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
878 addMemOperands(Inst, N);
879 }
Devang Patelfc6be102012-01-12 01:51:42 +0000880
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000881 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000882 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000883 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
884 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
885 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000886 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000887 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
888 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000889
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000890 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
891 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000892 // Add as immediates when possible.
893 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
894 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
895 else
896 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000897 }
898
Chris Lattner528d00b2010-01-15 19:28:38 +0000899 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000900 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000901 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000902 Res->Tok.Data = Str.data();
903 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000904 return Res;
905 }
906
Chad Rosier91c82662012-10-24 17:22:29 +0000907 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000908 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000909 SMLoc OffsetOfLoc = SMLoc(),
910 StringRef SymName = StringRef()) {
Chris Lattner86e61532010-01-15 19:06:59 +0000911 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000912 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000913 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000914 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000915 Res->SymName = SymName;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000916 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000917 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000918
Chad Rosierf3c04f62013-03-19 21:58:18 +0000919 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000920 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000921 Res->Imm.Val = Val;
922 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000923 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000924
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000925 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000926 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000927 unsigned Size = 0,
928 StringRef SymName = StringRef()) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000929 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
930 Res->Mem.SegReg = 0;
931 Res->Mem.Disp = Disp;
932 Res->Mem.BaseReg = 0;
933 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000934 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000935 Res->Mem.Size = Size;
Chad Rosiere81309b2013-04-09 17:53:49 +0000936 Res->SymName = SymName;
Chad Rosier8c2a9c72013-01-10 23:39:07 +0000937 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000938 return Res;
939 }
940
941 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000942 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
943 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +0000944 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000945 unsigned Size = 0,
946 StringRef SymName = StringRef()) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000947 // We should never just have a displacement, that should be parsed as an
948 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000949 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
950
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000951 // The scale should always be one of {1,2,4,8}.
952 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000953 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000954 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000955 Res->Mem.SegReg = SegReg;
956 Res->Mem.Disp = Disp;
957 Res->Mem.BaseReg = BaseReg;
958 Res->Mem.IndexReg = IndexReg;
959 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +0000960 Res->Mem.Size = Size;
Chad Rosiere81309b2013-04-09 17:53:49 +0000961 Res->SymName = SymName;
NAKAMURA Takumi7f254272013-01-11 01:13:54 +0000962 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000963 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000964 }
965};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000966
Chris Lattner4eb9df02009-07-29 06:33:53 +0000967} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000968
Devang Patel4a6e7782012-01-12 18:03:40 +0000969bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000970 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000971
972 return (Op.isMem() &&
973 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
974 isa<MCConstantExpr>(Op.Mem.Disp) &&
975 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
976 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
977}
978
Devang Patel4a6e7782012-01-12 18:03:40 +0000979bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000980 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000981
Chad Rosier51afe632012-06-27 22:34:28 +0000982 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000983 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000984 isa<MCConstantExpr>(Op.Mem.Disp) &&
985 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
986 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
987}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000988
Devang Patel4a6e7782012-01-12 18:03:40 +0000989bool X86AsmParser::ParseRegister(unsigned &RegNo,
990 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000991 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000992 const AsmToken &PercentTok = Parser.getTok();
993 StartLoc = PercentTok.getLoc();
994
995 // If we encounter a %, ignore it. This code handles registers with and
996 // without the prefix, unprefixed registers can occur in cfi directives.
997 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000998 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000999
Sean Callanan936b0d32010-01-19 21:44:56 +00001000 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001001 EndLoc = Tok.getEndLoc();
1002
Devang Patelce6a2ca2012-01-20 22:32:05 +00001003 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001004 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001005 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001006 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001007 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001008
Kevin Enderby7d912182009-09-03 17:15:07 +00001009 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001010
Chris Lattner1261b812010-09-22 04:11:10 +00001011 // If the match failed, try the register name as lowercase.
1012 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +00001013 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +00001014
Evan Chengeda1d4f2011-07-27 23:22:03 +00001015 if (!is64BitMode()) {
1016 // FIXME: This should be done using Requires<In32BitMode> and
1017 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
1018 // checked.
1019 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
1020 // REX prefix.
1021 if (RegNo == X86::RIZ ||
1022 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
1023 X86II::isX86_64NonExtLowByteReg(RegNo) ||
1024 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +00001025 return Error(StartLoc, "register %"
1026 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001027 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +00001028 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001029
Chris Lattner1261b812010-09-22 04:11:10 +00001030 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
1031 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001032 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001033 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001034
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001035 // Check to see if we have '(4)' after %st.
1036 if (getLexer().isNot(AsmToken::LParen))
1037 return false;
1038 // Lex the paren.
1039 getParser().Lex();
1040
1041 const AsmToken &IntTok = Parser.getTok();
1042 if (IntTok.isNot(AsmToken::Integer))
1043 return Error(IntTok.getLoc(), "expected stack index");
1044 switch (IntTok.getIntVal()) {
1045 case 0: RegNo = X86::ST0; break;
1046 case 1: RegNo = X86::ST1; break;
1047 case 2: RegNo = X86::ST2; break;
1048 case 3: RegNo = X86::ST3; break;
1049 case 4: RegNo = X86::ST4; break;
1050 case 5: RegNo = X86::ST5; break;
1051 case 6: RegNo = X86::ST6; break;
1052 case 7: RegNo = X86::ST7; break;
1053 default: return Error(IntTok.getLoc(), "invalid stack index");
1054 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001055
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001056 if (getParser().Lex().isNot(AsmToken::RParen))
1057 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001058
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001059 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +00001060 Parser.Lex(); // Eat ')'
1061 return false;
1062 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001063
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001064 EndLoc = Parser.getTok().getEndLoc();
1065
Chris Lattner80486622010-06-24 07:29:18 +00001066 // If this is "db[0-7]", match it as an alias
1067 // for dr[0-7].
1068 if (RegNo == 0 && Tok.getString().size() == 3 &&
1069 Tok.getString().startswith("db")) {
1070 switch (Tok.getString()[2]) {
1071 case '0': RegNo = X86::DR0; break;
1072 case '1': RegNo = X86::DR1; break;
1073 case '2': RegNo = X86::DR2; break;
1074 case '3': RegNo = X86::DR3; break;
1075 case '4': RegNo = X86::DR4; break;
1076 case '5': RegNo = X86::DR5; break;
1077 case '6': RegNo = X86::DR6; break;
1078 case '7': RegNo = X86::DR7; break;
1079 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001080
Chris Lattner80486622010-06-24 07:29:18 +00001081 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001082 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +00001083 Parser.Lex(); // Eat it.
1084 return false;
1085 }
1086 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001087
Devang Patelce6a2ca2012-01-20 22:32:05 +00001088 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001089 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +00001090 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001091 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +00001092 }
Daniel Dunbar00331992009-07-29 00:02:19 +00001093
Sean Callanana83fd7d2010-01-19 20:27:46 +00001094 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001095 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +00001096}
1097
Devang Patel4a6e7782012-01-12 18:03:40 +00001098X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001099 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +00001100 return ParseIntelOperand();
1101 return ParseATTOperand();
1102}
1103
Devang Patel41b9dde2012-01-17 18:00:18 +00001104/// getIntelMemOperandSize - Return intel memory operand size.
1105static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +00001106 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +00001107 .Cases("BYTE", "byte", 8)
1108 .Cases("WORD", "word", 16)
1109 .Cases("DWORD", "dword", 32)
1110 .Cases("QWORD", "qword", 64)
1111 .Cases("XWORD", "xword", 80)
1112 .Cases("XMMWORD", "xmmword", 128)
1113 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +00001114 .Default(0);
1115 return Size;
Devang Patel46831de2012-01-12 01:36:43 +00001116}
1117
Chad Rosier175d0ae2013-04-12 18:21:18 +00001118X86Operand *
1119X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1120 unsigned BaseReg, unsigned IndexReg,
1121 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiere9902d82013-04-12 19:51:49 +00001122 unsigned Size, StringRef SymName) {
Chad Rosier7ca135b2013-03-19 21:11:56 +00001123 bool NeedSizeDir = false;
Chad Rosier7ca135b2013-03-19 21:11:56 +00001124 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1125 const MCSymbol &Sym = SymRef->getSymbol();
1126 // FIXME: The SemaLookup will fail if the name is anything other then an
1127 // identifier.
1128 // FIXME: Pass a valid SMLoc.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001129 bool IsVarDecl = false;
Chad Rosier7ca135b2013-03-19 21:11:56 +00001130 unsigned tLength, tSize, tType;
Chad Rosiere81309b2013-04-09 17:53:49 +00001131 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength, tSize,
1132 tType, IsVarDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001133 if (!Size) {
1134 Size = tType * 8; // Size is in terms of bits in this context.
1135 NeedSizeDir = Size > 0;
1136 }
Chad Rosier175d0ae2013-04-12 18:21:18 +00001137 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1138 // reference. We need an 'r' constraint here, so we need to create register
1139 // operand to ensure proper matching. Just pick a GPR based on the size of
1140 // a pointer.
1141 if (!IsVarDecl) {
1142 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1143 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
1144 SMLoc(), SymName);
1145 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001146 }
1147
1148 if (NeedSizeDir)
Chad Rosiere9902d82013-04-12 19:51:49 +00001149 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1150 /*Len=*/0, Size));
Chad Rosier7ca135b2013-03-19 21:11:56 +00001151
1152 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001153 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001154 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001155 BaseReg = BaseReg ? BaseReg : 1;
1156 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1157 End, Size, SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001158}
1159
Chad Rosierd383db52013-04-12 20:20:54 +00001160static void
1161RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
1162 StringRef SymName, int64_t ImmDisp,
1163 int64_t FinalImmDisp, SMLoc &BracLoc,
1164 SMLoc &StartInBrac, SMLoc &End) {
1165 // Remove the '[' and ']' from the IR string.
1166 AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
1167 AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1168
1169 // If ImmDisp is non-zero, then we parsed a displacement before the
1170 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
1171 // If ImmDisp doesn't match the displacement computed by the state machine
1172 // then we have an additional displacement in the bracketed expression.
1173 if (ImmDisp != FinalImmDisp) {
1174 if (ImmDisp) {
1175 // We have an immediate displacement before the bracketed expression.
1176 // Adjust this to match the final immediate displacement.
1177 bool Found = false;
1178 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1179 E = AsmRewrites->end(); I != E; ++I) {
1180 if ((*I).Loc.getPointer() > BracLoc.getPointer())
1181 continue;
Chad Rosierbfb70992013-04-17 00:11:46 +00001182 if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1183 assert (!Found && "ImmDisp already rewritten.");
Chad Rosierd383db52013-04-12 20:20:54 +00001184 (*I).Kind = AOK_Imm;
1185 (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1186 (*I).Val = FinalImmDisp;
1187 Found = true;
1188 break;
1189 }
1190 }
1191 assert (Found && "Unable to rewrite ImmDisp.");
1192 } else {
1193 // We have a symbolic and an immediate displacement, but no displacement
Chad Rosierbfb70992013-04-17 00:11:46 +00001194 // before the bracketed expression. Put the immediate displacement
Chad Rosierd383db52013-04-12 20:20:54 +00001195 // before the bracketed expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001196 AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
Chad Rosierd383db52013-04-12 20:20:54 +00001197 }
1198 }
1199 // Remove all the ImmPrefix rewrites within the brackets.
1200 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1201 E = AsmRewrites->end(); I != E; ++I) {
1202 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1203 continue;
1204 if ((*I).Kind == AOK_ImmPrefix)
1205 (*I).Kind = AOK_Delete;
1206 }
1207 const char *SymLocPtr = SymName.data();
1208 // Skip everything before the symbol.
1209 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1210 assert(Len > 0 && "Expected a non-negative length.");
1211 AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1212 }
1213 // Skip everything after the symbol.
1214 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1215 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1216 assert(Len > 0 && "Expected a non-negative length.");
1217 AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1218 }
1219}
1220
Chad Rosier5362af92013-04-16 18:15:40 +00001221X86Operand *
1222X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001223 const AsmToken &Tok = Parser.getTok();
Chad Rosier51afe632012-06-27 22:34:28 +00001224
Chad Rosier5c118fd2013-01-14 22:31:35 +00001225 bool Done = false;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001226 while (!Done) {
1227 bool UpdateLocLex = true;
1228
1229 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1230 // identifier. Don't try an parse it as a register.
1231 if (Tok.getString().startswith("."))
1232 break;
Chad Rosierbfb70992013-04-17 00:11:46 +00001233
1234 // If we're parsing an immediate expression, we don't expect a '['.
1235 if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1236 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001237
1238 switch (getLexer().getKind()) {
1239 default: {
1240 if (SM.isValidEndState()) {
1241 Done = true;
1242 break;
1243 }
1244 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1245 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001246 case AsmToken::EndOfStatement: {
1247 Done = true;
1248 break;
1249 }
Chad Rosier5c118fd2013-01-14 22:31:35 +00001250 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001251 // This could be a register or a symbolic displacement.
1252 unsigned TmpReg;
1253 const MCExpr *Disp = 0;
Chad Rosier152749c2013-04-12 18:54:20 +00001254 SMLoc IdentLoc = Tok.getLoc();
1255 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001256 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001257 SM.onRegister(TmpReg);
1258 UpdateLocLex = false;
1259 break;
Chad Rosier1863f4f2013-04-10 17:35:30 +00001260 } else if (!getParser().parsePrimaryExpr(Disp, End)) {
Chad Rosier152749c2013-04-12 18:54:20 +00001261 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1262 return Err;
1263
1264 SM.onDispExpr(Disp, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001265 UpdateLocLex = false;
1266 break;
1267 }
1268 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1269 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001270 case AsmToken::Integer:
Chad Rosierbfb70992013-04-17 00:11:46 +00001271 if (isParsingInlineAsm() && SM.getAddImmPrefix())
Chad Rosier4a7005e2013-04-05 16:28:55 +00001272 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1273 Tok.getLoc()));
1274 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001275 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001276 case AsmToken::Plus: SM.onPlus(); break;
1277 case AsmToken::Minus: SM.onMinus(); break;
1278 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001279 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001280 case AsmToken::LBrac: SM.onLBrac(); break;
1281 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001282 case AsmToken::LParen: SM.onLParen(); break;
1283 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001284 }
Chad Rosier31246272013-04-17 21:01:45 +00001285 if (SM.hadError())
1286 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1287
Chad Rosier5c118fd2013-01-14 22:31:35 +00001288 if (!Done && UpdateLocLex) {
1289 End = Tok.getLoc();
1290 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001291 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001292 }
Chad Rosier5362af92013-04-16 18:15:40 +00001293 return 0;
1294}
1295
1296X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001297 int64_t ImmDisp,
Chad Rosier5362af92013-04-16 18:15:40 +00001298 unsigned Size) {
1299 const AsmToken &Tok = Parser.getTok();
1300 SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1301 if (getLexer().isNot(AsmToken::LBrac))
1302 return ErrorOperand(BracLoc, "Expected '[' token!");
1303 Parser.Lex(); // Eat '['
1304
1305 SMLoc StartInBrac = Tok.getLoc();
1306 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1307 // may have already parsed an immediate displacement before the bracketed
1308 // expression.
Chad Rosierbfb70992013-04-17 00:11:46 +00001309 IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
Chad Rosier5362af92013-04-16 18:15:40 +00001310 if (X86Operand *Err = ParseIntelExpression(SM, End))
1311 return Err;
Devang Patel41b9dde2012-01-17 18:00:18 +00001312
Chad Rosier175d0ae2013-04-12 18:21:18 +00001313 const MCExpr *Disp;
1314 if (const MCExpr *Sym = SM.getSym()) {
Chad Rosierd383db52013-04-12 20:20:54 +00001315 // A symbolic displacement.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001316 Disp = Sym;
Chad Rosierd383db52013-04-12 20:20:54 +00001317 if (isParsingInlineAsm())
1318 RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
Chad Rosier5362af92013-04-16 18:15:40 +00001319 ImmDisp, SM.getImm(), BracLoc, StartInBrac,
Chad Rosierd383db52013-04-12 20:20:54 +00001320 End);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001321 } else {
Chad Rosier31246272013-04-17 21:01:45 +00001322 // An immediate displacement only.
Chad Rosier5362af92013-04-16 18:15:40 +00001323 Disp = MCConstantExpr::Create(SM.getImm(), getContext());
Chad Rosier175d0ae2013-04-12 18:21:18 +00001324 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001325
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001326 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001327 if (Tok.getString().startswith(".")) {
Chad Rosier911c1f32012-10-25 17:37:43 +00001328 const MCExpr *NewDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001329 if (X86Operand *Err = ParseIntelDotOperator(Disp, NewDisp))
1330 return Err;
Chad Rosier911c1f32012-10-25 17:37:43 +00001331
Chad Rosier70f47592013-04-10 20:07:47 +00001332 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001333 Parser.Lex(); // Eat the field.
1334 Disp = NewDisp;
1335 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001336
Chad Rosier5c118fd2013-01-14 22:31:35 +00001337 int BaseReg = SM.getBaseReg();
1338 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001339 int Scale = SM.getScale();
1340
1341 if (isParsingInlineAsm())
1342 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
Chad Rosiere9902d82013-04-12 19:51:49 +00001343 End, Size, SM.getSymName());
Devang Pateld0930ff2012-01-20 21:21:01 +00001344
Chad Rosier5c118fd2013-01-14 22:31:35 +00001345 // handle [-42]
1346 if (!BaseReg && !IndexReg) {
1347 if (!SegReg)
Chad Rosiere81309b2013-04-09 17:53:49 +00001348 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001349 else
1350 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1351 }
Chad Rosiere81309b2013-04-09 17:53:49 +00001352 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1353 End, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001354}
1355
Chad Rosier8a244662013-04-02 20:02:33 +00001356// Inline assembly may use variable names with namespace alias qualifiers.
1357X86Operand *X86AsmParser::ParseIntelVarWithQualifier(const MCExpr *&Disp,
Chad Rosierce031892013-04-11 23:24:15 +00001358 StringRef &Identifier) {
Chad Rosier8a244662013-04-02 20:02:33 +00001359 // We should only see Foo::Bar if we're parsing inline assembly.
1360 if (!isParsingInlineAsm())
1361 return 0;
1362
1363 // If we don't see a ':' then there can't be a qualifier.
1364 if (getLexer().isNot(AsmToken::Colon))
1365 return 0;
1366
Chad Rosier8a244662013-04-02 20:02:33 +00001367 bool Done = false;
1368 const AsmToken &Tok = Parser.getTok();
Chad Rosierce031892013-04-11 23:24:15 +00001369 AsmToken IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001370 while (!Done) {
1371 switch (getLexer().getKind()) {
1372 default:
1373 Done = true;
1374 break;
1375 case AsmToken::Colon:
1376 getLexer().Lex(); // Consume ':'.
1377 if (getLexer().isNot(AsmToken::Colon))
1378 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1379 getLexer().Lex(); // Consume second ':'.
1380 if (getLexer().isNot(AsmToken::Identifier))
1381 return ErrorOperand(Tok.getLoc(), "Expected an identifier token!");
1382 break;
1383 case AsmToken::Identifier:
Chad Rosierce031892013-04-11 23:24:15 +00001384 IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001385 getLexer().Lex(); // Consume the identifier.
1386 break;
1387 }
1388 }
Chad Rosierce031892013-04-11 23:24:15 +00001389
1390 unsigned Len = IdentEnd.getLoc().getPointer() - Identifier.data();
1391 Identifier = StringRef(Identifier.data(), Len + IdentEnd.getString().size());
Chad Rosier8a244662013-04-02 20:02:33 +00001392 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1393 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1394 Disp = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1395 return 0;
1396}
1397
Devang Patel41b9dde2012-01-17 18:00:18 +00001398/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001399X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
Chad Rosier6241c1a2013-04-17 21:14:38 +00001400 int64_t ImmDisp,
Chad Rosier1530ba52013-03-27 21:49:56 +00001401 SMLoc Start) {
Devang Patel41b9dde2012-01-17 18:00:18 +00001402 const AsmToken &Tok = Parser.getTok();
Chad Rosier91c82662012-10-24 17:22:29 +00001403 SMLoc End;
Devang Patel41b9dde2012-01-17 18:00:18 +00001404
1405 unsigned Size = getIntelMemOperandSize(Tok.getString());
1406 if (Size) {
1407 Parser.Lex();
Chad Rosierab53b4f2012-09-12 18:24:26 +00001408 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1409 "Unexpected token!");
Devang Patel41b9dde2012-01-17 18:00:18 +00001410 Parser.Lex();
1411 }
1412
Chad Rosier1530ba52013-03-27 21:49:56 +00001413 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1414 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier1530ba52013-03-27 21:49:56 +00001415 if (isParsingInlineAsm())
1416 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
Chad Rosier70f47592013-04-10 20:07:47 +00001417 Tok.getLoc()));
Chad Rosier6241c1a2013-04-17 21:14:38 +00001418 int64_t ImmDisp = Tok.getIntVal();
Chad Rosier1530ba52013-03-27 21:49:56 +00001419 Parser.Lex(); // Eat the integer.
1420 if (getLexer().isNot(AsmToken::LBrac))
1421 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001422 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Chad Rosier1530ba52013-03-27 21:49:56 +00001423 }
1424
Chad Rosier91c82662012-10-24 17:22:29 +00001425 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001426 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001427
1428 if (!ParseRegister(SegReg, Start, End)) {
1429 // Handel SegReg : [ ... ]
1430 if (getLexer().isNot(AsmToken::Colon))
1431 return ErrorOperand(Start, "Expected ':' token!");
1432 Parser.Lex(); // Eat :
1433 if (getLexer().isNot(AsmToken::LBrac))
1434 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001435 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001436 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001437
Chad Rosiere81309b2013-04-09 17:53:49 +00001438 const MCExpr *Disp = 0;
Chad Rosierce031892013-04-11 23:24:15 +00001439 StringRef Identifier = Tok.getString();
Chad Rosier43554ee2013-04-12 23:03:20 +00001440 if (getParser().parsePrimaryExpr(Disp, End))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001441 return 0;
Chad Rosier0f48c552012-10-19 20:57:14 +00001442
Chad Rosier146310a2012-10-23 23:31:33 +00001443 if (!isParsingInlineAsm())
Chad Rosier91c82662012-10-24 17:22:29 +00001444 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier8a244662013-04-02 20:02:33 +00001445
Chad Rosierce031892013-04-11 23:24:15 +00001446 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
Chad Rosier8a244662013-04-02 20:02:33 +00001447 return Err;
1448
Chad Rosier175d0ae2013-04-12 18:21:18 +00001449 return CreateMemForInlineAsm(/*SegReg=*/0, Disp, /*BaseReg=*/0,/*IndexReg=*/0,
Chad Rosiere9902d82013-04-12 19:51:49 +00001450 /*Scale=*/1, Start, End, Size, Identifier);
Chad Rosier91c82662012-10-24 17:22:29 +00001451}
1452
Chad Rosier5dcb4662012-10-24 22:21:50 +00001453/// Parse the '.' operator.
Chad Rosiercc541e82013-04-19 15:57:00 +00001454X86Operand *X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1455 const MCExpr *&NewDisp) {
Chad Rosier70f47592013-04-10 20:07:47 +00001456 const AsmToken &Tok = Parser.getTok();
Chad Rosier6241c1a2013-04-17 21:14:38 +00001457 int64_t OrigDispVal, DotDispVal;
Chad Rosier911c1f32012-10-25 17:37:43 +00001458
1459 // FIXME: Handle non-constant expressions.
Chad Rosiercc541e82013-04-19 15:57:00 +00001460 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
Chad Rosier911c1f32012-10-25 17:37:43 +00001461 OrigDispVal = OrigDisp->getValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001462 else
1463 return ErrorOperand(Tok.getLoc(), "Non-constant offsets are not supported!");
Chad Rosier5dcb4662012-10-24 22:21:50 +00001464
1465 // Drop the '.'.
1466 StringRef DotDispStr = Tok.getString().drop_front(1);
1467
Chad Rosier5dcb4662012-10-24 22:21:50 +00001468 // .Imm gets lexed as a real.
1469 if (Tok.is(AsmToken::Real)) {
1470 APInt DotDisp;
1471 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001472 DotDispVal = DotDisp.getZExtValue();
Chad Rosiercc541e82013-04-19 15:57:00 +00001473 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
Chad Rosier240b7b92012-10-25 21:51:10 +00001474 unsigned DotDisp;
1475 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1476 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
Chad Rosiercc541e82013-04-19 15:57:00 +00001477 DotDisp))
1478 return ErrorOperand(Tok.getLoc(), "Unable to lookup field reference!");
Chad Rosier240b7b92012-10-25 21:51:10 +00001479 DotDispVal = DotDisp;
Chad Rosiercc541e82013-04-19 15:57:00 +00001480 } else
1481 return ErrorOperand(Tok.getLoc(), "Unexpected token type!");
Chad Rosier911c1f32012-10-25 17:37:43 +00001482
Chad Rosier240b7b92012-10-25 21:51:10 +00001483 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1484 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1485 unsigned Len = DotDispStr.size();
1486 unsigned Val = OrigDispVal + DotDispVal;
1487 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1488 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001489 }
1490
Chad Rosiercc541e82013-04-19 15:57:00 +00001491 NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1492 return 0;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001493}
1494
Chad Rosier91c82662012-10-24 17:22:29 +00001495/// Parse the 'offset' operator. This operator is used to specify the
1496/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001497X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001498 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001499 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001500 Parser.Lex(); // Eat offset.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001501 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier91c82662012-10-24 17:22:29 +00001502
Chad Rosier91c82662012-10-24 17:22:29 +00001503 const MCExpr *Val;
Chad Rosier18785852013-04-09 20:58:48 +00001504 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001505 StringRef Identifier = Tok.getString();
Chad Rosier1863f4f2013-04-10 17:35:30 +00001506 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosier58593562012-10-26 18:32:44 +00001507 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosier91c82662012-10-24 17:22:29 +00001508
Chad Rosierae7ecd62013-04-11 23:37:34 +00001509 const MCExpr *Disp = 0;
1510 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1511 return Err;
1512
Chad Rosiere2f03772012-10-26 16:09:20 +00001513 // Don't emit the offset operator.
1514 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1515
Chad Rosier91c82662012-10-24 17:22:29 +00001516 // The offset operator will have an 'r' constraint, thus we need to create
1517 // register operand to ensure proper matching. Just pick a GPR based on
1518 // the size of a pointer.
1519 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001520 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosierae7ecd62013-04-11 23:37:34 +00001521 OffsetOfLoc, Identifier);
Devang Patel41b9dde2012-01-17 18:00:18 +00001522}
1523
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001524enum IntelOperatorKind {
1525 IOK_LENGTH,
1526 IOK_SIZE,
1527 IOK_TYPE
1528};
1529
1530/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1531/// returns the number of elements in an array. It returns the value 1 for
1532/// non-array variables. The SIZE operator returns the size of a C or C++
1533/// variable. A variable's size is the product of its LENGTH and TYPE. The
1534/// TYPE operator returns the size of a C or C++ type or variable. If the
1535/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001536X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001537 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001538 SMLoc TypeLoc = Tok.getLoc();
1539 Parser.Lex(); // Eat operator.
Chad Rosier18785852013-04-09 20:58:48 +00001540 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier11c42f22012-10-26 18:04:20 +00001541
Chad Rosier11c42f22012-10-26 18:04:20 +00001542 const MCExpr *Val;
Chad Rosierb67f8052013-04-11 23:57:04 +00001543 AsmToken StartTok = Tok;
Chad Rosier18785852013-04-09 20:58:48 +00001544 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001545 StringRef Identifier = Tok.getString();
Chad Rosier1863f4f2013-04-10 17:35:30 +00001546 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosierb67f8052013-04-11 23:57:04 +00001547 return ErrorOperand(Start, "Unable to parse expression!");
1548
1549 const MCExpr *Disp = 0;
1550 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1551 return Err;
Chad Rosier11c42f22012-10-26 18:04:20 +00001552
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001553 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosier11c42f22012-10-26 18:04:20 +00001554 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1555 const MCSymbol &Sym = SymRef->getSymbol();
1556 // FIXME: The SemaLookup will fail if the name is anything other then an
1557 // identifier.
1558 // FIXME: Pass a valid SMLoc.
Chad Rosiera4bc9432013-01-10 22:10:27 +00001559 bool IsVarDecl;
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001560 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1561 Size, Type, IsVarDecl))
Chad Rosierb67f8052013-04-11 23:57:04 +00001562 // FIXME: We don't warn on variables with namespace alias qualifiers
1563 // because support still needs to be added in the frontend.
1564 if (Identifier.equals(StartTok.getString()))
1565 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001566 }
1567 unsigned CVal;
1568 switch(OpKind) {
1569 default: llvm_unreachable("Unexpected operand kind!");
1570 case IOK_LENGTH: CVal = Length; break;
1571 case IOK_SIZE: CVal = Size; break;
1572 case IOK_TYPE: CVal = Type; break;
Chad Rosier11c42f22012-10-26 18:04:20 +00001573 }
1574
1575 // Rewrite the type operator and the C or C++ type or variable in terms of an
1576 // immediate. E.g. TYPE foo -> $$4
1577 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001578 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001579
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001580 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001581 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001582}
1583
Devang Patel41b9dde2012-01-17 18:00:18 +00001584X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001585 const AsmToken &Tok = Parser.getTok();
1586 SMLoc Start = Tok.getLoc(), End;
1587 StringRef AsmTokStr = Tok.getString();
Chad Rosier91c82662012-10-24 17:22:29 +00001588
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001589 // Offset, length, type and size operators.
1590 if (isParsingInlineAsm()) {
1591 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001592 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001593 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001594 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001595 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001596 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001597 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001598 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001599 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001600
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001601 // Immediate.
Chad Rosierbfb70992013-04-17 00:11:46 +00001602 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1603 getLexer().is(AsmToken::LParen)) {
1604 AsmToken StartTok = Tok;
1605 IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1606 /*AddImmPrefix=*/false);
1607 if (X86Operand *Err = ParseIntelExpression(SM, End))
1608 return Err;
1609
1610 int64_t Imm = SM.getImm();
1611 if (isParsingInlineAsm()) {
1612 unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1613 if (StartTok.getString().size() == Len)
1614 // Just add a prefix if this wasn't a complex immediate expression.
Chad Rosierf3c04f62013-03-19 21:58:18 +00001615 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierbfb70992013-04-17 00:11:46 +00001616 else
1617 // Otherwise, rewrite the complex expression as a single immediate.
1618 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
Devang Patel41b9dde2012-01-17 18:00:18 +00001619 }
Chad Rosierbfb70992013-04-17 00:11:46 +00001620
1621 if (getLexer().isNot(AsmToken::LBrac)) {
1622 const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1623 return X86Operand::CreateImm(ImmExpr, Start, End);
1624 }
1625
1626 // Only positive immediates are valid.
1627 if (Imm < 0)
1628 return ErrorOperand(Start, "expected a positive immediate displacement "
1629 "before bracketed expr.");
1630
1631 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1632 return ParseIntelMemOperand(/*SegReg=*/0, Imm, Start);
Devang Patel41b9dde2012-01-17 18:00:18 +00001633 }
1634
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001635 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001636 unsigned RegNo = 0;
1637 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001638 // If this is a segment register followed by a ':', then this is the start
1639 // of a memory reference, otherwise this is a normal register reference.
1640 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001641 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001642
1643 getParser().Lex(); // Eat the colon.
Chad Rosier1530ba52013-03-27 21:49:56 +00001644 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001645 }
1646
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001647 // Memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001648 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001649}
1650
Devang Patel4a6e7782012-01-12 18:03:40 +00001651X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001652 switch (getLexer().getKind()) {
1653 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001654 // Parse a memory operand with no segment register.
1655 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001656 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001657 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001658 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001659 SMLoc Start, End;
1660 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001661 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001662 Error(Start, "%eiz and %riz can only be used as index registers",
1663 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001664 return 0;
1665 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001666
Chris Lattnerb9270732010-04-17 18:56:34 +00001667 // If this is a segment register followed by a ':', then this is the start
1668 // of a memory reference, otherwise this is a normal register reference.
1669 if (getLexer().isNot(AsmToken::Colon))
1670 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001671
Chris Lattnerb9270732010-04-17 18:56:34 +00001672 getParser().Lex(); // Eat the colon.
1673 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001674 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001675 case AsmToken::Dollar: {
1676 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001677 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001678 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001679 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001680 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001681 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001682 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001683 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001684 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001685}
1686
Chris Lattnerb9270732010-04-17 18:56:34 +00001687/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1688/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001689X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001690
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001691 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1692 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001693 // only way to do this without lookahead is to eat the '(' and see what is
1694 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001695 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001696 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001697 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001698 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001699
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001700 // After parsing the base expression we could either have a parenthesized
1701 // memory address or not. If not, return now. If so, eat the (.
1702 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001703 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001704 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001705 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001706 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001707 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001708
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001709 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001710 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001711 } else {
1712 // Okay, we have a '('. We don't know if this is an expression or not, but
1713 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001714 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001715 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001716
Kevin Enderby7d912182009-09-03 17:15:07 +00001717 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001718 // Nothing to do here, fall into the code below with the '(' part of the
1719 // memory operand consumed.
1720 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001721 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001722
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001723 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001724 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001725 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001726
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001727 // After parsing the base expression we could either have a parenthesized
1728 // memory address or not. If not, return now. If so, eat the (.
1729 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001730 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001731 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001732 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001733 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001734 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001735
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001736 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001737 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001738 }
1739 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001740
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001741 // If we reached here, then we just ate the ( of the memory operand. Process
1742 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001743 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001744 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001745
Chris Lattner0c2538f2010-01-15 18:51:29 +00001746 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001747 SMLoc StartLoc, EndLoc;
1748 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001749 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001750 Error(StartLoc, "eiz and riz can only be used as index registers",
1751 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001752 return 0;
1753 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001754 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001755
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001756 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001757 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001758 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001759
1760 // Following the comma we should have either an index register, or a scale
1761 // value. We don't support the later form, but we want to parse it
1762 // correctly.
1763 //
1764 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001765 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001766 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001767 SMLoc L;
1768 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001769
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001770 if (getLexer().isNot(AsmToken::RParen)) {
1771 // Parse the scale amount:
1772 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001773 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001774 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001775 "expected comma in scale expression");
1776 return 0;
1777 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001778 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001779
1780 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001781 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001782
1783 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001784 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001785 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001786 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001787 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001788
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001789 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001790 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1791 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1792 return 0;
1793 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001794 Scale = (unsigned)ScaleVal;
1795 }
1796 }
1797 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001798 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001799 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001800 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001801
1802 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001803 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001804 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001805
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001806 if (Value != 1)
1807 Warning(Loc, "scale factor without index register is ignored");
1808 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001809 }
1810 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001811
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001812 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001813 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001814 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001815 return 0;
1816 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001817 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001818 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001819
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001820 // If we have both a base register and an index register make sure they are
1821 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001822 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001823 if (BaseReg != 0 && IndexReg != 0) {
1824 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001825 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1826 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001827 IndexReg != X86::RIZ) {
1828 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1829 return 0;
1830 }
1831 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001832 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1833 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001834 IndexReg != X86::EIZ){
1835 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1836 return 0;
1837 }
1838 }
1839
Chris Lattner015cfb12010-01-15 19:33:43 +00001840 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1841 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001842}
1843
Devang Patel4a6e7782012-01-12 18:03:40 +00001844bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001845ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001846 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001847 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001848 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001849
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001850 // FIXME: Hack to recognize setneb as setne.
1851 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1852 PatchedName != "setb" && PatchedName != "setnb")
1853 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001854
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001855 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1856 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001857 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001858 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1859 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001860 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001861 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001862 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001863 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001864 .Case("eq", 0x00)
1865 .Case("lt", 0x01)
1866 .Case("le", 0x02)
1867 .Case("unord", 0x03)
1868 .Case("neq", 0x04)
1869 .Case("nlt", 0x05)
1870 .Case("nle", 0x06)
1871 .Case("ord", 0x07)
1872 /* AVX only from here */
1873 .Case("eq_uq", 0x08)
1874 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001875 .Case("ngt", 0x0A)
1876 .Case("false", 0x0B)
1877 .Case("neq_oq", 0x0C)
1878 .Case("ge", 0x0D)
1879 .Case("gt", 0x0E)
1880 .Case("true", 0x0F)
1881 .Case("eq_os", 0x10)
1882 .Case("lt_oq", 0x11)
1883 .Case("le_oq", 0x12)
1884 .Case("unord_s", 0x13)
1885 .Case("neq_us", 0x14)
1886 .Case("nlt_uq", 0x15)
1887 .Case("nle_uq", 0x16)
1888 .Case("ord_s", 0x17)
1889 .Case("eq_us", 0x18)
1890 .Case("nge_uq", 0x19)
1891 .Case("ngt_uq", 0x1A)
1892 .Case("false_os", 0x1B)
1893 .Case("neq_os", 0x1C)
1894 .Case("ge_oq", 0x1D)
1895 .Case("gt_oq", 0x1E)
1896 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001897 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001898 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001899 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1900 getParser().getContext());
1901 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001902 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001903 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001904 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001905 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001906 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001907 } else {
1908 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001909 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001910 }
1911 }
1912 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001913
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001914 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001915
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001916 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001917 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001918
Chris Lattner086a83a2010-09-08 05:17:37 +00001919 // Determine whether this is an instruction prefix.
1920 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001921 Name == "lock" || Name == "rep" ||
1922 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001923 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001924 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001925
1926
Chris Lattner086a83a2010-09-08 05:17:37 +00001927 // This does the actual operand parsing. Don't parse any more if we have a
1928 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1929 // just want to parse the "lock" as the first instruction and the "incl" as
1930 // the next one.
1931 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001932
1933 // Parse '*' modifier.
1934 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001935 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001936 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001937 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001938 }
1939
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001940 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001941 if (X86Operand *Op = ParseOperand())
1942 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001943 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001944 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001945 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001946 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001947
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001948 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001949 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001950
1951 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001952 if (X86Operand *Op = ParseOperand())
1953 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001954 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001955 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001956 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001957 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001958 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001959
Chris Lattnera2a9d162010-09-11 16:18:25 +00001960 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00001961 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001962 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00001963 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00001964 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001965 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001966
Chris Lattner086a83a2010-09-08 05:17:37 +00001967 if (getLexer().is(AsmToken::EndOfStatement))
1968 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00001969 else if (isPrefix && getLexer().is(AsmToken::Slash))
1970 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001971
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001972 if (ExtraImmOp && isParsingIntelSyntax())
1973 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1974
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001975 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1976 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1977 // documented form in various unofficial manuals, so a lot of code uses it.
1978 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1979 Operands.size() == 3) {
1980 X86Operand &Op = *(X86Operand*)Operands.back();
1981 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1982 isa<MCConstantExpr>(Op.Mem.Disp) &&
1983 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1984 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1985 SMLoc Loc = Op.getEndLoc();
1986 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1987 delete &Op;
1988 }
1989 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00001990 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1991 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1992 Operands.size() == 3) {
1993 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1994 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1995 isa<MCConstantExpr>(Op.Mem.Disp) &&
1996 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1997 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1998 SMLoc Loc = Op.getEndLoc();
1999 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
2000 delete &Op;
2001 }
2002 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002003 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
2004 if (Name.startswith("ins") && Operands.size() == 3 &&
2005 (Name == "insb" || Name == "insw" || Name == "insl")) {
2006 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2007 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2008 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
2009 Operands.pop_back();
2010 Operands.pop_back();
2011 delete &Op;
2012 delete &Op2;
2013 }
2014 }
2015
2016 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
2017 if (Name.startswith("outs") && Operands.size() == 3 &&
2018 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
2019 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2020 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2021 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
2022 Operands.pop_back();
2023 Operands.pop_back();
2024 delete &Op;
2025 delete &Op2;
2026 }
2027 }
2028
2029 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
2030 if (Name.startswith("movs") && Operands.size() == 3 &&
2031 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002032 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002033 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2034 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2035 if (isSrcOp(Op) && isDstOp(Op2)) {
2036 Operands.pop_back();
2037 Operands.pop_back();
2038 delete &Op;
2039 delete &Op2;
2040 }
2041 }
2042 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2043 if (Name.startswith("lods") && Operands.size() == 3 &&
2044 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002045 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002046 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2047 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2048 if (isSrcOp(*Op1) && Op2->isReg()) {
2049 const char *ins;
2050 unsigned reg = Op2->getReg();
2051 bool isLods = Name == "lods";
2052 if (reg == X86::AL && (isLods || Name == "lodsb"))
2053 ins = "lodsb";
2054 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2055 ins = "lodsw";
2056 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2057 ins = "lodsl";
2058 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2059 ins = "lodsq";
2060 else
2061 ins = NULL;
2062 if (ins != NULL) {
2063 Operands.pop_back();
2064 Operands.pop_back();
2065 delete Op1;
2066 delete Op2;
2067 if (Name != ins)
2068 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2069 }
2070 }
2071 }
2072 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2073 if (Name.startswith("stos") && Operands.size() == 3 &&
2074 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002075 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002076 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2077 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2078 if (isDstOp(*Op2) && Op1->isReg()) {
2079 const char *ins;
2080 unsigned reg = Op1->getReg();
2081 bool isStos = Name == "stos";
2082 if (reg == X86::AL && (isStos || Name == "stosb"))
2083 ins = "stosb";
2084 else if (reg == X86::AX && (isStos || Name == "stosw"))
2085 ins = "stosw";
2086 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2087 ins = "stosl";
2088 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2089 ins = "stosq";
2090 else
2091 ins = NULL;
2092 if (ins != NULL) {
2093 Operands.pop_back();
2094 Operands.pop_back();
2095 delete Op1;
2096 delete Op2;
2097 if (Name != ins)
2098 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2099 }
2100 }
2101 }
2102
Chris Lattner4bd21712010-09-15 04:33:27 +00002103 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002104 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002105 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002106 Name.startswith("shl") || Name.startswith("sal") ||
2107 Name.startswith("rcl") || Name.startswith("rcr") ||
2108 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002109 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002110 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002111 // Intel syntax
2112 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2113 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002114 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2115 delete Operands[2];
2116 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002117 }
2118 } else {
2119 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2120 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002121 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2122 delete Operands[1];
2123 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002124 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002125 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002126 }
Chad Rosier51afe632012-06-27 22:34:28 +00002127
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002128 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2129 // instalias with an immediate operand yet.
2130 if (Name == "int" && Operands.size() == 2) {
2131 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2132 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2133 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2134 delete Operands[1];
2135 Operands.erase(Operands.begin() + 1);
2136 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2137 }
2138 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002139
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002140 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002141}
2142
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002143static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2144 bool isCmp) {
2145 MCInst TmpInst;
2146 TmpInst.setOpcode(Opcode);
2147 if (!isCmp)
2148 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2149 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2150 TmpInst.addOperand(Inst.getOperand(0));
2151 Inst = TmpInst;
2152 return true;
2153}
2154
2155static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2156 bool isCmp = false) {
2157 if (!Inst.getOperand(0).isImm() ||
2158 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2159 return false;
2160
2161 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2162}
2163
2164static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2165 bool isCmp = false) {
2166 if (!Inst.getOperand(0).isImm() ||
2167 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2168 return false;
2169
2170 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2171}
2172
2173static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2174 bool isCmp = false) {
2175 if (!Inst.getOperand(0).isImm() ||
2176 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2177 return false;
2178
2179 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2180}
2181
Devang Patel4a6e7782012-01-12 18:03:40 +00002182bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002183processInstruction(MCInst &Inst,
2184 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2185 switch (Inst.getOpcode()) {
2186 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002187 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2188 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2189 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2190 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2191 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2192 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2193 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2194 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2195 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2196 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2197 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2198 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2199 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2200 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2201 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2202 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2203 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2204 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002205 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2206 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2207 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2208 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2209 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2210 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelde47cce2012-01-18 22:42:29 +00002211 }
Devang Patelde47cce2012-01-18 22:42:29 +00002212}
2213
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002214static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002215bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002216MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002217 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002218 MCStreamer &Out, unsigned &ErrorInfo,
2219 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002220 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002221 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2222 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosier3d4bc622012-08-21 19:36:59 +00002223 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002224
Chris Lattnera63292a2010-09-29 01:50:45 +00002225 // First, handle aliases that expand to multiple instructions.
2226 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002227 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002228 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002229 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002230 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002231 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002232 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002233 MCInst Inst;
2234 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002235 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002236 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002237 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002238
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002239 const char *Repl =
2240 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002241 .Case("finit", "fninit")
2242 .Case("fsave", "fnsave")
2243 .Case("fstcw", "fnstcw")
2244 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002245 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002246 .Case("fstsw", "fnstsw")
2247 .Case("fstsww", "fnstsw")
2248 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002249 .Default(0);
2250 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002251 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002252 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002253 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002254
Chris Lattner628fbec2010-09-06 21:54:15 +00002255 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002256 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002257
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002258 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002259 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002260 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002261 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002262 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002263 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002264 // Some instructions need post-processing to, for example, tweak which
2265 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002266 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002267 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002268 while (processInstruction(Inst, Operands))
2269 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002270
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002271 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002272 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002273 Out.EmitInstruction(Inst);
2274 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002275 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002276 case Match_MissingFeature: {
2277 assert(ErrorInfo && "Unknown missing feature!");
2278 // Special case the error message for the very common case where only
2279 // a single subtarget feature is missing.
2280 std::string Msg = "instruction requires:";
2281 unsigned Mask = 1;
2282 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2283 if (ErrorInfo & Mask) {
2284 Msg += " ";
2285 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2286 }
2287 Mask <<= 1;
2288 }
2289 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2290 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002291 case Match_InvalidOperand:
2292 WasOriginallyInvalidOperand = true;
2293 break;
2294 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002295 break;
2296 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002297
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002298 // FIXME: Ideally, we would only attempt suffix matches for things which are
2299 // valid prefixes, and we could just infer the right unambiguous
2300 // type. However, that requires substantially more matcher support than the
2301 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002302
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002303 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002304 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002305 SmallString<16> Tmp;
2306 Tmp += Base;
2307 Tmp += ' ';
2308 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002309
Chris Lattnerfab94132010-11-06 18:28:02 +00002310 // If this instruction starts with an 'f', then it is a floating point stack
2311 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2312 // 80-bit floating point, which use the suffixes s,l,t respectively.
2313 //
2314 // Otherwise, we assume that this may be an integer instruction, which comes
2315 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2316 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002317
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002318 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002319 Tmp[Base.size()] = Suffixes[0];
2320 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002321 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002322 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002323
Chad Rosier2f480a82012-10-12 22:53:36 +00002324 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2325 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002326 // If this returned as a missing feature failure, remember that.
2327 if (Match1 == Match_MissingFeature)
2328 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002329 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002330 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2331 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002332 // If this returned as a missing feature failure, remember that.
2333 if (Match2 == Match_MissingFeature)
2334 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002335 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002336 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2337 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002338 // If this returned as a missing feature failure, remember that.
2339 if (Match3 == Match_MissingFeature)
2340 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002341 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002342 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2343 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002344 // If this returned as a missing feature failure, remember that.
2345 if (Match4 == Match_MissingFeature)
2346 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002347
2348 // Restore the old token.
2349 Op->setTokenValue(Base);
2350
2351 // If exactly one matched, then we treat that as a successful match (and the
2352 // instruction will already have been filled in correctly, since the failing
2353 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002354 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002355 (Match1 == Match_Success) + (Match2 == Match_Success) +
2356 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002357 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002358 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002359 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002360 Out.EmitInstruction(Inst);
2361 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002362 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002363 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002364
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002365 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002366
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002367 // If we had multiple suffix matches, then identify this as an ambiguous
2368 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002369 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002370 char MatchChars[4];
2371 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002372 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2373 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2374 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2375 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002376
2377 SmallString<126> Msg;
2378 raw_svector_ostream OS(Msg);
2379 OS << "ambiguous instructions require an explicit suffix (could be ";
2380 for (unsigned i = 0; i != NumMatches; ++i) {
2381 if (i != 0)
2382 OS << ", ";
2383 if (i + 1 == NumMatches)
2384 OS << "or ";
2385 OS << "'" << Base << MatchChars[i] << "'";
2386 }
2387 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002388 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002389 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002390 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002391
Chris Lattner628fbec2010-09-06 21:54:15 +00002392 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002393
Chris Lattner628fbec2010-09-06 21:54:15 +00002394 // If all of the instructions reported an invalid mnemonic, then the original
2395 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002396 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2397 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002398 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002399 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002400 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002401 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002402 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002403 }
2404
2405 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002406 if (ErrorInfo != ~0U) {
2407 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002408 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002409 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002410
Chad Rosier49963552012-10-13 00:26:04 +00002411 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002412 if (Operand->getStartLoc().isValid()) {
2413 SMRange OperandRange = Operand->getLocRange();
2414 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002415 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002416 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002417 }
2418
Chad Rosier3d4bc622012-08-21 19:36:59 +00002419 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002420 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002421 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002422
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002423 // If one instruction matched with a missing feature, report this as a
2424 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002425 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2426 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002427 std::string Msg = "instruction requires:";
2428 unsigned Mask = 1;
2429 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2430 if (ErrorInfoMissingFeature & Mask) {
2431 Msg += " ";
2432 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2433 }
2434 Mask <<= 1;
2435 }
2436 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002437 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002438
Chris Lattner628fbec2010-09-06 21:54:15 +00002439 // If one instruction matched with an invalid operand, report this as an
2440 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002441 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2442 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002443 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002444 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002445 return true;
2446 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002447
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002448 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002449 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002450 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002451 return true;
2452}
2453
2454
Devang Patel4a6e7782012-01-12 18:03:40 +00002455bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002456 StringRef IDVal = DirectiveID.getIdentifier();
2457 if (IDVal == ".word")
2458 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002459 else if (IDVal.startswith(".code"))
2460 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002461 else if (IDVal.startswith(".att_syntax")) {
2462 getParser().setAssemblerDialect(0);
2463 return false;
2464 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002465 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2467 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002468 // FIXME : Handle noprefix
2469 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002470 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002471 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002472 }
2473 return false;
2474 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002475 return true;
2476}
2477
2478/// ParseDirectiveWord
2479/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002480bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002481 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2482 for (;;) {
2483 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002484 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002485 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002486
Eric Christopherbf7bc492013-01-09 03:52:05 +00002487 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002488
Chris Lattner72c0b592010-10-30 17:38:55 +00002489 if (getLexer().is(AsmToken::EndOfStatement))
2490 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002491
Chris Lattner72c0b592010-10-30 17:38:55 +00002492 // FIXME: Improve diagnostic.
2493 if (getLexer().isNot(AsmToken::Comma))
2494 return Error(L, "unexpected token in directive");
2495 Parser.Lex();
2496 }
2497 }
Chad Rosier51afe632012-06-27 22:34:28 +00002498
Chris Lattner72c0b592010-10-30 17:38:55 +00002499 Parser.Lex();
2500 return false;
2501}
2502
Evan Cheng481ebb02011-07-27 00:38:12 +00002503/// ParseDirectiveCode
2504/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002505bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002506 if (IDVal == ".code32") {
2507 Parser.Lex();
2508 if (is64BitMode()) {
2509 SwitchMode();
2510 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2511 }
2512 } else if (IDVal == ".code64") {
2513 Parser.Lex();
2514 if (!is64BitMode()) {
2515 SwitchMode();
2516 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2517 }
2518 } else {
2519 return Error(L, "unexpected directive " + IDVal);
2520 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002521
Evan Cheng481ebb02011-07-27 00:38:12 +00002522 return false;
2523}
Chris Lattner72c0b592010-10-30 17:38:55 +00002524
Daniel Dunbar71475772009-07-17 20:42:00 +00002525// Force static initialization.
2526extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002527 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2528 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002529}
Daniel Dunbar00331992009-07-29 00:02:19 +00002530
Chris Lattner3e4582a2010-09-06 19:11:01 +00002531#define GET_REGISTER_MATCHER
2532#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002533#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002534#include "X86GenAsmMatcher.inc"