MC/X86 AsmMatcher: Fix a use after free spotted by d0k, and de-XFAIL
[oota-llvm.git] / lib / Target / X86 / AsmParser / X86AsmParser.cpp
1 //===-- 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
10 #include "llvm/Target/TargetAsmParser.h"
11 #include "X86.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCParser/MCAsmLexer.h"
19 #include "llvm/MC/MCParser/MCAsmParser.h"
20 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Target/TargetAsmParser.h"
24 using namespace llvm;
25
26 namespace {
27 struct X86Operand;
28
29 class X86ATTAsmParser : public TargetAsmParser {
30   MCAsmParser &Parser;
31
32 private:
33   MCAsmParser &getParser() const { return Parser; }
34
35   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
36
37   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
38
39   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
40
41   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
42
43   X86Operand *ParseOperand();
44   X86Operand *ParseMemOperand();
45
46   bool ParseDirectiveWord(unsigned Size, SMLoc L);
47
48   /// @name Auto-generated Match Functions
49   /// {  
50
51   bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
52                         MCInst &Inst);
53
54   /// }
55
56 public:
57   X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
58     : TargetAsmParser(T), Parser(_Parser) {}
59
60   virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
61                                 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
62
63   virtual bool ParseDirective(AsmToken DirectiveID);
64 };
65   
66 } // end anonymous namespace
67
68 /// @name Auto-generated Match Functions
69 /// {  
70
71 static unsigned MatchRegisterName(StringRef Name);
72
73 /// }
74
75 namespace {
76
77 /// X86Operand - Instances of this class represent a parsed X86 machine
78 /// instruction.
79 struct X86Operand : public MCParsedAsmOperand {
80   enum KindTy {
81     Token,
82     Register,
83     Immediate,
84     Memory
85   } Kind;
86
87   SMLoc StartLoc, EndLoc;
88   
89   union {
90     struct {
91       const char *Data;
92       unsigned Length;
93     } Tok;
94
95     struct {
96       unsigned RegNo;
97     } Reg;
98
99     struct {
100       const MCExpr *Val;
101     } Imm;
102
103     struct {
104       unsigned SegReg;
105       const MCExpr *Disp;
106       unsigned BaseReg;
107       unsigned IndexReg;
108       unsigned Scale;
109     } Mem;
110   };
111
112   X86Operand(KindTy K, SMLoc Start, SMLoc End)
113     : Kind(K), StartLoc(Start), EndLoc(End) {}
114   
115   /// getStartLoc - Get the location of the first token of this operand.
116   SMLoc getStartLoc() const { return StartLoc; }
117   /// getEndLoc - Get the location of the last token of this operand.
118   SMLoc getEndLoc() const { return EndLoc; }
119
120   StringRef getToken() const {
121     assert(Kind == Token && "Invalid access!");
122     return StringRef(Tok.Data, Tok.Length);
123   }
124
125   unsigned getReg() const {
126     assert(Kind == Register && "Invalid access!");
127     return Reg.RegNo;
128   }
129
130   const MCExpr *getImm() const {
131     assert(Kind == Immediate && "Invalid access!");
132     return Imm.Val;
133   }
134
135   const MCExpr *getMemDisp() const {
136     assert(Kind == Memory && "Invalid access!");
137     return Mem.Disp;
138   }
139   unsigned getMemSegReg() const {
140     assert(Kind == Memory && "Invalid access!");
141     return Mem.SegReg;
142   }
143   unsigned getMemBaseReg() const {
144     assert(Kind == Memory && "Invalid access!");
145     return Mem.BaseReg;
146   }
147   unsigned getMemIndexReg() const {
148     assert(Kind == Memory && "Invalid access!");
149     return Mem.IndexReg;
150   }
151   unsigned getMemScale() const {
152     assert(Kind == Memory && "Invalid access!");
153     return Mem.Scale;
154   }
155
156   bool isToken() const {return Kind == Token; }
157
158   bool isImm() const { return Kind == Immediate; }
159   
160   bool isImmSExt8() const { 
161     // Accept immediates which fit in 8 bits when sign extended, and
162     // non-absolute immediates.
163     if (!isImm())
164       return false;
165
166     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
167       int64_t Value = CE->getValue();
168       return Value == (int64_t) (int8_t) Value;
169     }
170
171     return true;
172   }
173   
174   bool isMem() const { return Kind == Memory; }
175
176   bool isAbsMem() const {
177     return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
178       !getMemIndexReg() && getMemScale() == 1;
179   }
180
181   bool isNoSegMem() const {
182     return Kind == Memory && !getMemSegReg();
183   }
184
185   bool isReg() const { return Kind == Register; }
186
187   void addRegOperands(MCInst &Inst, unsigned N) const {
188     assert(N == 1 && "Invalid number of operands!");
189     Inst.addOperand(MCOperand::CreateReg(getReg()));
190   }
191
192   void addImmOperands(MCInst &Inst, unsigned N) const {
193     assert(N == 1 && "Invalid number of operands!");
194     Inst.addOperand(MCOperand::CreateExpr(getImm()));
195   }
196
197   void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
198     // FIXME: Support user customization of the render method.
199     assert(N == 1 && "Invalid number of operands!");
200     Inst.addOperand(MCOperand::CreateExpr(getImm()));
201   }
202
203   void addMemOperands(MCInst &Inst, unsigned N) const {
204     assert((N == 5) && "Invalid number of operands!");
205     Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
206     Inst.addOperand(MCOperand::CreateImm(getMemScale()));
207     Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
208     Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
209     Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
210   }
211
212   void addAbsMemOperands(MCInst &Inst, unsigned N) const {
213     assert((N == 1) && "Invalid number of operands!");
214     Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
215   }
216
217   void addNoSegMemOperands(MCInst &Inst, unsigned N) const {
218     assert((N == 4) && "Invalid number of operands!");
219     Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
220     Inst.addOperand(MCOperand::CreateImm(getMemScale()));
221     Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
222     Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
223   }
224
225   static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
226     X86Operand *Res = new X86Operand(Token, Loc, Loc);
227     Res->Tok.Data = Str.data();
228     Res->Tok.Length = Str.size();
229     return Res;
230   }
231
232   static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
233     X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
234     Res->Reg.RegNo = RegNo;
235     return Res;
236   }
237
238   static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
239     X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
240     Res->Imm.Val = Val;
241     return Res;
242   }
243
244   /// Create an absolute memory operand.
245   static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
246                                SMLoc EndLoc) {
247     X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
248     Res->Mem.SegReg   = 0;
249     Res->Mem.Disp     = Disp;
250     Res->Mem.BaseReg  = 0;
251     Res->Mem.IndexReg = 0;
252     Res->Mem.Scale    = 1;
253     return Res;
254   }
255
256   /// Create a generalized memory operand.
257   static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
258                                unsigned BaseReg, unsigned IndexReg,
259                                unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
260     // We should never just have a displacement, that should be parsed as an
261     // absolute memory operand.
262     assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
263
264     // The scale should always be one of {1,2,4,8}.
265     assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
266            "Invalid scale!");
267     X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
268     Res->Mem.SegReg   = SegReg;
269     Res->Mem.Disp     = Disp;
270     Res->Mem.BaseReg  = BaseReg;
271     Res->Mem.IndexReg = IndexReg;
272     Res->Mem.Scale    = Scale;
273     return Res;
274   }
275 };
276
277 } // end anonymous namespace.
278
279
280 bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
281                                     SMLoc &StartLoc, SMLoc &EndLoc) {
282   RegNo = 0;
283   const AsmToken &TokPercent = Parser.getTok();
284   assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
285   StartLoc = TokPercent.getLoc();
286   Parser.Lex(); // Eat percent token.
287
288   const AsmToken &Tok = Parser.getTok();
289   if (Tok.isNot(AsmToken::Identifier))
290     return Error(Tok.getLoc(), "invalid register name");
291
292   // FIXME: Validate register for the current architecture; we have to do
293   // validation later, so maybe there is no need for this here.
294   RegNo = MatchRegisterName(Tok.getString());
295   
296   // Parse %st(1) and "%st" as "%st(0)"
297   if (RegNo == 0 && Tok.getString() == "st") {
298     RegNo = X86::ST0;
299     EndLoc = Tok.getLoc();
300     Parser.Lex(); // Eat 'st'
301     
302     // Check to see if we have '(4)' after %st.
303     if (getLexer().isNot(AsmToken::LParen))
304       return false;
305     // Lex the paren.
306     getParser().Lex();
307
308     const AsmToken &IntTok = Parser.getTok();
309     if (IntTok.isNot(AsmToken::Integer))
310       return Error(IntTok.getLoc(), "expected stack index");
311     switch (IntTok.getIntVal()) {
312     case 0: RegNo = X86::ST0; break;
313     case 1: RegNo = X86::ST1; break;
314     case 2: RegNo = X86::ST2; break;
315     case 3: RegNo = X86::ST3; break;
316     case 4: RegNo = X86::ST4; break;
317     case 5: RegNo = X86::ST5; break;
318     case 6: RegNo = X86::ST6; break;
319     case 7: RegNo = X86::ST7; break;
320     default: return Error(IntTok.getLoc(), "invalid stack index");
321     }
322     
323     if (getParser().Lex().isNot(AsmToken::RParen))
324       return Error(Parser.getTok().getLoc(), "expected ')'");
325     
326     EndLoc = Tok.getLoc();
327     Parser.Lex(); // Eat ')'
328     return false;
329   }
330   
331   if (RegNo == 0)
332     return Error(Tok.getLoc(), "invalid register name");
333
334   EndLoc = Tok.getLoc();
335   Parser.Lex(); // Eat identifier token.
336   return false;
337 }
338
339 X86Operand *X86ATTAsmParser::ParseOperand() {
340   switch (getLexer().getKind()) {
341   default:
342     return ParseMemOperand();
343   case AsmToken::Percent: {
344     // FIXME: if a segment register, this could either be just the seg reg, or
345     // the start of a memory operand.
346     unsigned RegNo;
347     SMLoc Start, End;
348     if (ParseRegister(RegNo, Start, End)) return 0;
349     return X86Operand::CreateReg(RegNo, Start, End);
350   }
351   case AsmToken::Dollar: {
352     // $42 -> immediate.
353     SMLoc Start = Parser.getTok().getLoc(), End;
354     Parser.Lex();
355     const MCExpr *Val;
356     if (getParser().ParseExpression(Val, End))
357       return 0;
358     return X86Operand::CreateImm(Val, Start, End);
359   }
360   }
361 }
362
363 /// ParseMemOperand: segment: disp(basereg, indexreg, scale)
364 X86Operand *X86ATTAsmParser::ParseMemOperand() {
365   SMLoc MemStart = Parser.getTok().getLoc();
366   
367   // FIXME: If SegReg ':'  (e.g. %gs:), eat and remember.
368   unsigned SegReg = 0;
369   
370   // We have to disambiguate a parenthesized expression "(4+5)" from the start
371   // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)".  The
372   // only way to do this without lookahead is to eat the '(' and see what is
373   // after it.
374   const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
375   if (getLexer().isNot(AsmToken::LParen)) {
376     SMLoc ExprEnd;
377     if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
378     
379     // After parsing the base expression we could either have a parenthesized
380     // memory address or not.  If not, return now.  If so, eat the (.
381     if (getLexer().isNot(AsmToken::LParen)) {
382       // Unless we have a segment register, treat this as an immediate.
383       if (SegReg == 0)
384         return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
385       return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
386     }
387     
388     // Eat the '('.
389     Parser.Lex();
390   } else {
391     // Okay, we have a '('.  We don't know if this is an expression or not, but
392     // so we have to eat the ( to see beyond it.
393     SMLoc LParenLoc = Parser.getTok().getLoc();
394     Parser.Lex(); // Eat the '('.
395     
396     if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
397       // Nothing to do here, fall into the code below with the '(' part of the
398       // memory operand consumed.
399     } else {
400       SMLoc ExprEnd;
401       
402       // It must be an parenthesized expression, parse it now.
403       if (getParser().ParseParenExpression(Disp, ExprEnd))
404         return 0;
405       
406       // After parsing the base expression we could either have a parenthesized
407       // memory address or not.  If not, return now.  If so, eat the (.
408       if (getLexer().isNot(AsmToken::LParen)) {
409         // Unless we have a segment register, treat this as an immediate.
410         if (SegReg == 0)
411           return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
412         return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
413       }
414       
415       // Eat the '('.
416       Parser.Lex();
417     }
418   }
419   
420   // If we reached here, then we just ate the ( of the memory operand.  Process
421   // the rest of the memory operand.
422   unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
423   
424   if (getLexer().is(AsmToken::Percent)) {
425     SMLoc L;
426     if (ParseRegister(BaseReg, L, L)) return 0;
427   }
428   
429   if (getLexer().is(AsmToken::Comma)) {
430     Parser.Lex(); // Eat the comma.
431
432     // Following the comma we should have either an index register, or a scale
433     // value. We don't support the later form, but we want to parse it
434     // correctly.
435     //
436     // Not that even though it would be completely consistent to support syntax
437     // like "1(%eax,,1)", the assembler doesn't.
438     if (getLexer().is(AsmToken::Percent)) {
439       SMLoc L;
440       if (ParseRegister(IndexReg, L, L)) return 0;
441     
442       if (getLexer().isNot(AsmToken::RParen)) {
443         // Parse the scale amount:
444         //  ::= ',' [scale-expression]
445         if (getLexer().isNot(AsmToken::Comma)) {
446           Error(Parser.getTok().getLoc(),
447                 "expected comma in scale expression");
448           return 0;
449         }
450         Parser.Lex(); // Eat the comma.
451
452         if (getLexer().isNot(AsmToken::RParen)) {
453           SMLoc Loc = Parser.getTok().getLoc();
454
455           int64_t ScaleVal;
456           if (getParser().ParseAbsoluteExpression(ScaleVal))
457             return 0;
458           
459           // Validate the scale amount.
460           if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
461             Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
462             return 0;
463           }
464           Scale = (unsigned)ScaleVal;
465         }
466       }
467     } else if (getLexer().isNot(AsmToken::RParen)) {
468       // Otherwise we have the unsupported form of a scale amount without an
469       // index.
470       SMLoc Loc = Parser.getTok().getLoc();
471
472       int64_t Value;
473       if (getParser().ParseAbsoluteExpression(Value))
474         return 0;
475       
476       Error(Loc, "cannot have scale factor without index register");
477       return 0;
478     }
479   }
480   
481   // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
482   if (getLexer().isNot(AsmToken::RParen)) {
483     Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
484     return 0;
485   }
486   SMLoc MemEnd = Parser.getTok().getLoc();
487   Parser.Lex(); // Eat the ')'.
488   
489   return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
490                                MemStart, MemEnd);
491 }
492
493 bool X86ATTAsmParser::
494 ParseInstruction(const StringRef &Name, SMLoc NameLoc,
495                  SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
496   // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to
497   // represent alternative syntaxes in the .td file, without requiring
498   // instruction duplication.
499   StringRef PatchedName = StringSwitch<StringRef>(Name)
500     .Case("sal", "shl")
501     .Case("salb", "shlb")
502     .Case("sall", "shll")
503     .Case("salq", "shlq")
504     .Case("salw", "shlw")
505     .Case("repe", "rep")
506     .Case("repz", "rep")
507     .Case("repnz", "repne")
508     .Default(Name);
509   Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
510
511   if (getLexer().isNot(AsmToken::EndOfStatement)) {
512
513     // Parse '*' modifier.
514     if (getLexer().is(AsmToken::Star)) {
515       SMLoc Loc = Parser.getTok().getLoc();
516       Operands.push_back(X86Operand::CreateToken("*", Loc));
517       Parser.Lex(); // Eat the star.
518     }
519
520     // Read the first operand.
521     if (X86Operand *Op = ParseOperand())
522       Operands.push_back(Op);
523     else
524       return true;
525     
526     while (getLexer().is(AsmToken::Comma)) {
527       Parser.Lex();  // Eat the comma.
528
529       // Parse and remember the operand.
530       if (X86Operand *Op = ParseOperand())
531         Operands.push_back(Op);
532       else
533         return true;
534     }
535   }
536
537   return false;
538 }
539
540 bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
541   StringRef IDVal = DirectiveID.getIdentifier();
542   if (IDVal == ".word")
543     return ParseDirectiveWord(2, DirectiveID.getLoc());
544   return true;
545 }
546
547 /// ParseDirectiveWord
548 ///  ::= .word [ expression (, expression)* ]
549 bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
550   if (getLexer().isNot(AsmToken::EndOfStatement)) {
551     for (;;) {
552       const MCExpr *Value;
553       if (getParser().ParseExpression(Value))
554         return true;
555
556       getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
557
558       if (getLexer().is(AsmToken::EndOfStatement))
559         break;
560       
561       // FIXME: Improve diagnostic.
562       if (getLexer().isNot(AsmToken::Comma))
563         return Error(L, "unexpected token in directive");
564       Parser.Lex();
565     }
566   }
567
568   Parser.Lex();
569   return false;
570 }
571
572 extern "C" void LLVMInitializeX86AsmLexer();
573
574 // Force static initialization.
575 extern "C" void LLVMInitializeX86AsmParser() {
576   RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
577   RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
578   LLVMInitializeX86AsmLexer();
579 }
580
581 #include "X86GenAsmMatcher.inc"