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