more syntactic cleanups.
[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 "X86.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmLexer.h"
14 #include "llvm/MC/MCAsmParser.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Target/TargetRegistry.h"
18 #include "llvm/Target/TargetAsmParser.h"
19 using namespace llvm;
20
21 namespace {
22 class X86Operand;
23
24 class X86ATTAsmParser : public TargetAsmParser {
25   MCAsmParser &Parser;
26
27 private:
28   bool MatchInstruction(const StringRef &Name,
29                         SmallVectorImpl<X86Operand> &Operands,
30                         MCInst &Inst);
31
32   MCAsmParser &getParser() const { return Parser; }
33
34   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
35
36   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
37
38   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
39
40   bool ParseRegister(X86Operand &Op);
41
42   bool ParseOperand(X86Operand &Op);
43
44   bool ParseMemOperand(X86Operand &Op);
45   
46   /// @name Auto-generated Match Functions
47   /// {  
48
49   bool MatchRegisterName(const StringRef &Name, unsigned &RegNo);
50
51   /// }
52
53 public:
54   X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
55     : TargetAsmParser(T), Parser(_Parser) {}
56
57   virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
58 };
59   
60 } // end anonymous namespace
61
62
63 namespace {
64
65 /// X86Operand - Instances of this class represent a parsed X86 machine
66 /// instruction.
67 struct X86Operand {
68   enum {
69     Register,
70     Immediate,
71     Memory
72   } Kind;
73
74   union {
75     struct {
76       unsigned RegNo;
77     } Reg;
78
79     struct {
80       MCValue Val;
81     } Imm;
82
83     struct {
84       unsigned SegReg;
85       MCValue Disp;
86       unsigned BaseReg;
87       unsigned IndexReg;
88       unsigned Scale;
89     } Mem;
90   };
91
92   unsigned getReg() const {
93     assert(Kind == Register && "Invalid access!");
94     return Reg.RegNo;
95   }
96
97   static X86Operand CreateReg(unsigned RegNo) {
98     X86Operand Res;
99     Res.Kind = Register;
100     Res.Reg.RegNo = RegNo;
101     return Res;
102   }
103   static X86Operand CreateImm(MCValue Val) {
104     X86Operand Res;
105     Res.Kind = Immediate;
106     Res.Imm.Val = Val;
107     return Res;
108   }
109   static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
110                               unsigned IndexReg, unsigned Scale) {
111     // If there is no index register, we should never have a scale, and we
112     // should always have a scale (in {1,2,4,8}) if we do.
113     assert(((Scale == 0 && !IndexReg) ||
114             (IndexReg && (Scale == 1 || Scale == 2 ||
115                           Scale == 4 || Scale == 8))) &&
116            "Invalid scale!");
117     X86Operand Res;
118     Res.Kind = Memory;
119     Res.Mem.SegReg   = SegReg;
120     Res.Mem.Disp     = Disp;
121     Res.Mem.BaseReg  = BaseReg;
122     Res.Mem.IndexReg = IndexReg;
123     Res.Mem.Scale    = Scale;
124     return Res;
125   }
126 };
127
128 } // end anonymous namespace.
129
130
131 bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
132   const AsmToken &Tok = getLexer().getTok();
133   assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
134
135   // FIXME: Validate register for the current architecture; we have to do
136   // validation later, so maybe there is no need for this here.
137   unsigned RegNo;
138   assert(Tok.getString().startswith("%") && "Invalid register name!");
139   if (MatchRegisterName(Tok.getString().substr(1), RegNo))
140     return Error(Tok.getLoc(), "invalid register name");
141
142   Op = X86Operand::CreateReg(RegNo);
143   getLexer().Lex(); // Eat register token.
144
145   return false;
146 }
147
148 bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
149   switch (getLexer().getKind()) {
150   default:
151     return ParseMemOperand(Op);
152   case AsmToken::Register:
153     // FIXME: if a segment register, this could either be just the seg reg, or
154     // the start of a memory operand.
155     return ParseRegister(Op);
156   case AsmToken::Dollar: {
157     // $42 -> immediate.
158     getLexer().Lex();
159     MCValue Val;
160     if (getParser().ParseRelocatableExpression(Val))
161       return true;
162     Op = X86Operand::CreateImm(Val);
163     return false;
164   }
165   case AsmToken::Star:
166     getLexer().Lex(); // Eat the star.
167     
168     if (getLexer().is(AsmToken::Register)) {
169       if (ParseRegister(Op))
170         return true;
171     } else if (ParseMemOperand(Op))
172       return true;
173
174     // FIXME: Note the '*' in the operand for use by the matcher.
175     return false;
176   }
177 }
178
179 /// ParseMemOperand: segment: disp(basereg, indexreg, scale)
180 bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
181   // FIXME: If SegReg ':'  (e.g. %gs:), eat and remember.
182   unsigned SegReg = 0;
183   
184   // We have to disambiguate a parenthesized expression "(4+5)" from the start
185   // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)".  The
186   // only way to do this without lookahead is to eat the ( and see what is after
187   // it.
188   MCValue Disp = MCValue::get(0, 0, 0);
189   if (getLexer().isNot(AsmToken::LParen)) {
190     if (getParser().ParseRelocatableExpression(Disp)) return true;
191     
192     // After parsing the base expression we could either have a parenthesized
193     // memory address or not.  If not, return now.  If so, eat the (.
194     if (getLexer().isNot(AsmToken::LParen)) {
195       Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
196       return false;
197     }
198     
199     // Eat the '('.
200     getLexer().Lex();
201   } else {
202     // Okay, we have a '('.  We don't know if this is an expression or not, but
203     // so we have to eat the ( to see beyond it.
204     getLexer().Lex(); // Eat the '('.
205     
206     if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
207       // Nothing to do here, fall into the code below with the '(' part of the
208       // memory operand consumed.
209     } else {
210       // It must be an parenthesized expression, parse it now.
211       if (getParser().ParseParenRelocatableExpression(Disp))
212         return true;
213       
214       // After parsing the base expression we could either have a parenthesized
215       // memory address or not.  If not, return now.  If so, eat the (.
216       if (getLexer().isNot(AsmToken::LParen)) {
217         Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
218         return false;
219       }
220       
221       // Eat the '('.
222       getLexer().Lex();
223     }
224   }
225   
226   // If we reached here, then we just ate the ( of the memory operand.  Process
227   // the rest of the memory operand.
228   unsigned BaseReg = 0, IndexReg = 0, Scale = 0;
229   
230   if (getLexer().is(AsmToken::Register)) {
231     if (ParseRegister(Op))
232       return true;
233     BaseReg = Op.getReg();
234   }
235   
236   if (getLexer().is(AsmToken::Comma)) {
237     getLexer().Lex(); // Eat the comma.
238
239     // Following the comma we should have either an index register, or a scale
240     // value. We don't support the later form, but we want to parse it
241     // correctly.
242     //
243     // Not that even though it would be completely consistent to support syntax
244     // like "1(%eax,,1)", the assembler doesn't.
245     if (getLexer().is(AsmToken::Register)) {
246       if (ParseRegister(Op))
247         return true;
248       IndexReg = Op.getReg();
249       Scale = 1;      // If not specified, the scale defaults to 1.
250     
251       if (getLexer().isNot(AsmToken::RParen)) {
252         // Parse the scale amount:
253         //  ::= ',' [scale-expression]
254         if (getLexer().isNot(AsmToken::Comma))
255           return true;
256         getLexer().Lex(); // Eat the comma.
257
258         if (getLexer().isNot(AsmToken::RParen)) {
259           SMLoc Loc = getLexer().getTok().getLoc();
260
261           int64_t ScaleVal;
262           if (getParser().ParseAbsoluteExpression(ScaleVal))
263             return true;
264           
265           // Validate the scale amount.
266           if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
267             return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
268           Scale = (unsigned)ScaleVal;
269         }
270       }
271     } else if (getLexer().isNot(AsmToken::RParen)) {
272       // Otherwise we have the unsupported form of a scale amount without an
273       // index.
274       SMLoc Loc = getLexer().getTok().getLoc();
275
276       int64_t Value;
277       if (getParser().ParseAbsoluteExpression(Value))
278         return true;
279       
280       return Error(Loc, "cannot have scale factor without index register");
281     }
282   }
283   
284   // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
285   if (getLexer().isNot(AsmToken::RParen))
286     return Error(getLexer().getTok().getLoc(),
287                     "unexpected token in memory operand");
288   getLexer().Lex(); // Eat the ')'.
289   
290   Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
291   return false;
292 }
293
294 bool
295 X86ATTAsmParser::MatchInstruction(const StringRef &Name,
296                                   SmallVectorImpl<X86Operand> &Operands,
297                                   MCInst &Inst) {
298   return false;
299 }
300
301 bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
302   SmallVector<X86Operand, 3> Operands;
303
304   if (getLexer().isNot(AsmToken::EndOfStatement)) {
305     // Read the first operand.
306     Operands.push_back(X86Operand());
307     if (ParseOperand(Operands.back()))
308       return true;
309
310     while (getLexer().is(AsmToken::Comma)) {
311       getLexer().Lex();  // Eat the comma.
312
313       // Parse and remember the operand.
314       Operands.push_back(X86Operand());
315       if (ParseOperand(Operands.back()))
316         return true;
317     }
318   }
319
320   return MatchInstruction(Name, Operands, Inst);
321 }
322
323 // Force static initialization.
324 extern "C" void LLVMInitializeX86AsmParser() {
325   RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
326   RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
327 }
328
329 #include "X86GenAsmMatcher.inc"