[Sparc] Add support for parsing directives in SparcAsmParser.
[oota-llvm.git] / lib / Target / Sparc / AsmParser / SparcAsmParser.cpp
1 //===-- SparcAsmParser.cpp - Parse Sparc 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 "MCTargetDesc/SparcMCTargetDesc.h"
11 #include "MCTargetDesc/SparcMCExpr.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 #include "llvm/MC/MCTargetAsmParser.h"
19 #include "llvm/Support/TargetRegistry.h"
20
21 using namespace llvm;
22
23 // The generated AsmMatcher SparcGenAsmMatcher uses "Sparc" as the target
24 // namespace. But SPARC backend uses "SP" as its namespace.
25 namespace llvm {
26   namespace Sparc {
27     using namespace SP;
28   }
29 }
30
31 namespace {
32 class SparcOperand;
33 class SparcAsmParser : public MCTargetAsmParser {
34
35   MCSubtargetInfo &STI;
36   MCAsmParser &Parser;
37
38   /// @name Auto-generated Match Functions
39   /// {
40
41 #define GET_ASSEMBLER_HEADER
42 #include "SparcGenAsmMatcher.inc"
43
44   /// }
45
46   // public interface of the MCTargetAsmParser.
47   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
48                                SmallVectorImpl<MCParsedAsmOperand*> &Operands,
49                                MCStreamer &Out, unsigned &ErrorInfo,
50                                bool MatchingInlineAsm);
51   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
52   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
53                         SMLoc NameLoc,
54                         SmallVectorImpl<MCParsedAsmOperand*> &Operands);
55   bool ParseDirective(AsmToken DirectiveID);
56
57   virtual unsigned validateTargetOperandClass(MCParsedAsmOperand *Op,
58                                               unsigned Kind);
59
60   // Custom parse functions for Sparc specific operands.
61   OperandMatchResultTy
62   parseMEMOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
63
64   OperandMatchResultTy
65   parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
66                StringRef Name);
67
68   OperandMatchResultTy
69   parseSparcAsmOperand(SparcOperand *&Operand);
70
71   // returns true if Tok is matched to a register and returns register in RegNo.
72   bool matchRegisterName(const AsmToken &Tok, unsigned &RegNo,
73                          unsigned &RegKind);
74
75   bool matchSparcAsmModifiers(const MCExpr *&EVal, SMLoc &EndLoc);
76   bool parseDirectiveWord(unsigned Size, SMLoc L);
77
78   bool is64Bit() const { return STI.getTargetTriple().startswith("sparcv9"); }
79 public:
80   SparcAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
81                 const MCInstrInfo &MII)
82       : MCTargetAsmParser(), STI(sti), Parser(parser) {
83     // Initialize the set of available features.
84     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
85   }
86
87 };
88
89   static unsigned IntRegs[32] = {
90     Sparc::G0, Sparc::G1, Sparc::G2, Sparc::G3,
91     Sparc::G4, Sparc::G5, Sparc::G6, Sparc::G7,
92     Sparc::O0, Sparc::O1, Sparc::O2, Sparc::O3,
93     Sparc::O4, Sparc::O5, Sparc::O6, Sparc::O7,
94     Sparc::L0, Sparc::L1, Sparc::L2, Sparc::L3,
95     Sparc::L4, Sparc::L5, Sparc::L6, Sparc::L7,
96     Sparc::I0, Sparc::I1, Sparc::I2, Sparc::I3,
97     Sparc::I4, Sparc::I5, Sparc::I6, Sparc::I7 };
98
99   static unsigned FloatRegs[32] = {
100     Sparc::F0,  Sparc::F1,  Sparc::F2,  Sparc::F3,
101     Sparc::F4,  Sparc::F5,  Sparc::F6,  Sparc::F7,
102     Sparc::F8,  Sparc::F9,  Sparc::F10, Sparc::F11,
103     Sparc::F12, Sparc::F13, Sparc::F14, Sparc::F15,
104     Sparc::F16, Sparc::F17, Sparc::F18, Sparc::F19,
105     Sparc::F20, Sparc::F21, Sparc::F22, Sparc::F23,
106     Sparc::F24, Sparc::F25, Sparc::F26, Sparc::F27,
107     Sparc::F28, Sparc::F29, Sparc::F30, Sparc::F31 };
108
109   static unsigned DoubleRegs[32] = {
110     Sparc::D0,  Sparc::D1,  Sparc::D2,  Sparc::D3,
111     Sparc::D4,  Sparc::D5,  Sparc::D6,  Sparc::D7,
112     Sparc::D8,  Sparc::D7,  Sparc::D8,  Sparc::D9,
113     Sparc::D12, Sparc::D13, Sparc::D14, Sparc::D15,
114     Sparc::D16, Sparc::D17, Sparc::D18, Sparc::D19,
115     Sparc::D20, Sparc::D21, Sparc::D22, Sparc::D23,
116     Sparc::D24, Sparc::D25, Sparc::D26, Sparc::D27,
117     Sparc::D28, Sparc::D29, Sparc::D30, Sparc::D31 };
118
119   static unsigned QuadFPRegs[32] = {
120     Sparc::Q0,  Sparc::Q1,  Sparc::Q2,  Sparc::Q3,
121     Sparc::Q4,  Sparc::Q5,  Sparc::Q6,  Sparc::Q7,
122     Sparc::Q8,  Sparc::Q9,  Sparc::Q10, Sparc::Q11,
123     Sparc::Q12, Sparc::Q13, Sparc::Q14, Sparc::Q15 };
124
125
126 /// SparcOperand - Instances of this class represent a parsed Sparc machine
127 /// instruction.
128 class SparcOperand : public MCParsedAsmOperand {
129 public:
130   enum RegisterKind {
131     rk_None,
132     rk_IntReg,
133     rk_FloatReg,
134     rk_DoubleReg,
135     rk_QuadReg,
136     rk_CCReg,
137     rk_Y
138   };
139 private:
140   enum KindTy {
141     k_Token,
142     k_Register,
143     k_Immediate,
144     k_MemoryReg,
145     k_MemoryImm
146   } Kind;
147
148   SMLoc StartLoc, EndLoc;
149
150   SparcOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
151
152   struct Token {
153     const char *Data;
154     unsigned Length;
155   };
156
157   struct RegOp {
158     unsigned RegNum;
159     RegisterKind Kind;
160   };
161
162   struct ImmOp {
163     const MCExpr *Val;
164   };
165
166   struct MemOp {
167     unsigned Base;
168     unsigned OffsetReg;
169     const MCExpr *Off;
170   };
171
172   union {
173     struct Token Tok;
174     struct RegOp Reg;
175     struct ImmOp Imm;
176     struct MemOp Mem;
177   };
178 public:
179   bool isToken() const { return Kind == k_Token; }
180   bool isReg() const { return Kind == k_Register; }
181   bool isImm() const { return Kind == k_Immediate; }
182   bool isMem() const { return isMEMrr() || isMEMri(); }
183   bool isMEMrr() const { return Kind == k_MemoryReg; }
184   bool isMEMri() const { return Kind == k_MemoryImm; }
185
186   bool isFloatReg() const {
187     return (Kind == k_Register && Reg.Kind == rk_FloatReg);
188   }
189
190   bool isFloatOrDoubleReg() const {
191     return (Kind == k_Register && (Reg.Kind == rk_FloatReg
192                                    || Reg.Kind == rk_DoubleReg));
193   }
194
195
196   StringRef getToken() const {
197     assert(Kind == k_Token && "Invalid access!");
198     return StringRef(Tok.Data, Tok.Length);
199   }
200
201   unsigned getReg() const {
202     assert((Kind == k_Register) && "Invalid access!");
203     return Reg.RegNum;
204   }
205
206   const MCExpr *getImm() const {
207     assert((Kind == k_Immediate) && "Invalid access!");
208     return Imm.Val;
209   }
210
211   unsigned getMemBase() const {
212     assert((Kind == k_MemoryReg || Kind == k_MemoryImm) && "Invalid access!");
213     return Mem.Base;
214   }
215
216   unsigned getMemOffsetReg() const {
217     assert((Kind == k_MemoryReg) && "Invalid access!");
218     return Mem.OffsetReg;
219   }
220
221   const MCExpr *getMemOff() const {
222     assert((Kind == k_MemoryImm) && "Invalid access!");
223     return Mem.Off;
224   }
225
226   /// getStartLoc - Get the location of the first token of this operand.
227   SMLoc getStartLoc() const {
228     return StartLoc;
229   }
230   /// getEndLoc - Get the location of the last token of this operand.
231   SMLoc getEndLoc() const {
232     return EndLoc;
233   }
234
235   virtual void print(raw_ostream &OS) const {
236     switch (Kind) {
237     case k_Token:     OS << "Token: " << getToken() << "\n"; break;
238     case k_Register:  OS << "Reg: #" << getReg() << "\n"; break;
239     case k_Immediate: OS << "Imm: " << getImm() << "\n"; break;
240     case k_MemoryReg: OS << "Mem: " << getMemBase() << "+"
241                          << getMemOffsetReg() << "\n"; break;
242     case k_MemoryImm: assert(getMemOff() != 0);
243       OS << "Mem: " << getMemBase()
244          << "+" << *getMemOff()
245          << "\n"; break;
246     }
247   }
248
249   void addRegOperands(MCInst &Inst, unsigned N) const {
250     assert(N == 1 && "Invalid number of operands!");
251     Inst.addOperand(MCOperand::CreateReg(getReg()));
252   }
253
254   void addImmOperands(MCInst &Inst, unsigned N) const {
255     assert(N == 1 && "Invalid number of operands!");
256     const MCExpr *Expr = getImm();
257     addExpr(Inst, Expr);
258   }
259
260   void addExpr(MCInst &Inst, const MCExpr *Expr) const{
261     // Add as immediate when possible.  Null MCExpr = 0.
262     if (Expr == 0)
263       Inst.addOperand(MCOperand::CreateImm(0));
264     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
265       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
266     else
267       Inst.addOperand(MCOperand::CreateExpr(Expr));
268   }
269
270   void addMEMrrOperands(MCInst &Inst, unsigned N) const {
271     assert(N == 2 && "Invalid number of operands!");
272
273     Inst.addOperand(MCOperand::CreateReg(getMemBase()));
274
275     assert(getMemOffsetReg() != 0 && "Invalid offset");
276     Inst.addOperand(MCOperand::CreateReg(getMemOffsetReg()));
277   }
278
279   void addMEMriOperands(MCInst &Inst, unsigned N) const {
280     assert(N == 2 && "Invalid number of operands!");
281
282     Inst.addOperand(MCOperand::CreateReg(getMemBase()));
283
284     const MCExpr *Expr = getMemOff();
285     addExpr(Inst, Expr);
286   }
287
288   static SparcOperand *CreateToken(StringRef Str, SMLoc S) {
289     SparcOperand *Op = new SparcOperand(k_Token);
290     Op->Tok.Data = Str.data();
291     Op->Tok.Length = Str.size();
292     Op->StartLoc = S;
293     Op->EndLoc = S;
294     return Op;
295   }
296
297   static SparcOperand *CreateReg(unsigned RegNum,
298                                  unsigned Kind,
299                                  SMLoc S, SMLoc E) {
300     SparcOperand *Op = new SparcOperand(k_Register);
301     Op->Reg.RegNum = RegNum;
302     Op->Reg.Kind   = (SparcOperand::RegisterKind)Kind;
303     Op->StartLoc = S;
304     Op->EndLoc = E;
305     return Op;
306   }
307
308   static SparcOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
309     SparcOperand *Op = new SparcOperand(k_Immediate);
310     Op->Imm.Val = Val;
311     Op->StartLoc = S;
312     Op->EndLoc = E;
313     return Op;
314   }
315
316   static SparcOperand *MorphToDoubleReg(SparcOperand *Op) {
317     unsigned Reg = Op->getReg();
318     assert(Op->Reg.Kind == rk_FloatReg);
319     unsigned regIdx = Reg - Sparc::F0;
320     if (regIdx % 2 || regIdx > 31)
321       return 0;
322     Op->Reg.RegNum = DoubleRegs[regIdx / 2];
323     Op->Reg.Kind = rk_DoubleReg;
324     return Op;
325   }
326
327   static SparcOperand *MorphToQuadReg(SparcOperand *Op) {
328     unsigned Reg = Op->getReg();
329     unsigned regIdx = 0;
330     switch (Op->Reg.Kind) {
331     default: assert(0 && "Unexpected register kind!");
332     case rk_FloatReg:
333       regIdx = Reg - Sparc::F0;
334       if (regIdx % 4 || regIdx > 31)
335         return 0;
336       Reg = QuadFPRegs[regIdx / 4];
337       break;
338     case rk_DoubleReg:
339       regIdx =  Reg - Sparc::D0;
340       if (regIdx % 2 || regIdx > 31)
341         return 0;
342       Reg = QuadFPRegs[regIdx / 2];
343       break;
344     }
345     Op->Reg.RegNum  = Reg;
346     Op->Reg.Kind = rk_QuadReg;
347     return Op;
348   }
349
350   static SparcOperand *MorphToMEMrr(unsigned Base, SparcOperand *Op) {
351     unsigned offsetReg = Op->getReg();
352     Op->Kind = k_MemoryReg;
353     Op->Mem.Base = Base;
354     Op->Mem.OffsetReg = offsetReg;
355     Op->Mem.Off = 0;
356     return Op;
357   }
358
359   static SparcOperand *CreateMEMri(unsigned Base,
360                                  const MCExpr *Off,
361                                  SMLoc S, SMLoc E) {
362     SparcOperand *Op = new SparcOperand(k_MemoryImm);
363     Op->Mem.Base = Base;
364     Op->Mem.OffsetReg = 0;
365     Op->Mem.Off = Off;
366     Op->StartLoc = S;
367     Op->EndLoc = E;
368     return Op;
369   }
370
371   static SparcOperand *MorphToMEMri(unsigned Base, SparcOperand *Op) {
372     const MCExpr *Imm  = Op->getImm();
373     Op->Kind = k_MemoryImm;
374     Op->Mem.Base = Base;
375     Op->Mem.OffsetReg = 0;
376     Op->Mem.Off = Imm;
377     return Op;
378   }
379 };
380
381 } // end namespace
382
383 bool SparcAsmParser::
384 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
385                         SmallVectorImpl<MCParsedAsmOperand*> &Operands,
386                         MCStreamer &Out, unsigned &ErrorInfo,
387                         bool MatchingInlineAsm) {
388   MCInst Inst;
389   SmallVector<MCInst, 8> Instructions;
390   unsigned MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
391                                               MatchingInlineAsm);
392   switch (MatchResult) {
393   default:
394     break;
395
396   case Match_Success: {
397     Inst.setLoc(IDLoc);
398     Out.EmitInstruction(Inst, STI);
399     return false;
400   }
401
402   case Match_MissingFeature:
403     return Error(IDLoc,
404                  "instruction requires a CPU feature not currently enabled");
405
406   case Match_InvalidOperand: {
407     SMLoc ErrorLoc = IDLoc;
408     if (ErrorInfo != ~0U) {
409       if (ErrorInfo >= Operands.size())
410         return Error(IDLoc, "too few operands for instruction");
411
412       ErrorLoc = ((SparcOperand*) Operands[ErrorInfo])->getStartLoc();
413       if (ErrorLoc == SMLoc())
414         ErrorLoc = IDLoc;
415     }
416
417     return Error(ErrorLoc, "invalid operand for instruction");
418   }
419   case Match_MnemonicFail:
420     return Error(IDLoc, "invalid instruction");
421   }
422   return true;
423 }
424
425 bool SparcAsmParser::
426 ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc)
427 {
428   const AsmToken &Tok = Parser.getTok();
429   StartLoc = Tok.getLoc();
430   EndLoc = Tok.getEndLoc();
431   RegNo = 0;
432   if (getLexer().getKind() != AsmToken::Percent)
433     return false;
434   Parser.Lex();
435   unsigned regKind = SparcOperand::rk_None;
436   if (matchRegisterName(Tok, RegNo, regKind)) {
437     Parser.Lex();
438     return false;
439   }
440
441   return Error(StartLoc, "invalid register name");
442 }
443
444 bool SparcAsmParser::
445 ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
446                  SMLoc NameLoc,
447                  SmallVectorImpl<MCParsedAsmOperand*> &Operands)
448 {
449   // Check if we have valid mnemonic.
450   if (!mnemonicIsValid(Name, 0)) {
451     Parser.eatToEndOfStatement();
452     return Error(NameLoc, "Unknown instruction");
453   }
454   // First operand in MCInst is instruction mnemonic.
455   Operands.push_back(SparcOperand::CreateToken(Name, NameLoc));
456
457   if (getLexer().isNot(AsmToken::EndOfStatement)) {
458     // Read the first operand.
459     if (parseOperand(Operands, Name) != MatchOperand_Success) {
460       SMLoc Loc = getLexer().getLoc();
461       Parser.eatToEndOfStatement();
462       return Error(Loc, "unexpected token");
463     }
464
465     while (getLexer().is(AsmToken::Comma)) {
466       Parser.Lex(); // Eat the comma.
467       // Parse and remember the operand.
468       if (parseOperand(Operands, Name) != MatchOperand_Success) {
469         SMLoc Loc = getLexer().getLoc();
470         Parser.eatToEndOfStatement();
471         return Error(Loc, "unexpected token");
472       }
473     }
474   }
475   if (getLexer().isNot(AsmToken::EndOfStatement)) {
476     SMLoc Loc = getLexer().getLoc();
477     Parser.eatToEndOfStatement();
478     return Error(Loc, "unexpected token");
479   }
480   Parser.Lex(); // Consume the EndOfStatement.
481   return false;
482 }
483
484 bool SparcAsmParser::
485 ParseDirective(AsmToken DirectiveID)
486 {
487   StringRef IDVal = DirectiveID.getString();
488
489   if (IDVal == ".byte")
490     return parseDirectiveWord(1, DirectiveID.getLoc());
491
492   if (IDVal == ".half")
493     return parseDirectiveWord(2, DirectiveID.getLoc());
494
495   if (IDVal == ".word")
496     return parseDirectiveWord(4, DirectiveID.getLoc());
497
498   if (IDVal == ".nword")
499     return parseDirectiveWord(is64Bit() ? 8 : 4, DirectiveID.getLoc());
500
501   if (is64Bit() && IDVal == ".xword")
502     return parseDirectiveWord(8, DirectiveID.getLoc());
503
504   if (IDVal == ".register") {
505     // For now, ignore .register directive.
506     Parser.eatToEndOfStatement();
507     return false;
508   }
509
510   // Let the MC layer to handle other directives.
511   return true;
512 }
513
514 bool SparcAsmParser:: parseDirectiveWord(unsigned Size, SMLoc L) {
515   if (getLexer().isNot(AsmToken::EndOfStatement)) {
516     for (;;) {
517       const MCExpr *Value;
518       if (getParser().parseExpression(Value))
519         return true;
520
521       getParser().getStreamer().EmitValue(Value, Size);
522
523       if (getLexer().is(AsmToken::EndOfStatement))
524         break;
525
526       // FIXME: Improve diagnostic.
527       if (getLexer().isNot(AsmToken::Comma))
528         return Error(L, "unexpected token in directive");
529       Parser.Lex();
530     }
531   }
532   Parser.Lex();
533   return false;
534 }
535
536 SparcAsmParser::OperandMatchResultTy SparcAsmParser::
537 parseMEMOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands)
538 {
539
540   SMLoc S, E;
541   unsigned BaseReg = 0;
542
543   if (ParseRegister(BaseReg, S, E)) {
544     return MatchOperand_NoMatch;
545   }
546
547   switch (getLexer().getKind()) {
548   default: return MatchOperand_NoMatch;
549
550   case AsmToken::Comma:
551   case AsmToken::RBrac:
552   case AsmToken::EndOfStatement:
553     Operands.push_back(SparcOperand::CreateMEMri(BaseReg, 0, S, E));
554     return MatchOperand_Success;
555
556   case AsmToken:: Plus:
557     Parser.Lex(); // Eat the '+'
558     break;
559   case AsmToken::Minus:
560     break;
561   }
562
563   SparcOperand *Offset = 0;
564   OperandMatchResultTy ResTy = parseSparcAsmOperand(Offset);
565   if (ResTy != MatchOperand_Success || !Offset)
566     return MatchOperand_NoMatch;
567
568   Offset = (Offset->isImm()
569             ? SparcOperand::MorphToMEMri(BaseReg, Offset)
570             : SparcOperand::MorphToMEMrr(BaseReg, Offset));
571
572   Operands.push_back(Offset);
573   return MatchOperand_Success;
574 }
575
576 SparcAsmParser::OperandMatchResultTy SparcAsmParser::
577 parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
578              StringRef Mnemonic)
579 {
580
581   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
582
583   // If there wasn't a custom match, try the generic matcher below. Otherwise,
584   // there was a match, but an error occurred, in which case, just return that
585   // the operand parsing failed.
586   if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail)
587     return ResTy;
588
589   if (getLexer().is(AsmToken::LBrac)) {
590     // Memory operand
591     Operands.push_back(SparcOperand::CreateToken("[",
592                                                  Parser.getTok().getLoc()));
593     Parser.Lex(); // Eat the [
594
595     if (Mnemonic == "cas" || Mnemonic == "casx") {
596       SMLoc S = Parser.getTok().getLoc();
597       if (getLexer().getKind() != AsmToken::Percent)
598         return MatchOperand_NoMatch;
599       Parser.Lex(); // eat %
600
601       unsigned RegNo, RegKind;
602       if (!matchRegisterName(Parser.getTok(), RegNo, RegKind))
603         return MatchOperand_NoMatch;
604
605       Parser.Lex(); // Eat the identifier token.
606       SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer()-1);
607       Operands.push_back(SparcOperand::CreateReg(RegNo, RegKind, S, E));
608       ResTy = MatchOperand_Success;
609     } else {
610       ResTy = parseMEMOperand(Operands);
611     }
612
613     if (ResTy != MatchOperand_Success)
614       return ResTy;
615
616     if (!getLexer().is(AsmToken::RBrac))
617       return MatchOperand_ParseFail;
618
619     Operands.push_back(SparcOperand::CreateToken("]",
620                                                  Parser.getTok().getLoc()));
621     Parser.Lex(); // Eat the ]
622     return MatchOperand_Success;
623   }
624
625   SparcOperand *Op = 0;
626   ResTy = parseSparcAsmOperand(Op);
627   if (ResTy != MatchOperand_Success || !Op)
628     return MatchOperand_ParseFail;
629
630   // Push the parsed operand into the list of operands
631   Operands.push_back(Op);
632
633   return MatchOperand_Success;
634 }
635
636 SparcAsmParser::OperandMatchResultTy
637 SparcAsmParser::parseSparcAsmOperand(SparcOperand *&Op)
638 {
639
640   SMLoc S = Parser.getTok().getLoc();
641   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
642   const MCExpr *EVal;
643
644   Op = 0;
645   switch (getLexer().getKind()) {
646   default:  break;
647
648   case AsmToken::Percent:
649     Parser.Lex(); // Eat the '%'.
650     unsigned RegNo;
651     unsigned RegKind;
652     if (matchRegisterName(Parser.getTok(), RegNo, RegKind)) {
653       StringRef name = Parser.getTok().getString();
654       Parser.Lex(); // Eat the identifier token.
655       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
656       switch (RegNo) {
657       default:
658         Op = SparcOperand::CreateReg(RegNo, RegKind, S, E);
659         break;
660       case Sparc::Y:
661         Op = SparcOperand::CreateToken("%y", S);
662         break;
663
664       case Sparc::ICC:
665         if (name == "xcc")
666           Op = SparcOperand::CreateToken("%xcc", S);
667         else
668           Op = SparcOperand::CreateToken("%icc", S);
669         break;
670
671       case Sparc::FCC:
672         assert(name == "fcc0" && "Cannot handle %fcc other than %fcc0 yet");
673         Op = SparcOperand::CreateToken("%fcc0", S);
674         break;
675       }
676       break;
677     }
678     if (matchSparcAsmModifiers(EVal, E)) {
679       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
680       Op = SparcOperand::CreateImm(EVal, S, E);
681     }
682     break;
683
684   case AsmToken::Minus:
685   case AsmToken::Integer:
686     if (!getParser().parseExpression(EVal, E))
687       Op = SparcOperand::CreateImm(EVal, S, E);
688     break;
689
690   case AsmToken::Identifier: {
691     StringRef Identifier;
692     if (!getParser().parseIdentifier(Identifier)) {
693       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
694       MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
695
696       const MCExpr *Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
697                                                   getContext());
698       Op = SparcOperand::CreateImm(Res, S, E);
699     }
700     break;
701   }
702   }
703   return (Op) ? MatchOperand_Success : MatchOperand_ParseFail;
704 }
705
706 bool SparcAsmParser::matchRegisterName(const AsmToken &Tok,
707                                        unsigned &RegNo,
708                                        unsigned &RegKind)
709 {
710   int64_t intVal = 0;
711   RegNo = 0;
712   RegKind = SparcOperand::rk_None;
713   if (Tok.is(AsmToken::Identifier)) {
714     StringRef name = Tok.getString();
715
716     // %fp
717     if (name.equals("fp")) {
718       RegNo = Sparc::I6;
719       RegKind = SparcOperand::rk_IntReg;
720       return true;
721     }
722     // %sp
723     if (name.equals("sp")) {
724       RegNo = Sparc::O6;
725       RegKind = SparcOperand::rk_IntReg;
726       return true;
727     }
728
729     if (name.equals("y")) {
730       RegNo = Sparc::Y;
731       RegKind = SparcOperand::rk_Y;
732       return true;
733     }
734
735     if (name.equals("icc")) {
736       RegNo = Sparc::ICC;
737       RegKind = SparcOperand::rk_CCReg;
738       return true;
739     }
740
741     if (name.equals("xcc")) {
742       // FIXME:: check 64bit.
743       RegNo = Sparc::ICC;
744       RegKind = SparcOperand::rk_CCReg;
745       return true;
746     }
747
748     // %fcc0 - %fcc3
749     if (name.substr(0, 3).equals_lower("fcc")
750         && !name.substr(3).getAsInteger(10, intVal)
751         && intVal < 4) {
752       // FIXME: check 64bit and  handle %fcc1 - %fcc3
753       RegNo = Sparc::FCC;
754       RegKind = SparcOperand::rk_CCReg;
755       return true;
756     }
757
758     // %g0 - %g7
759     if (name.substr(0, 1).equals_lower("g")
760         && !name.substr(1).getAsInteger(10, intVal)
761         && intVal < 8) {
762       RegNo = IntRegs[intVal];
763       RegKind = SparcOperand::rk_IntReg;
764       return true;
765     }
766     // %o0 - %o7
767     if (name.substr(0, 1).equals_lower("o")
768         && !name.substr(1).getAsInteger(10, intVal)
769         && intVal < 8) {
770       RegNo = IntRegs[8 + intVal];
771       RegKind = SparcOperand::rk_IntReg;
772       return true;
773     }
774     if (name.substr(0, 1).equals_lower("l")
775         && !name.substr(1).getAsInteger(10, intVal)
776         && intVal < 8) {
777       RegNo = IntRegs[16 + intVal];
778       RegKind = SparcOperand::rk_IntReg;
779       return true;
780     }
781     if (name.substr(0, 1).equals_lower("i")
782         && !name.substr(1).getAsInteger(10, intVal)
783         && intVal < 8) {
784       RegNo = IntRegs[24 + intVal];
785       RegKind = SparcOperand::rk_IntReg;
786       return true;
787     }
788     // %f0 - %f31
789     if (name.substr(0, 1).equals_lower("f")
790         && !name.substr(1, 2).getAsInteger(10, intVal) && intVal < 32) {
791       RegNo = FloatRegs[intVal];
792       RegKind = SparcOperand::rk_FloatReg;
793       return true;
794     }
795     // %f32 - %f62
796     if (name.substr(0, 1).equals_lower("f")
797         && !name.substr(1, 2).getAsInteger(10, intVal)
798         && intVal >= 32 && intVal <= 62 && (intVal % 2 == 0)) {
799       // FIXME: Check V9
800       RegNo = DoubleRegs[intVal/2];
801       RegKind = SparcOperand::rk_DoubleReg;
802       return true;
803     }
804
805     // %r0 - %r31
806     if (name.substr(0, 1).equals_lower("r")
807         && !name.substr(1, 2).getAsInteger(10, intVal) && intVal < 31) {
808       RegNo = IntRegs[intVal];
809       RegKind = SparcOperand::rk_IntReg;
810       return true;
811     }
812   }
813   return false;
814 }
815
816
817 bool SparcAsmParser::matchSparcAsmModifiers(const MCExpr *&EVal,
818                                             SMLoc &EndLoc)
819 {
820   AsmToken Tok = Parser.getTok();
821   if (!Tok.is(AsmToken::Identifier))
822     return false;
823
824   StringRef name = Tok.getString();
825
826   SparcMCExpr::VariantKind VK = SparcMCExpr::parseVariantKind(name);
827
828   if (VK == SparcMCExpr::VK_Sparc_None)
829     return false;
830
831   Parser.Lex(); // Eat the identifier.
832   if (Parser.getTok().getKind() != AsmToken::LParen)
833     return false;
834
835   Parser.Lex(); // Eat the LParen token.
836   const MCExpr *subExpr;
837   if (Parser.parseParenExpression(subExpr, EndLoc))
838     return false;
839   EVal = SparcMCExpr::Create(VK, subExpr, getContext());
840   return true;
841 }
842
843
844 extern "C" void LLVMInitializeSparcAsmParser() {
845   RegisterMCAsmParser<SparcAsmParser> A(TheSparcTarget);
846   RegisterMCAsmParser<SparcAsmParser> B(TheSparcV9Target);
847 }
848
849 #define GET_REGISTER_MATCHER
850 #define GET_MATCHER_IMPLEMENTATION
851 #include "SparcGenAsmMatcher.inc"
852
853
854
855 unsigned SparcAsmParser::
856 validateTargetOperandClass(MCParsedAsmOperand *GOp,
857                            unsigned Kind)
858 {
859   SparcOperand *Op = (SparcOperand*)GOp;
860   if (Op->isFloatOrDoubleReg()) {
861     switch (Kind) {
862     default: break;
863     case MCK_DFPRegs:
864       if (!Op->isFloatReg() || SparcOperand::MorphToDoubleReg(Op))
865         return MCTargetAsmParser::Match_Success;
866       break;
867     case MCK_QFPRegs:
868       if (SparcOperand::MorphToQuadReg(Op))
869         return MCTargetAsmParser::Match_Success;
870       break;
871     }
872   }
873   return Match_InvalidOperand;
874 }