xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- PPCAsmParser.cpp - Parse PowerPC asm to MCInst instructions ---------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "MCTargetDesc/PPCMCTargetDesc.h"
11f4a2713aSLionel Sambuc #include "MCTargetDesc/PPCMCExpr.h"
12*0a6a1f1dSLionel Sambuc #include "PPCTargetStreamer.h"
13f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
14f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
16f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
17f4a2713aSLionel Sambuc #include "llvm/ADT/Twine.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCContext.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCExpr.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInst.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInstrInfo.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCParser/MCAsmLexer.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCParser/MCAsmParser.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCRegisterInfo.h"
26*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCStreamer.h"
27*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCSubtargetInfo.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCTargetAsmParser.h"
29f4a2713aSLionel Sambuc #include "llvm/Support/SourceMgr.h"
30f4a2713aSLionel Sambuc #include "llvm/Support/TargetRegistry.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc using namespace llvm;
34f4a2713aSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc static const MCPhysReg RRegs[32] = {
36f4a2713aSLionel Sambuc   PPC::R0,  PPC::R1,  PPC::R2,  PPC::R3,
37f4a2713aSLionel Sambuc   PPC::R4,  PPC::R5,  PPC::R6,  PPC::R7,
38f4a2713aSLionel Sambuc   PPC::R8,  PPC::R9,  PPC::R10, PPC::R11,
39f4a2713aSLionel Sambuc   PPC::R12, PPC::R13, PPC::R14, PPC::R15,
40f4a2713aSLionel Sambuc   PPC::R16, PPC::R17, PPC::R18, PPC::R19,
41f4a2713aSLionel Sambuc   PPC::R20, PPC::R21, PPC::R22, PPC::R23,
42f4a2713aSLionel Sambuc   PPC::R24, PPC::R25, PPC::R26, PPC::R27,
43f4a2713aSLionel Sambuc   PPC::R28, PPC::R29, PPC::R30, PPC::R31
44f4a2713aSLionel Sambuc };
45*0a6a1f1dSLionel Sambuc static const MCPhysReg RRegsNoR0[32] = {
46f4a2713aSLionel Sambuc   PPC::ZERO,
47f4a2713aSLionel Sambuc             PPC::R1,  PPC::R2,  PPC::R3,
48f4a2713aSLionel Sambuc   PPC::R4,  PPC::R5,  PPC::R6,  PPC::R7,
49f4a2713aSLionel Sambuc   PPC::R8,  PPC::R9,  PPC::R10, PPC::R11,
50f4a2713aSLionel Sambuc   PPC::R12, PPC::R13, PPC::R14, PPC::R15,
51f4a2713aSLionel Sambuc   PPC::R16, PPC::R17, PPC::R18, PPC::R19,
52f4a2713aSLionel Sambuc   PPC::R20, PPC::R21, PPC::R22, PPC::R23,
53f4a2713aSLionel Sambuc   PPC::R24, PPC::R25, PPC::R26, PPC::R27,
54f4a2713aSLionel Sambuc   PPC::R28, PPC::R29, PPC::R30, PPC::R31
55f4a2713aSLionel Sambuc };
56*0a6a1f1dSLionel Sambuc static const MCPhysReg XRegs[32] = {
57f4a2713aSLionel Sambuc   PPC::X0,  PPC::X1,  PPC::X2,  PPC::X3,
58f4a2713aSLionel Sambuc   PPC::X4,  PPC::X5,  PPC::X6,  PPC::X7,
59f4a2713aSLionel Sambuc   PPC::X8,  PPC::X9,  PPC::X10, PPC::X11,
60f4a2713aSLionel Sambuc   PPC::X12, PPC::X13, PPC::X14, PPC::X15,
61f4a2713aSLionel Sambuc   PPC::X16, PPC::X17, PPC::X18, PPC::X19,
62f4a2713aSLionel Sambuc   PPC::X20, PPC::X21, PPC::X22, PPC::X23,
63f4a2713aSLionel Sambuc   PPC::X24, PPC::X25, PPC::X26, PPC::X27,
64f4a2713aSLionel Sambuc   PPC::X28, PPC::X29, PPC::X30, PPC::X31
65f4a2713aSLionel Sambuc };
66*0a6a1f1dSLionel Sambuc static const MCPhysReg XRegsNoX0[32] = {
67f4a2713aSLionel Sambuc   PPC::ZERO8,
68f4a2713aSLionel Sambuc             PPC::X1,  PPC::X2,  PPC::X3,
69f4a2713aSLionel Sambuc   PPC::X4,  PPC::X5,  PPC::X6,  PPC::X7,
70f4a2713aSLionel Sambuc   PPC::X8,  PPC::X9,  PPC::X10, PPC::X11,
71f4a2713aSLionel Sambuc   PPC::X12, PPC::X13, PPC::X14, PPC::X15,
72f4a2713aSLionel Sambuc   PPC::X16, PPC::X17, PPC::X18, PPC::X19,
73f4a2713aSLionel Sambuc   PPC::X20, PPC::X21, PPC::X22, PPC::X23,
74f4a2713aSLionel Sambuc   PPC::X24, PPC::X25, PPC::X26, PPC::X27,
75f4a2713aSLionel Sambuc   PPC::X28, PPC::X29, PPC::X30, PPC::X31
76f4a2713aSLionel Sambuc };
77*0a6a1f1dSLionel Sambuc static const MCPhysReg FRegs[32] = {
78f4a2713aSLionel Sambuc   PPC::F0,  PPC::F1,  PPC::F2,  PPC::F3,
79f4a2713aSLionel Sambuc   PPC::F4,  PPC::F5,  PPC::F6,  PPC::F7,
80f4a2713aSLionel Sambuc   PPC::F8,  PPC::F9,  PPC::F10, PPC::F11,
81f4a2713aSLionel Sambuc   PPC::F12, PPC::F13, PPC::F14, PPC::F15,
82f4a2713aSLionel Sambuc   PPC::F16, PPC::F17, PPC::F18, PPC::F19,
83f4a2713aSLionel Sambuc   PPC::F20, PPC::F21, PPC::F22, PPC::F23,
84f4a2713aSLionel Sambuc   PPC::F24, PPC::F25, PPC::F26, PPC::F27,
85f4a2713aSLionel Sambuc   PPC::F28, PPC::F29, PPC::F30, PPC::F31
86f4a2713aSLionel Sambuc };
87*0a6a1f1dSLionel Sambuc static const MCPhysReg VRegs[32] = {
88f4a2713aSLionel Sambuc   PPC::V0,  PPC::V1,  PPC::V2,  PPC::V3,
89f4a2713aSLionel Sambuc   PPC::V4,  PPC::V5,  PPC::V6,  PPC::V7,
90f4a2713aSLionel Sambuc   PPC::V8,  PPC::V9,  PPC::V10, PPC::V11,
91f4a2713aSLionel Sambuc   PPC::V12, PPC::V13, PPC::V14, PPC::V15,
92f4a2713aSLionel Sambuc   PPC::V16, PPC::V17, PPC::V18, PPC::V19,
93f4a2713aSLionel Sambuc   PPC::V20, PPC::V21, PPC::V22, PPC::V23,
94f4a2713aSLionel Sambuc   PPC::V24, PPC::V25, PPC::V26, PPC::V27,
95f4a2713aSLionel Sambuc   PPC::V28, PPC::V29, PPC::V30, PPC::V31
96f4a2713aSLionel Sambuc };
97*0a6a1f1dSLionel Sambuc static const MCPhysReg VSRegs[64] = {
98*0a6a1f1dSLionel Sambuc   PPC::VSL0,  PPC::VSL1,  PPC::VSL2,  PPC::VSL3,
99*0a6a1f1dSLionel Sambuc   PPC::VSL4,  PPC::VSL5,  PPC::VSL6,  PPC::VSL7,
100*0a6a1f1dSLionel Sambuc   PPC::VSL8,  PPC::VSL9,  PPC::VSL10, PPC::VSL11,
101*0a6a1f1dSLionel Sambuc   PPC::VSL12, PPC::VSL13, PPC::VSL14, PPC::VSL15,
102*0a6a1f1dSLionel Sambuc   PPC::VSL16, PPC::VSL17, PPC::VSL18, PPC::VSL19,
103*0a6a1f1dSLionel Sambuc   PPC::VSL20, PPC::VSL21, PPC::VSL22, PPC::VSL23,
104*0a6a1f1dSLionel Sambuc   PPC::VSL24, PPC::VSL25, PPC::VSL26, PPC::VSL27,
105*0a6a1f1dSLionel Sambuc   PPC::VSL28, PPC::VSL29, PPC::VSL30, PPC::VSL31,
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc   PPC::VSH0,  PPC::VSH1,  PPC::VSH2,  PPC::VSH3,
108*0a6a1f1dSLionel Sambuc   PPC::VSH4,  PPC::VSH5,  PPC::VSH6,  PPC::VSH7,
109*0a6a1f1dSLionel Sambuc   PPC::VSH8,  PPC::VSH9,  PPC::VSH10, PPC::VSH11,
110*0a6a1f1dSLionel Sambuc   PPC::VSH12, PPC::VSH13, PPC::VSH14, PPC::VSH15,
111*0a6a1f1dSLionel Sambuc   PPC::VSH16, PPC::VSH17, PPC::VSH18, PPC::VSH19,
112*0a6a1f1dSLionel Sambuc   PPC::VSH20, PPC::VSH21, PPC::VSH22, PPC::VSH23,
113*0a6a1f1dSLionel Sambuc   PPC::VSH24, PPC::VSH25, PPC::VSH26, PPC::VSH27,
114*0a6a1f1dSLionel Sambuc   PPC::VSH28, PPC::VSH29, PPC::VSH30, PPC::VSH31
115*0a6a1f1dSLionel Sambuc };
116*0a6a1f1dSLionel Sambuc static const MCPhysReg VSFRegs[64] = {
117*0a6a1f1dSLionel Sambuc   PPC::F0,  PPC::F1,  PPC::F2,  PPC::F3,
118*0a6a1f1dSLionel Sambuc   PPC::F4,  PPC::F5,  PPC::F6,  PPC::F7,
119*0a6a1f1dSLionel Sambuc   PPC::F8,  PPC::F9,  PPC::F10, PPC::F11,
120*0a6a1f1dSLionel Sambuc   PPC::F12, PPC::F13, PPC::F14, PPC::F15,
121*0a6a1f1dSLionel Sambuc   PPC::F16, PPC::F17, PPC::F18, PPC::F19,
122*0a6a1f1dSLionel Sambuc   PPC::F20, PPC::F21, PPC::F22, PPC::F23,
123*0a6a1f1dSLionel Sambuc   PPC::F24, PPC::F25, PPC::F26, PPC::F27,
124*0a6a1f1dSLionel Sambuc   PPC::F28, PPC::F29, PPC::F30, PPC::F31,
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc   PPC::VF0,  PPC::VF1,  PPC::VF2,  PPC::VF3,
127*0a6a1f1dSLionel Sambuc   PPC::VF4,  PPC::VF5,  PPC::VF6,  PPC::VF7,
128*0a6a1f1dSLionel Sambuc   PPC::VF8,  PPC::VF9,  PPC::VF10, PPC::VF11,
129*0a6a1f1dSLionel Sambuc   PPC::VF12, PPC::VF13, PPC::VF14, PPC::VF15,
130*0a6a1f1dSLionel Sambuc   PPC::VF16, PPC::VF17, PPC::VF18, PPC::VF19,
131*0a6a1f1dSLionel Sambuc   PPC::VF20, PPC::VF21, PPC::VF22, PPC::VF23,
132*0a6a1f1dSLionel Sambuc   PPC::VF24, PPC::VF25, PPC::VF26, PPC::VF27,
133*0a6a1f1dSLionel Sambuc   PPC::VF28, PPC::VF29, PPC::VF30, PPC::VF31
134*0a6a1f1dSLionel Sambuc };
135*0a6a1f1dSLionel Sambuc static const MCPhysReg CRBITRegs[32] = {
136f4a2713aSLionel Sambuc   PPC::CR0LT, PPC::CR0GT, PPC::CR0EQ, PPC::CR0UN,
137f4a2713aSLionel Sambuc   PPC::CR1LT, PPC::CR1GT, PPC::CR1EQ, PPC::CR1UN,
138f4a2713aSLionel Sambuc   PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
139f4a2713aSLionel Sambuc   PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
140f4a2713aSLionel Sambuc   PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
141f4a2713aSLionel Sambuc   PPC::CR5LT, PPC::CR5GT, PPC::CR5EQ, PPC::CR5UN,
142f4a2713aSLionel Sambuc   PPC::CR6LT, PPC::CR6GT, PPC::CR6EQ, PPC::CR6UN,
143f4a2713aSLionel Sambuc   PPC::CR7LT, PPC::CR7GT, PPC::CR7EQ, PPC::CR7UN
144f4a2713aSLionel Sambuc };
145*0a6a1f1dSLionel Sambuc static const MCPhysReg CRRegs[8] = {
146f4a2713aSLionel Sambuc   PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
147f4a2713aSLionel Sambuc   PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7
148f4a2713aSLionel Sambuc };
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc // Evaluate an expression containing condition register
151f4a2713aSLionel Sambuc // or condition register field symbols.  Returns positive
152f4a2713aSLionel Sambuc // value on success, or -1 on error.
153f4a2713aSLionel Sambuc static int64_t
EvaluateCRExpr(const MCExpr * E)154f4a2713aSLionel Sambuc EvaluateCRExpr(const MCExpr *E) {
155f4a2713aSLionel Sambuc   switch (E->getKind()) {
156f4a2713aSLionel Sambuc   case MCExpr::Target:
157f4a2713aSLionel Sambuc     return -1;
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   case MCExpr::Constant: {
160f4a2713aSLionel Sambuc     int64_t Res = cast<MCConstantExpr>(E)->getValue();
161f4a2713aSLionel Sambuc     return Res < 0 ? -1 : Res;
162f4a2713aSLionel Sambuc   }
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc   case MCExpr::SymbolRef: {
165f4a2713aSLionel Sambuc     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
166f4a2713aSLionel Sambuc     StringRef Name = SRE->getSymbol().getName();
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc     if (Name == "lt") return 0;
169f4a2713aSLionel Sambuc     if (Name == "gt") return 1;
170f4a2713aSLionel Sambuc     if (Name == "eq") return 2;
171f4a2713aSLionel Sambuc     if (Name == "so") return 3;
172f4a2713aSLionel Sambuc     if (Name == "un") return 3;
173f4a2713aSLionel Sambuc 
174f4a2713aSLionel Sambuc     if (Name == "cr0") return 0;
175f4a2713aSLionel Sambuc     if (Name == "cr1") return 1;
176f4a2713aSLionel Sambuc     if (Name == "cr2") return 2;
177f4a2713aSLionel Sambuc     if (Name == "cr3") return 3;
178f4a2713aSLionel Sambuc     if (Name == "cr4") return 4;
179f4a2713aSLionel Sambuc     if (Name == "cr5") return 5;
180f4a2713aSLionel Sambuc     if (Name == "cr6") return 6;
181f4a2713aSLionel Sambuc     if (Name == "cr7") return 7;
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc     return -1;
184f4a2713aSLionel Sambuc   }
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc   case MCExpr::Unary:
187f4a2713aSLionel Sambuc     return -1;
188f4a2713aSLionel Sambuc 
189f4a2713aSLionel Sambuc   case MCExpr::Binary: {
190f4a2713aSLionel Sambuc     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
191f4a2713aSLionel Sambuc     int64_t LHSVal = EvaluateCRExpr(BE->getLHS());
192f4a2713aSLionel Sambuc     int64_t RHSVal = EvaluateCRExpr(BE->getRHS());
193f4a2713aSLionel Sambuc     int64_t Res;
194f4a2713aSLionel Sambuc 
195f4a2713aSLionel Sambuc     if (LHSVal < 0 || RHSVal < 0)
196f4a2713aSLionel Sambuc       return -1;
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc     switch (BE->getOpcode()) {
199f4a2713aSLionel Sambuc     default: return -1;
200f4a2713aSLionel Sambuc     case MCBinaryExpr::Add: Res = LHSVal + RHSVal; break;
201f4a2713aSLionel Sambuc     case MCBinaryExpr::Mul: Res = LHSVal * RHSVal; break;
202f4a2713aSLionel Sambuc     }
203f4a2713aSLionel Sambuc 
204f4a2713aSLionel Sambuc     return Res < 0 ? -1 : Res;
205f4a2713aSLionel Sambuc   }
206f4a2713aSLionel Sambuc   }
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc   llvm_unreachable("Invalid expression kind!");
209f4a2713aSLionel Sambuc }
210f4a2713aSLionel Sambuc 
211*0a6a1f1dSLionel Sambuc namespace {
212*0a6a1f1dSLionel Sambuc 
213f4a2713aSLionel Sambuc struct PPCOperand;
214f4a2713aSLionel Sambuc 
215f4a2713aSLionel Sambuc class PPCAsmParser : public MCTargetAsmParser {
216f4a2713aSLionel Sambuc   MCSubtargetInfo &STI;
217f4a2713aSLionel Sambuc   const MCInstrInfo &MII;
218f4a2713aSLionel Sambuc   bool IsPPC64;
219*0a6a1f1dSLionel Sambuc   bool IsDarwin;
220f4a2713aSLionel Sambuc 
Warning(SMLoc L,const Twine & Msg)221*0a6a1f1dSLionel Sambuc   void Warning(SMLoc L, const Twine &Msg) { getParser().Warning(L, Msg); }
Error(SMLoc L,const Twine & Msg)222*0a6a1f1dSLionel Sambuc   bool Error(SMLoc L, const Twine &Msg) { return getParser().Error(L, Msg); }
223f4a2713aSLionel Sambuc 
isPPC64() const224f4a2713aSLionel Sambuc   bool isPPC64() const { return IsPPC64; }
isDarwin() const225*0a6a1f1dSLionel Sambuc   bool isDarwin() const { return IsDarwin; }
226f4a2713aSLionel Sambuc 
227f4a2713aSLionel Sambuc   bool MatchRegisterName(const AsmToken &Tok,
228f4a2713aSLionel Sambuc                          unsigned &RegNo, int64_t &IntVal);
229f4a2713aSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc   const MCExpr *ExtractModifierFromExpr(const MCExpr *E,
233f4a2713aSLionel Sambuc                                         PPCMCExpr::VariantKind &Variant);
234f4a2713aSLionel Sambuc   const MCExpr *FixupVariantKind(const MCExpr *E);
235f4a2713aSLionel Sambuc   bool ParseExpression(const MCExpr *&EVal);
236*0a6a1f1dSLionel Sambuc   bool ParseDarwinExpression(const MCExpr *&EVal);
237f4a2713aSLionel Sambuc 
238*0a6a1f1dSLionel Sambuc   bool ParseOperand(OperandVector &Operands);
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc   bool ParseDirectiveWord(unsigned Size, SMLoc L);
241f4a2713aSLionel Sambuc   bool ParseDirectiveTC(unsigned Size, SMLoc L);
242f4a2713aSLionel Sambuc   bool ParseDirectiveMachine(SMLoc L);
243*0a6a1f1dSLionel Sambuc   bool ParseDarwinDirectiveMachine(SMLoc L);
244*0a6a1f1dSLionel Sambuc   bool ParseDirectiveAbiVersion(SMLoc L);
245*0a6a1f1dSLionel Sambuc   bool ParseDirectiveLocalEntry(SMLoc L);
246f4a2713aSLionel Sambuc 
247f4a2713aSLionel Sambuc   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
248*0a6a1f1dSLionel Sambuc                                OperandVector &Operands, MCStreamer &Out,
249*0a6a1f1dSLionel Sambuc                                uint64_t &ErrorInfo,
250*0a6a1f1dSLionel Sambuc                                bool MatchingInlineAsm) override;
251f4a2713aSLionel Sambuc 
252*0a6a1f1dSLionel Sambuc   void ProcessInstruction(MCInst &Inst, const OperandVector &Ops);
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc   /// @name Auto-generated Match Functions
255f4a2713aSLionel Sambuc   /// {
256f4a2713aSLionel Sambuc 
257f4a2713aSLionel Sambuc #define GET_ASSEMBLER_HEADER
258f4a2713aSLionel Sambuc #include "PPCGenAsmMatcher.inc"
259f4a2713aSLionel Sambuc 
260f4a2713aSLionel Sambuc   /// }
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc 
263f4a2713aSLionel Sambuc public:
PPCAsmParser(MCSubtargetInfo & _STI,MCAsmParser & _Parser,const MCInstrInfo & _MII,const MCTargetOptions & Options)264f4a2713aSLionel Sambuc   PPCAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser,
265*0a6a1f1dSLionel Sambuc                const MCInstrInfo &_MII, const MCTargetOptions &Options)
266*0a6a1f1dSLionel Sambuc       : MCTargetAsmParser(), STI(_STI), MII(_MII) {
267f4a2713aSLionel Sambuc     // Check for 64-bit vs. 32-bit pointer mode.
268f4a2713aSLionel Sambuc     Triple TheTriple(STI.getTargetTriple());
269f4a2713aSLionel Sambuc     IsPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
270f4a2713aSLionel Sambuc                TheTriple.getArch() == Triple::ppc64le);
271*0a6a1f1dSLionel Sambuc     IsDarwin = TheTriple.isMacOSX();
272f4a2713aSLionel Sambuc     // Initialize the set of available features.
273f4a2713aSLionel Sambuc     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
274f4a2713aSLionel Sambuc   }
275f4a2713aSLionel Sambuc 
276*0a6a1f1dSLionel Sambuc   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
277*0a6a1f1dSLionel Sambuc                         SMLoc NameLoc, OperandVector &Operands) override;
278f4a2713aSLionel Sambuc 
279*0a6a1f1dSLionel Sambuc   bool ParseDirective(AsmToken DirectiveID) override;
280f4a2713aSLionel Sambuc 
281*0a6a1f1dSLionel Sambuc   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
282*0a6a1f1dSLionel Sambuc                                       unsigned Kind) override;
283f4a2713aSLionel Sambuc 
284*0a6a1f1dSLionel Sambuc   const MCExpr *applyModifierToExpr(const MCExpr *E,
285f4a2713aSLionel Sambuc                                     MCSymbolRefExpr::VariantKind,
286*0a6a1f1dSLionel Sambuc                                     MCContext &Ctx) override;
287f4a2713aSLionel Sambuc };
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc /// PPCOperand - Instances of this class represent a parsed PowerPC machine
290f4a2713aSLionel Sambuc /// instruction.
291f4a2713aSLionel Sambuc struct PPCOperand : public MCParsedAsmOperand {
292f4a2713aSLionel Sambuc   enum KindTy {
293f4a2713aSLionel Sambuc     Token,
294f4a2713aSLionel Sambuc     Immediate,
295*0a6a1f1dSLionel Sambuc     ContextImmediate,
296f4a2713aSLionel Sambuc     Expression,
297f4a2713aSLionel Sambuc     TLSRegister
298f4a2713aSLionel Sambuc   } Kind;
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc   SMLoc StartLoc, EndLoc;
301f4a2713aSLionel Sambuc   bool IsPPC64;
302f4a2713aSLionel Sambuc 
303f4a2713aSLionel Sambuc   struct TokOp {
304f4a2713aSLionel Sambuc     const char *Data;
305f4a2713aSLionel Sambuc     unsigned Length;
306f4a2713aSLionel Sambuc   };
307f4a2713aSLionel Sambuc 
308f4a2713aSLionel Sambuc   struct ImmOp {
309f4a2713aSLionel Sambuc     int64_t Val;
310f4a2713aSLionel Sambuc   };
311f4a2713aSLionel Sambuc 
312f4a2713aSLionel Sambuc   struct ExprOp {
313f4a2713aSLionel Sambuc     const MCExpr *Val;
314f4a2713aSLionel Sambuc     int64_t CRVal;     // Cached result of EvaluateCRExpr(Val)
315f4a2713aSLionel Sambuc   };
316f4a2713aSLionel Sambuc 
317f4a2713aSLionel Sambuc   struct TLSRegOp {
318f4a2713aSLionel Sambuc     const MCSymbolRefExpr *Sym;
319f4a2713aSLionel Sambuc   };
320f4a2713aSLionel Sambuc 
321f4a2713aSLionel Sambuc   union {
322f4a2713aSLionel Sambuc     struct TokOp Tok;
323f4a2713aSLionel Sambuc     struct ImmOp Imm;
324f4a2713aSLionel Sambuc     struct ExprOp Expr;
325f4a2713aSLionel Sambuc     struct TLSRegOp TLSReg;
326f4a2713aSLionel Sambuc   };
327f4a2713aSLionel Sambuc 
PPCOperand__anon5c4977960111::PPCOperand328f4a2713aSLionel Sambuc   PPCOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
329f4a2713aSLionel Sambuc public:
PPCOperand__anon5c4977960111::PPCOperand330f4a2713aSLionel Sambuc   PPCOperand(const PPCOperand &o) : MCParsedAsmOperand() {
331f4a2713aSLionel Sambuc     Kind = o.Kind;
332f4a2713aSLionel Sambuc     StartLoc = o.StartLoc;
333f4a2713aSLionel Sambuc     EndLoc = o.EndLoc;
334f4a2713aSLionel Sambuc     IsPPC64 = o.IsPPC64;
335f4a2713aSLionel Sambuc     switch (Kind) {
336f4a2713aSLionel Sambuc     case Token:
337f4a2713aSLionel Sambuc       Tok = o.Tok;
338f4a2713aSLionel Sambuc       break;
339f4a2713aSLionel Sambuc     case Immediate:
340*0a6a1f1dSLionel Sambuc     case ContextImmediate:
341f4a2713aSLionel Sambuc       Imm = o.Imm;
342f4a2713aSLionel Sambuc       break;
343f4a2713aSLionel Sambuc     case Expression:
344f4a2713aSLionel Sambuc       Expr = o.Expr;
345f4a2713aSLionel Sambuc       break;
346f4a2713aSLionel Sambuc     case TLSRegister:
347f4a2713aSLionel Sambuc       TLSReg = o.TLSReg;
348f4a2713aSLionel Sambuc       break;
349f4a2713aSLionel Sambuc     }
350f4a2713aSLionel Sambuc   }
351f4a2713aSLionel Sambuc 
352f4a2713aSLionel Sambuc   /// getStartLoc - Get the location of the first token of this operand.
getStartLoc__anon5c4977960111::PPCOperand353*0a6a1f1dSLionel Sambuc   SMLoc getStartLoc() const override { return StartLoc; }
354f4a2713aSLionel Sambuc 
355f4a2713aSLionel Sambuc   /// getEndLoc - Get the location of the last token of this operand.
getEndLoc__anon5c4977960111::PPCOperand356*0a6a1f1dSLionel Sambuc   SMLoc getEndLoc() const override { return EndLoc; }
357f4a2713aSLionel Sambuc 
358f4a2713aSLionel Sambuc   /// isPPC64 - True if this operand is for an instruction in 64-bit mode.
isPPC64__anon5c4977960111::PPCOperand359f4a2713aSLionel Sambuc   bool isPPC64() const { return IsPPC64; }
360f4a2713aSLionel Sambuc 
getImm__anon5c4977960111::PPCOperand361f4a2713aSLionel Sambuc   int64_t getImm() const {
362f4a2713aSLionel Sambuc     assert(Kind == Immediate && "Invalid access!");
363f4a2713aSLionel Sambuc     return Imm.Val;
364f4a2713aSLionel Sambuc   }
getImmS16Context__anon5c4977960111::PPCOperand365*0a6a1f1dSLionel Sambuc   int64_t getImmS16Context() const {
366*0a6a1f1dSLionel Sambuc     assert((Kind == Immediate || Kind == ContextImmediate) && "Invalid access!");
367*0a6a1f1dSLionel Sambuc     if (Kind == Immediate)
368*0a6a1f1dSLionel Sambuc       return Imm.Val;
369*0a6a1f1dSLionel Sambuc     return static_cast<int16_t>(Imm.Val);
370*0a6a1f1dSLionel Sambuc   }
getImmU16Context__anon5c4977960111::PPCOperand371*0a6a1f1dSLionel Sambuc   int64_t getImmU16Context() const {
372*0a6a1f1dSLionel Sambuc     assert((Kind == Immediate || Kind == ContextImmediate) && "Invalid access!");
373*0a6a1f1dSLionel Sambuc     return Imm.Val;
374*0a6a1f1dSLionel Sambuc   }
375f4a2713aSLionel Sambuc 
getExpr__anon5c4977960111::PPCOperand376f4a2713aSLionel Sambuc   const MCExpr *getExpr() const {
377f4a2713aSLionel Sambuc     assert(Kind == Expression && "Invalid access!");
378f4a2713aSLionel Sambuc     return Expr.Val;
379f4a2713aSLionel Sambuc   }
380f4a2713aSLionel Sambuc 
getExprCRVal__anon5c4977960111::PPCOperand381f4a2713aSLionel Sambuc   int64_t getExprCRVal() const {
382f4a2713aSLionel Sambuc     assert(Kind == Expression && "Invalid access!");
383f4a2713aSLionel Sambuc     return Expr.CRVal;
384f4a2713aSLionel Sambuc   }
385f4a2713aSLionel Sambuc 
getTLSReg__anon5c4977960111::PPCOperand386f4a2713aSLionel Sambuc   const MCExpr *getTLSReg() const {
387f4a2713aSLionel Sambuc     assert(Kind == TLSRegister && "Invalid access!");
388f4a2713aSLionel Sambuc     return TLSReg.Sym;
389f4a2713aSLionel Sambuc   }
390f4a2713aSLionel Sambuc 
getReg__anon5c4977960111::PPCOperand391*0a6a1f1dSLionel Sambuc   unsigned getReg() const override {
392f4a2713aSLionel Sambuc     assert(isRegNumber() && "Invalid access!");
393f4a2713aSLionel Sambuc     return (unsigned) Imm.Val;
394f4a2713aSLionel Sambuc   }
395f4a2713aSLionel Sambuc 
getVSReg__anon5c4977960111::PPCOperand396*0a6a1f1dSLionel Sambuc   unsigned getVSReg() const {
397*0a6a1f1dSLionel Sambuc     assert(isVSRegNumber() && "Invalid access!");
398*0a6a1f1dSLionel Sambuc     return (unsigned) Imm.Val;
399*0a6a1f1dSLionel Sambuc   }
400*0a6a1f1dSLionel Sambuc 
getCCReg__anon5c4977960111::PPCOperand401f4a2713aSLionel Sambuc   unsigned getCCReg() const {
402f4a2713aSLionel Sambuc     assert(isCCRegNumber() && "Invalid access!");
403f4a2713aSLionel Sambuc     return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal);
404f4a2713aSLionel Sambuc   }
405f4a2713aSLionel Sambuc 
getCRBit__anon5c4977960111::PPCOperand406f4a2713aSLionel Sambuc   unsigned getCRBit() const {
407f4a2713aSLionel Sambuc     assert(isCRBitNumber() && "Invalid access!");
408f4a2713aSLionel Sambuc     return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal);
409f4a2713aSLionel Sambuc   }
410f4a2713aSLionel Sambuc 
getCRBitMask__anon5c4977960111::PPCOperand411f4a2713aSLionel Sambuc   unsigned getCRBitMask() const {
412f4a2713aSLionel Sambuc     assert(isCRBitMask() && "Invalid access!");
413f4a2713aSLionel Sambuc     return 7 - countTrailingZeros<uint64_t>(Imm.Val);
414f4a2713aSLionel Sambuc   }
415f4a2713aSLionel Sambuc 
isToken__anon5c4977960111::PPCOperand416*0a6a1f1dSLionel Sambuc   bool isToken() const override { return Kind == Token; }
isImm__anon5c4977960111::PPCOperand417*0a6a1f1dSLionel Sambuc   bool isImm() const override { return Kind == Immediate || Kind == Expression; }
isU2Imm__anon5c4977960111::PPCOperand418*0a6a1f1dSLionel Sambuc   bool isU2Imm() const { return Kind == Immediate && isUInt<2>(getImm()); }
isU4Imm__anon5c4977960111::PPCOperand419*0a6a1f1dSLionel Sambuc   bool isU4Imm() const { return Kind == Immediate && isUInt<4>(getImm()); }
isU5Imm__anon5c4977960111::PPCOperand420f4a2713aSLionel Sambuc   bool isU5Imm() const { return Kind == Immediate && isUInt<5>(getImm()); }
isS5Imm__anon5c4977960111::PPCOperand421f4a2713aSLionel Sambuc   bool isS5Imm() const { return Kind == Immediate && isInt<5>(getImm()); }
isU6Imm__anon5c4977960111::PPCOperand422f4a2713aSLionel Sambuc   bool isU6Imm() const { return Kind == Immediate && isUInt<6>(getImm()); }
isU6ImmX2__anon5c4977960111::PPCOperand423*0a6a1f1dSLionel Sambuc   bool isU6ImmX2() const { return Kind == Immediate &&
424*0a6a1f1dSLionel Sambuc                                   isUInt<6>(getImm()) &&
425*0a6a1f1dSLionel Sambuc                                   (getImm() & 1) == 0; }
isU7ImmX4__anon5c4977960111::PPCOperand426*0a6a1f1dSLionel Sambuc   bool isU7ImmX4() const { return Kind == Immediate &&
427*0a6a1f1dSLionel Sambuc                                   isUInt<7>(getImm()) &&
428*0a6a1f1dSLionel Sambuc                                   (getImm() & 3) == 0; }
isU8ImmX8__anon5c4977960111::PPCOperand429*0a6a1f1dSLionel Sambuc   bool isU8ImmX8() const { return Kind == Immediate &&
430*0a6a1f1dSLionel Sambuc                                   isUInt<8>(getImm()) &&
431*0a6a1f1dSLionel Sambuc                                   (getImm() & 7) == 0; }
isU16Imm__anon5c4977960111::PPCOperand432*0a6a1f1dSLionel Sambuc   bool isU16Imm() const {
433*0a6a1f1dSLionel Sambuc     switch (Kind) {
434*0a6a1f1dSLionel Sambuc       case Expression:
435*0a6a1f1dSLionel Sambuc         return true;
436*0a6a1f1dSLionel Sambuc       case Immediate:
437*0a6a1f1dSLionel Sambuc       case ContextImmediate:
438*0a6a1f1dSLionel Sambuc         return isUInt<16>(getImmU16Context());
439*0a6a1f1dSLionel Sambuc       default:
440*0a6a1f1dSLionel Sambuc         return false;
441*0a6a1f1dSLionel Sambuc     }
442*0a6a1f1dSLionel Sambuc   }
isS16Imm__anon5c4977960111::PPCOperand443*0a6a1f1dSLionel Sambuc   bool isS16Imm() const {
444*0a6a1f1dSLionel Sambuc     switch (Kind) {
445*0a6a1f1dSLionel Sambuc       case Expression:
446*0a6a1f1dSLionel Sambuc         return true;
447*0a6a1f1dSLionel Sambuc       case Immediate:
448*0a6a1f1dSLionel Sambuc       case ContextImmediate:
449*0a6a1f1dSLionel Sambuc         return isInt<16>(getImmS16Context());
450*0a6a1f1dSLionel Sambuc       default:
451*0a6a1f1dSLionel Sambuc         return false;
452*0a6a1f1dSLionel Sambuc     }
453*0a6a1f1dSLionel Sambuc   }
isS16ImmX4__anon5c4977960111::PPCOperand454f4a2713aSLionel Sambuc   bool isS16ImmX4() const { return Kind == Expression ||
455f4a2713aSLionel Sambuc                                    (Kind == Immediate && isInt<16>(getImm()) &&
456f4a2713aSLionel Sambuc                                     (getImm() & 3) == 0); }
isS17Imm__anon5c4977960111::PPCOperand457*0a6a1f1dSLionel Sambuc   bool isS17Imm() const {
458*0a6a1f1dSLionel Sambuc     switch (Kind) {
459*0a6a1f1dSLionel Sambuc       case Expression:
460*0a6a1f1dSLionel Sambuc         return true;
461*0a6a1f1dSLionel Sambuc       case Immediate:
462*0a6a1f1dSLionel Sambuc       case ContextImmediate:
463*0a6a1f1dSLionel Sambuc         return isInt<17>(getImmS16Context());
464*0a6a1f1dSLionel Sambuc       default:
465*0a6a1f1dSLionel Sambuc         return false;
466*0a6a1f1dSLionel Sambuc     }
467*0a6a1f1dSLionel Sambuc   }
isTLSReg__anon5c4977960111::PPCOperand468f4a2713aSLionel Sambuc   bool isTLSReg() const { return Kind == TLSRegister; }
isDirectBr__anon5c4977960111::PPCOperand469*0a6a1f1dSLionel Sambuc   bool isDirectBr() const {
470*0a6a1f1dSLionel Sambuc     if (Kind == Expression)
471*0a6a1f1dSLionel Sambuc       return true;
472*0a6a1f1dSLionel Sambuc     if (Kind != Immediate)
473*0a6a1f1dSLionel Sambuc       return false;
474*0a6a1f1dSLionel Sambuc     // Operand must be 64-bit aligned, signed 27-bit immediate.
475*0a6a1f1dSLionel Sambuc     if ((getImm() & 3) != 0)
476*0a6a1f1dSLionel Sambuc       return false;
477*0a6a1f1dSLionel Sambuc     if (isInt<26>(getImm()))
478*0a6a1f1dSLionel Sambuc       return true;
479*0a6a1f1dSLionel Sambuc     if (!IsPPC64) {
480*0a6a1f1dSLionel Sambuc       // In 32-bit mode, large 32-bit quantities wrap around.
481*0a6a1f1dSLionel Sambuc       if (isUInt<32>(getImm()) && isInt<26>(static_cast<int32_t>(getImm())))
482*0a6a1f1dSLionel Sambuc         return true;
483*0a6a1f1dSLionel Sambuc     }
484*0a6a1f1dSLionel Sambuc     return false;
485*0a6a1f1dSLionel Sambuc   }
isCondBr__anon5c4977960111::PPCOperand486f4a2713aSLionel Sambuc   bool isCondBr() const { return Kind == Expression ||
487f4a2713aSLionel Sambuc                                  (Kind == Immediate && isInt<16>(getImm()) &&
488f4a2713aSLionel Sambuc                                   (getImm() & 3) == 0); }
isRegNumber__anon5c4977960111::PPCOperand489f4a2713aSLionel Sambuc   bool isRegNumber() const { return Kind == Immediate && isUInt<5>(getImm()); }
isVSRegNumber__anon5c4977960111::PPCOperand490*0a6a1f1dSLionel Sambuc   bool isVSRegNumber() const { return Kind == Immediate && isUInt<6>(getImm()); }
isCCRegNumber__anon5c4977960111::PPCOperand491f4a2713aSLionel Sambuc   bool isCCRegNumber() const { return (Kind == Expression
492f4a2713aSLionel Sambuc                                        && isUInt<3>(getExprCRVal())) ||
493f4a2713aSLionel Sambuc                                       (Kind == Immediate
494f4a2713aSLionel Sambuc                                        && isUInt<3>(getImm())); }
isCRBitNumber__anon5c4977960111::PPCOperand495f4a2713aSLionel Sambuc   bool isCRBitNumber() const { return (Kind == Expression
496f4a2713aSLionel Sambuc                                        && isUInt<5>(getExprCRVal())) ||
497f4a2713aSLionel Sambuc                                       (Kind == Immediate
498f4a2713aSLionel Sambuc                                        && isUInt<5>(getImm())); }
isCRBitMask__anon5c4977960111::PPCOperand499f4a2713aSLionel Sambuc   bool isCRBitMask() const { return Kind == Immediate && isUInt<8>(getImm()) &&
500f4a2713aSLionel Sambuc                                     isPowerOf2_32(getImm()); }
isMem__anon5c4977960111::PPCOperand501*0a6a1f1dSLionel Sambuc   bool isMem() const override { return false; }
isReg__anon5c4977960111::PPCOperand502*0a6a1f1dSLionel Sambuc   bool isReg() const override { return false; }
503f4a2713aSLionel Sambuc 
addRegOperands__anon5c4977960111::PPCOperand504f4a2713aSLionel Sambuc   void addRegOperands(MCInst &Inst, unsigned N) const {
505f4a2713aSLionel Sambuc     llvm_unreachable("addRegOperands");
506f4a2713aSLionel Sambuc   }
507f4a2713aSLionel Sambuc 
addRegGPRCOperands__anon5c4977960111::PPCOperand508f4a2713aSLionel Sambuc   void addRegGPRCOperands(MCInst &Inst, unsigned N) const {
509f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
510f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(RRegs[getReg()]));
511f4a2713aSLionel Sambuc   }
512f4a2713aSLionel Sambuc 
addRegGPRCNoR0Operands__anon5c4977960111::PPCOperand513f4a2713aSLionel Sambuc   void addRegGPRCNoR0Operands(MCInst &Inst, unsigned N) const {
514f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
515f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(RRegsNoR0[getReg()]));
516f4a2713aSLionel Sambuc   }
517f4a2713aSLionel Sambuc 
addRegG8RCOperands__anon5c4977960111::PPCOperand518f4a2713aSLionel Sambuc   void addRegG8RCOperands(MCInst &Inst, unsigned N) const {
519f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
520f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(XRegs[getReg()]));
521f4a2713aSLionel Sambuc   }
522f4a2713aSLionel Sambuc 
addRegG8RCNoX0Operands__anon5c4977960111::PPCOperand523f4a2713aSLionel Sambuc   void addRegG8RCNoX0Operands(MCInst &Inst, unsigned N) const {
524f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
525f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(XRegsNoX0[getReg()]));
526f4a2713aSLionel Sambuc   }
527f4a2713aSLionel Sambuc 
addRegGxRCOperands__anon5c4977960111::PPCOperand528f4a2713aSLionel Sambuc   void addRegGxRCOperands(MCInst &Inst, unsigned N) const {
529f4a2713aSLionel Sambuc     if (isPPC64())
530f4a2713aSLionel Sambuc       addRegG8RCOperands(Inst, N);
531f4a2713aSLionel Sambuc     else
532f4a2713aSLionel Sambuc       addRegGPRCOperands(Inst, N);
533f4a2713aSLionel Sambuc   }
534f4a2713aSLionel Sambuc 
addRegGxRCNoR0Operands__anon5c4977960111::PPCOperand535f4a2713aSLionel Sambuc   void addRegGxRCNoR0Operands(MCInst &Inst, unsigned N) const {
536f4a2713aSLionel Sambuc     if (isPPC64())
537f4a2713aSLionel Sambuc       addRegG8RCNoX0Operands(Inst, N);
538f4a2713aSLionel Sambuc     else
539f4a2713aSLionel Sambuc       addRegGPRCNoR0Operands(Inst, N);
540f4a2713aSLionel Sambuc   }
541f4a2713aSLionel Sambuc 
addRegF4RCOperands__anon5c4977960111::PPCOperand542f4a2713aSLionel Sambuc   void addRegF4RCOperands(MCInst &Inst, unsigned N) const {
543f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
544f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(FRegs[getReg()]));
545f4a2713aSLionel Sambuc   }
546f4a2713aSLionel Sambuc 
addRegF8RCOperands__anon5c4977960111::PPCOperand547f4a2713aSLionel Sambuc   void addRegF8RCOperands(MCInst &Inst, unsigned N) const {
548f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
549f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(FRegs[getReg()]));
550f4a2713aSLionel Sambuc   }
551f4a2713aSLionel Sambuc 
addRegVRRCOperands__anon5c4977960111::PPCOperand552f4a2713aSLionel Sambuc   void addRegVRRCOperands(MCInst &Inst, unsigned N) const {
553f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
554f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(VRegs[getReg()]));
555f4a2713aSLionel Sambuc   }
556f4a2713aSLionel Sambuc 
addRegVSRCOperands__anon5c4977960111::PPCOperand557*0a6a1f1dSLionel Sambuc   void addRegVSRCOperands(MCInst &Inst, unsigned N) const {
558*0a6a1f1dSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
559*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(VSRegs[getVSReg()]));
560*0a6a1f1dSLionel Sambuc   }
561*0a6a1f1dSLionel Sambuc 
addRegVSFRCOperands__anon5c4977960111::PPCOperand562*0a6a1f1dSLionel Sambuc   void addRegVSFRCOperands(MCInst &Inst, unsigned N) const {
563*0a6a1f1dSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
564*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(VSFRegs[getVSReg()]));
565*0a6a1f1dSLionel Sambuc   }
566*0a6a1f1dSLionel Sambuc 
addRegCRBITRCOperands__anon5c4977960111::PPCOperand567f4a2713aSLionel Sambuc   void addRegCRBITRCOperands(MCInst &Inst, unsigned N) const {
568f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
569f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(CRBITRegs[getCRBit()]));
570f4a2713aSLionel Sambuc   }
571f4a2713aSLionel Sambuc 
addRegCRRCOperands__anon5c4977960111::PPCOperand572f4a2713aSLionel Sambuc   void addRegCRRCOperands(MCInst &Inst, unsigned N) const {
573f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
574f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(CRRegs[getCCReg()]));
575f4a2713aSLionel Sambuc   }
576f4a2713aSLionel Sambuc 
addCRBitMaskOperands__anon5c4977960111::PPCOperand577f4a2713aSLionel Sambuc   void addCRBitMaskOperands(MCInst &Inst, unsigned N) const {
578f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
579f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(CRRegs[getCRBitMask()]));
580f4a2713aSLionel Sambuc   }
581f4a2713aSLionel Sambuc 
addImmOperands__anon5c4977960111::PPCOperand582f4a2713aSLionel Sambuc   void addImmOperands(MCInst &Inst, unsigned N) const {
583f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
584f4a2713aSLionel Sambuc     if (Kind == Immediate)
585f4a2713aSLionel Sambuc       Inst.addOperand(MCOperand::CreateImm(getImm()));
586f4a2713aSLionel Sambuc     else
587f4a2713aSLionel Sambuc       Inst.addOperand(MCOperand::CreateExpr(getExpr()));
588f4a2713aSLionel Sambuc   }
589f4a2713aSLionel Sambuc 
addS16ImmOperands__anon5c4977960111::PPCOperand590*0a6a1f1dSLionel Sambuc   void addS16ImmOperands(MCInst &Inst, unsigned N) const {
591*0a6a1f1dSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
592*0a6a1f1dSLionel Sambuc     switch (Kind) {
593*0a6a1f1dSLionel Sambuc       case Immediate:
594*0a6a1f1dSLionel Sambuc         Inst.addOperand(MCOperand::CreateImm(getImm()));
595*0a6a1f1dSLionel Sambuc         break;
596*0a6a1f1dSLionel Sambuc       case ContextImmediate:
597*0a6a1f1dSLionel Sambuc         Inst.addOperand(MCOperand::CreateImm(getImmS16Context()));
598*0a6a1f1dSLionel Sambuc         break;
599*0a6a1f1dSLionel Sambuc       default:
600*0a6a1f1dSLionel Sambuc         Inst.addOperand(MCOperand::CreateExpr(getExpr()));
601*0a6a1f1dSLionel Sambuc         break;
602*0a6a1f1dSLionel Sambuc     }
603*0a6a1f1dSLionel Sambuc   }
604*0a6a1f1dSLionel Sambuc 
addU16ImmOperands__anon5c4977960111::PPCOperand605*0a6a1f1dSLionel Sambuc   void addU16ImmOperands(MCInst &Inst, unsigned N) const {
606*0a6a1f1dSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
607*0a6a1f1dSLionel Sambuc     switch (Kind) {
608*0a6a1f1dSLionel Sambuc       case Immediate:
609*0a6a1f1dSLionel Sambuc         Inst.addOperand(MCOperand::CreateImm(getImm()));
610*0a6a1f1dSLionel Sambuc         break;
611*0a6a1f1dSLionel Sambuc       case ContextImmediate:
612*0a6a1f1dSLionel Sambuc         Inst.addOperand(MCOperand::CreateImm(getImmU16Context()));
613*0a6a1f1dSLionel Sambuc         break;
614*0a6a1f1dSLionel Sambuc       default:
615*0a6a1f1dSLionel Sambuc         Inst.addOperand(MCOperand::CreateExpr(getExpr()));
616*0a6a1f1dSLionel Sambuc         break;
617*0a6a1f1dSLionel Sambuc     }
618*0a6a1f1dSLionel Sambuc   }
619*0a6a1f1dSLionel Sambuc 
addBranchTargetOperands__anon5c4977960111::PPCOperand620f4a2713aSLionel Sambuc   void addBranchTargetOperands(MCInst &Inst, unsigned N) const {
621f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
622f4a2713aSLionel Sambuc     if (Kind == Immediate)
623f4a2713aSLionel Sambuc       Inst.addOperand(MCOperand::CreateImm(getImm() / 4));
624f4a2713aSLionel Sambuc     else
625f4a2713aSLionel Sambuc       Inst.addOperand(MCOperand::CreateExpr(getExpr()));
626f4a2713aSLionel Sambuc   }
627f4a2713aSLionel Sambuc 
addTLSRegOperands__anon5c4977960111::PPCOperand628f4a2713aSLionel Sambuc   void addTLSRegOperands(MCInst &Inst, unsigned N) const {
629f4a2713aSLionel Sambuc     assert(N == 1 && "Invalid number of operands!");
630f4a2713aSLionel Sambuc     Inst.addOperand(MCOperand::CreateExpr(getTLSReg()));
631f4a2713aSLionel Sambuc   }
632f4a2713aSLionel Sambuc 
getToken__anon5c4977960111::PPCOperand633f4a2713aSLionel Sambuc   StringRef getToken() const {
634f4a2713aSLionel Sambuc     assert(Kind == Token && "Invalid access!");
635f4a2713aSLionel Sambuc     return StringRef(Tok.Data, Tok.Length);
636f4a2713aSLionel Sambuc   }
637f4a2713aSLionel Sambuc 
638*0a6a1f1dSLionel Sambuc   void print(raw_ostream &OS) const override;
639f4a2713aSLionel Sambuc 
CreateToken__anon5c4977960111::PPCOperand640*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand> CreateToken(StringRef Str, SMLoc S,
641*0a6a1f1dSLionel Sambuc                                                  bool IsPPC64) {
642*0a6a1f1dSLionel Sambuc     auto Op = make_unique<PPCOperand>(Token);
643f4a2713aSLionel Sambuc     Op->Tok.Data = Str.data();
644f4a2713aSLionel Sambuc     Op->Tok.Length = Str.size();
645f4a2713aSLionel Sambuc     Op->StartLoc = S;
646f4a2713aSLionel Sambuc     Op->EndLoc = S;
647f4a2713aSLionel Sambuc     Op->IsPPC64 = IsPPC64;
648f4a2713aSLionel Sambuc     return Op;
649f4a2713aSLionel Sambuc   }
650f4a2713aSLionel Sambuc 
651*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand>
CreateTokenWithStringCopy__anon5c4977960111::PPCOperand652*0a6a1f1dSLionel Sambuc   CreateTokenWithStringCopy(StringRef Str, SMLoc S, bool IsPPC64) {
653f4a2713aSLionel Sambuc     // Allocate extra memory for the string and copy it.
654*0a6a1f1dSLionel Sambuc     // FIXME: This is incorrect, Operands are owned by unique_ptr with a default
655*0a6a1f1dSLionel Sambuc     // deleter which will destroy them by simply using "delete", not correctly
656*0a6a1f1dSLionel Sambuc     // calling operator delete on this extra memory after calling the dtor
657*0a6a1f1dSLionel Sambuc     // explicitly.
658f4a2713aSLionel Sambuc     void *Mem = ::operator new(sizeof(PPCOperand) + Str.size());
659*0a6a1f1dSLionel Sambuc     std::unique_ptr<PPCOperand> Op(new (Mem) PPCOperand(Token));
660*0a6a1f1dSLionel Sambuc     Op->Tok.Data = reinterpret_cast<const char *>(Op.get() + 1);
661f4a2713aSLionel Sambuc     Op->Tok.Length = Str.size();
662*0a6a1f1dSLionel Sambuc     std::memcpy(const_cast<char *>(Op->Tok.Data), Str.data(), Str.size());
663f4a2713aSLionel Sambuc     Op->StartLoc = S;
664f4a2713aSLionel Sambuc     Op->EndLoc = S;
665f4a2713aSLionel Sambuc     Op->IsPPC64 = IsPPC64;
666f4a2713aSLionel Sambuc     return Op;
667f4a2713aSLionel Sambuc   }
668f4a2713aSLionel Sambuc 
CreateImm__anon5c4977960111::PPCOperand669*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand> CreateImm(int64_t Val, SMLoc S, SMLoc E,
670*0a6a1f1dSLionel Sambuc                                                bool IsPPC64) {
671*0a6a1f1dSLionel Sambuc     auto Op = make_unique<PPCOperand>(Immediate);
672f4a2713aSLionel Sambuc     Op->Imm.Val = Val;
673f4a2713aSLionel Sambuc     Op->StartLoc = S;
674f4a2713aSLionel Sambuc     Op->EndLoc = E;
675f4a2713aSLionel Sambuc     Op->IsPPC64 = IsPPC64;
676f4a2713aSLionel Sambuc     return Op;
677f4a2713aSLionel Sambuc   }
678f4a2713aSLionel Sambuc 
CreateExpr__anon5c4977960111::PPCOperand679*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand> CreateExpr(const MCExpr *Val, SMLoc S,
680*0a6a1f1dSLionel Sambuc                                                 SMLoc E, bool IsPPC64) {
681*0a6a1f1dSLionel Sambuc     auto Op = make_unique<PPCOperand>(Expression);
682f4a2713aSLionel Sambuc     Op->Expr.Val = Val;
683f4a2713aSLionel Sambuc     Op->Expr.CRVal = EvaluateCRExpr(Val);
684f4a2713aSLionel Sambuc     Op->StartLoc = S;
685f4a2713aSLionel Sambuc     Op->EndLoc = E;
686f4a2713aSLionel Sambuc     Op->IsPPC64 = IsPPC64;
687f4a2713aSLionel Sambuc     return Op;
688f4a2713aSLionel Sambuc   }
689f4a2713aSLionel Sambuc 
690*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand>
CreateTLSReg__anon5c4977960111::PPCOperand691*0a6a1f1dSLionel Sambuc   CreateTLSReg(const MCSymbolRefExpr *Sym, SMLoc S, SMLoc E, bool IsPPC64) {
692*0a6a1f1dSLionel Sambuc     auto Op = make_unique<PPCOperand>(TLSRegister);
693f4a2713aSLionel Sambuc     Op->TLSReg.Sym = Sym;
694f4a2713aSLionel Sambuc     Op->StartLoc = S;
695f4a2713aSLionel Sambuc     Op->EndLoc = E;
696f4a2713aSLionel Sambuc     Op->IsPPC64 = IsPPC64;
697f4a2713aSLionel Sambuc     return Op;
698f4a2713aSLionel Sambuc   }
699f4a2713aSLionel Sambuc 
700*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand>
CreateContextImm__anon5c4977960111::PPCOperand701*0a6a1f1dSLionel Sambuc   CreateContextImm(int64_t Val, SMLoc S, SMLoc E, bool IsPPC64) {
702*0a6a1f1dSLionel Sambuc     auto Op = make_unique<PPCOperand>(ContextImmediate);
703*0a6a1f1dSLionel Sambuc     Op->Imm.Val = Val;
704*0a6a1f1dSLionel Sambuc     Op->StartLoc = S;
705*0a6a1f1dSLionel Sambuc     Op->EndLoc = E;
706*0a6a1f1dSLionel Sambuc     Op->IsPPC64 = IsPPC64;
707*0a6a1f1dSLionel Sambuc     return Op;
708*0a6a1f1dSLionel Sambuc   }
709*0a6a1f1dSLionel Sambuc 
710*0a6a1f1dSLionel Sambuc   static std::unique_ptr<PPCOperand>
CreateFromMCExpr__anon5c4977960111::PPCOperand711*0a6a1f1dSLionel Sambuc   CreateFromMCExpr(const MCExpr *Val, SMLoc S, SMLoc E, bool IsPPC64) {
712f4a2713aSLionel Sambuc     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Val))
713f4a2713aSLionel Sambuc       return CreateImm(CE->getValue(), S, E, IsPPC64);
714f4a2713aSLionel Sambuc 
715f4a2713aSLionel Sambuc     if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Val))
716f4a2713aSLionel Sambuc       if (SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS)
717f4a2713aSLionel Sambuc         return CreateTLSReg(SRE, S, E, IsPPC64);
718f4a2713aSLionel Sambuc 
719*0a6a1f1dSLionel Sambuc     if (const PPCMCExpr *TE = dyn_cast<PPCMCExpr>(Val)) {
720*0a6a1f1dSLionel Sambuc       int64_t Res;
721*0a6a1f1dSLionel Sambuc       if (TE->EvaluateAsConstant(Res))
722*0a6a1f1dSLionel Sambuc         return CreateContextImm(Res, S, E, IsPPC64);
723*0a6a1f1dSLionel Sambuc     }
724*0a6a1f1dSLionel Sambuc 
725f4a2713aSLionel Sambuc     return CreateExpr(Val, S, E, IsPPC64);
726f4a2713aSLionel Sambuc   }
727f4a2713aSLionel Sambuc };
728f4a2713aSLionel Sambuc 
729f4a2713aSLionel Sambuc } // end anonymous namespace.
730f4a2713aSLionel Sambuc 
print(raw_ostream & OS) const731f4a2713aSLionel Sambuc void PPCOperand::print(raw_ostream &OS) const {
732f4a2713aSLionel Sambuc   switch (Kind) {
733f4a2713aSLionel Sambuc   case Token:
734f4a2713aSLionel Sambuc     OS << "'" << getToken() << "'";
735f4a2713aSLionel Sambuc     break;
736f4a2713aSLionel Sambuc   case Immediate:
737*0a6a1f1dSLionel Sambuc   case ContextImmediate:
738f4a2713aSLionel Sambuc     OS << getImm();
739f4a2713aSLionel Sambuc     break;
740f4a2713aSLionel Sambuc   case Expression:
741f4a2713aSLionel Sambuc     getExpr()->print(OS);
742f4a2713aSLionel Sambuc     break;
743f4a2713aSLionel Sambuc   case TLSRegister:
744f4a2713aSLionel Sambuc     getTLSReg()->print(OS);
745f4a2713aSLionel Sambuc     break;
746f4a2713aSLionel Sambuc   }
747f4a2713aSLionel Sambuc }
748f4a2713aSLionel Sambuc 
749*0a6a1f1dSLionel Sambuc static void
addNegOperand(MCInst & Inst,MCOperand & Op,MCContext & Ctx)750*0a6a1f1dSLionel Sambuc addNegOperand(MCInst &Inst, MCOperand &Op, MCContext &Ctx) {
751*0a6a1f1dSLionel Sambuc   if (Op.isImm()) {
752*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateImm(-Op.getImm()));
753*0a6a1f1dSLionel Sambuc     return;
754*0a6a1f1dSLionel Sambuc   }
755*0a6a1f1dSLionel Sambuc   const MCExpr *Expr = Op.getExpr();
756*0a6a1f1dSLionel Sambuc   if (const MCUnaryExpr *UnExpr = dyn_cast<MCUnaryExpr>(Expr)) {
757*0a6a1f1dSLionel Sambuc     if (UnExpr->getOpcode() == MCUnaryExpr::Minus) {
758*0a6a1f1dSLionel Sambuc       Inst.addOperand(MCOperand::CreateExpr(UnExpr->getSubExpr()));
759*0a6a1f1dSLionel Sambuc       return;
760*0a6a1f1dSLionel Sambuc     }
761*0a6a1f1dSLionel Sambuc   } else if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Expr)) {
762*0a6a1f1dSLionel Sambuc     if (BinExpr->getOpcode() == MCBinaryExpr::Sub) {
763*0a6a1f1dSLionel Sambuc       const MCExpr *NE = MCBinaryExpr::CreateSub(BinExpr->getRHS(),
764*0a6a1f1dSLionel Sambuc                                                  BinExpr->getLHS(), Ctx);
765*0a6a1f1dSLionel Sambuc       Inst.addOperand(MCOperand::CreateExpr(NE));
766*0a6a1f1dSLionel Sambuc       return;
767*0a6a1f1dSLionel Sambuc     }
768*0a6a1f1dSLionel Sambuc   }
769*0a6a1f1dSLionel Sambuc   Inst.addOperand(MCOperand::CreateExpr(MCUnaryExpr::CreateMinus(Expr, Ctx)));
770*0a6a1f1dSLionel Sambuc }
771f4a2713aSLionel Sambuc 
ProcessInstruction(MCInst & Inst,const OperandVector & Operands)772*0a6a1f1dSLionel Sambuc void PPCAsmParser::ProcessInstruction(MCInst &Inst,
773*0a6a1f1dSLionel Sambuc                                       const OperandVector &Operands) {
774f4a2713aSLionel Sambuc   int Opcode = Inst.getOpcode();
775f4a2713aSLionel Sambuc   switch (Opcode) {
776f4a2713aSLionel Sambuc   case PPC::LAx: {
777f4a2713aSLionel Sambuc     MCInst TmpInst;
778f4a2713aSLionel Sambuc     TmpInst.setOpcode(PPC::LA);
779f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
780f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(2));
781f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
782f4a2713aSLionel Sambuc     Inst = TmpInst;
783f4a2713aSLionel Sambuc     break;
784f4a2713aSLionel Sambuc   }
785f4a2713aSLionel Sambuc   case PPC::SUBI: {
786f4a2713aSLionel Sambuc     MCInst TmpInst;
787f4a2713aSLionel Sambuc     TmpInst.setOpcode(PPC::ADDI);
788f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
789f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
790*0a6a1f1dSLionel Sambuc     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
791f4a2713aSLionel Sambuc     Inst = TmpInst;
792f4a2713aSLionel Sambuc     break;
793f4a2713aSLionel Sambuc   }
794f4a2713aSLionel Sambuc   case PPC::SUBIS: {
795f4a2713aSLionel Sambuc     MCInst TmpInst;
796f4a2713aSLionel Sambuc     TmpInst.setOpcode(PPC::ADDIS);
797f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
798f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
799*0a6a1f1dSLionel Sambuc     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
800f4a2713aSLionel Sambuc     Inst = TmpInst;
801f4a2713aSLionel Sambuc     break;
802f4a2713aSLionel Sambuc   }
803f4a2713aSLionel Sambuc   case PPC::SUBIC: {
804f4a2713aSLionel Sambuc     MCInst TmpInst;
805f4a2713aSLionel Sambuc     TmpInst.setOpcode(PPC::ADDIC);
806f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
807f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
808*0a6a1f1dSLionel Sambuc     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
809f4a2713aSLionel Sambuc     Inst = TmpInst;
810f4a2713aSLionel Sambuc     break;
811f4a2713aSLionel Sambuc   }
812f4a2713aSLionel Sambuc   case PPC::SUBICo: {
813f4a2713aSLionel Sambuc     MCInst TmpInst;
814f4a2713aSLionel Sambuc     TmpInst.setOpcode(PPC::ADDICo);
815f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
816f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
817*0a6a1f1dSLionel Sambuc     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
818f4a2713aSLionel Sambuc     Inst = TmpInst;
819f4a2713aSLionel Sambuc     break;
820f4a2713aSLionel Sambuc   }
821f4a2713aSLionel Sambuc   case PPC::EXTLWI:
822f4a2713aSLionel Sambuc   case PPC::EXTLWIo: {
823f4a2713aSLionel Sambuc     MCInst TmpInst;
824f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
825f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
826f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::EXTLWI? PPC::RLWINM : PPC::RLWINMo);
827f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
828f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
829f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B));
830f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
831f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N - 1));
832f4a2713aSLionel Sambuc     Inst = TmpInst;
833f4a2713aSLionel Sambuc     break;
834f4a2713aSLionel Sambuc   }
835f4a2713aSLionel Sambuc   case PPC::EXTRWI:
836f4a2713aSLionel Sambuc   case PPC::EXTRWIo: {
837f4a2713aSLionel Sambuc     MCInst TmpInst;
838f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
839f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
840f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::EXTRWI? PPC::RLWINM : PPC::RLWINMo);
841f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
842f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
843f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B + N));
844f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(32 - N));
845f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(31));
846f4a2713aSLionel Sambuc     Inst = TmpInst;
847f4a2713aSLionel Sambuc     break;
848f4a2713aSLionel Sambuc   }
849f4a2713aSLionel Sambuc   case PPC::INSLWI:
850f4a2713aSLionel Sambuc   case PPC::INSLWIo: {
851f4a2713aSLionel Sambuc     MCInst TmpInst;
852f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
853f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
854f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::INSLWI? PPC::RLWIMI : PPC::RLWIMIo);
855f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
856f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
857f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
858f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(32 - B));
859f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B));
860f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm((B + N) - 1));
861f4a2713aSLionel Sambuc     Inst = TmpInst;
862f4a2713aSLionel Sambuc     break;
863f4a2713aSLionel Sambuc   }
864f4a2713aSLionel Sambuc   case PPC::INSRWI:
865f4a2713aSLionel Sambuc   case PPC::INSRWIo: {
866f4a2713aSLionel Sambuc     MCInst TmpInst;
867f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
868f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
869f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::INSRWI? PPC::RLWIMI : PPC::RLWIMIo);
870f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
871f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
872f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
873f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(32 - (B + N)));
874f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B));
875f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm((B + N) - 1));
876f4a2713aSLionel Sambuc     Inst = TmpInst;
877f4a2713aSLionel Sambuc     break;
878f4a2713aSLionel Sambuc   }
879f4a2713aSLionel Sambuc   case PPC::ROTRWI:
880f4a2713aSLionel Sambuc   case PPC::ROTRWIo: {
881f4a2713aSLionel Sambuc     MCInst TmpInst;
882f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
883f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::ROTRWI? PPC::RLWINM : PPC::RLWINMo);
884f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
885f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
886f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(32 - N));
887f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
888f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(31));
889f4a2713aSLionel Sambuc     Inst = TmpInst;
890f4a2713aSLionel Sambuc     break;
891f4a2713aSLionel Sambuc   }
892f4a2713aSLionel Sambuc   case PPC::SLWI:
893f4a2713aSLionel Sambuc   case PPC::SLWIo: {
894f4a2713aSLionel Sambuc     MCInst TmpInst;
895f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
896f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::SLWI? PPC::RLWINM : PPC::RLWINMo);
897f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
898f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
899f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N));
900f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
901f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(31 - N));
902f4a2713aSLionel Sambuc     Inst = TmpInst;
903f4a2713aSLionel Sambuc     break;
904f4a2713aSLionel Sambuc   }
905f4a2713aSLionel Sambuc   case PPC::SRWI:
906f4a2713aSLionel Sambuc   case PPC::SRWIo: {
907f4a2713aSLionel Sambuc     MCInst TmpInst;
908f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
909f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::SRWI? PPC::RLWINM : PPC::RLWINMo);
910f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
911f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
912f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(32 - N));
913f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N));
914f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(31));
915f4a2713aSLionel Sambuc     Inst = TmpInst;
916f4a2713aSLionel Sambuc     break;
917f4a2713aSLionel Sambuc   }
918f4a2713aSLionel Sambuc   case PPC::CLRRWI:
919f4a2713aSLionel Sambuc   case PPC::CLRRWIo: {
920f4a2713aSLionel Sambuc     MCInst TmpInst;
921f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
922f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::CLRRWI? PPC::RLWINM : PPC::RLWINMo);
923f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
924f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
925f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
926f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
927f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(31 - N));
928f4a2713aSLionel Sambuc     Inst = TmpInst;
929f4a2713aSLionel Sambuc     break;
930f4a2713aSLionel Sambuc   }
931f4a2713aSLionel Sambuc   case PPC::CLRLSLWI:
932f4a2713aSLionel Sambuc   case PPC::CLRLSLWIo: {
933f4a2713aSLionel Sambuc     MCInst TmpInst;
934f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(2).getImm();
935f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(3).getImm();
936f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::CLRLSLWI? PPC::RLWINM : PPC::RLWINMo);
937f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
938f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
939f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N));
940f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B - N));
941f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(31 - N));
942f4a2713aSLionel Sambuc     Inst = TmpInst;
943f4a2713aSLionel Sambuc     break;
944f4a2713aSLionel Sambuc   }
945f4a2713aSLionel Sambuc   case PPC::EXTLDI:
946f4a2713aSLionel Sambuc   case PPC::EXTLDIo: {
947f4a2713aSLionel Sambuc     MCInst TmpInst;
948f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
949f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
950f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::EXTLDI? PPC::RLDICR : PPC::RLDICRo);
951f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
952f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
953f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B));
954f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N - 1));
955f4a2713aSLionel Sambuc     Inst = TmpInst;
956f4a2713aSLionel Sambuc     break;
957f4a2713aSLionel Sambuc   }
958f4a2713aSLionel Sambuc   case PPC::EXTRDI:
959f4a2713aSLionel Sambuc   case PPC::EXTRDIo: {
960f4a2713aSLionel Sambuc     MCInst TmpInst;
961f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
962f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
963f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::EXTRDI? PPC::RLDICL : PPC::RLDICLo);
964f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
965f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
966f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B + N));
967f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(64 - N));
968f4a2713aSLionel Sambuc     Inst = TmpInst;
969f4a2713aSLionel Sambuc     break;
970f4a2713aSLionel Sambuc   }
971f4a2713aSLionel Sambuc   case PPC::INSRDI:
972f4a2713aSLionel Sambuc   case PPC::INSRDIo: {
973f4a2713aSLionel Sambuc     MCInst TmpInst;
974f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
975f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(3).getImm();
976f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::INSRDI? PPC::RLDIMI : PPC::RLDIMIo);
977f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
978f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
979f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
980f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(64 - (B + N)));
981f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B));
982f4a2713aSLionel Sambuc     Inst = TmpInst;
983f4a2713aSLionel Sambuc     break;
984f4a2713aSLionel Sambuc   }
985f4a2713aSLionel Sambuc   case PPC::ROTRDI:
986f4a2713aSLionel Sambuc   case PPC::ROTRDIo: {
987f4a2713aSLionel Sambuc     MCInst TmpInst;
988f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
989f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::ROTRDI? PPC::RLDICL : PPC::RLDICLo);
990f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
991f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
992f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(64 - N));
993f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
994f4a2713aSLionel Sambuc     Inst = TmpInst;
995f4a2713aSLionel Sambuc     break;
996f4a2713aSLionel Sambuc   }
997f4a2713aSLionel Sambuc   case PPC::SLDI:
998f4a2713aSLionel Sambuc   case PPC::SLDIo: {
999f4a2713aSLionel Sambuc     MCInst TmpInst;
1000f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
1001f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::SLDI? PPC::RLDICR : PPC::RLDICRo);
1002f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
1003f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
1004f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N));
1005f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(63 - N));
1006f4a2713aSLionel Sambuc     Inst = TmpInst;
1007f4a2713aSLionel Sambuc     break;
1008f4a2713aSLionel Sambuc   }
1009f4a2713aSLionel Sambuc   case PPC::SRDI:
1010f4a2713aSLionel Sambuc   case PPC::SRDIo: {
1011f4a2713aSLionel Sambuc     MCInst TmpInst;
1012f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
1013f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::SRDI? PPC::RLDICL : PPC::RLDICLo);
1014f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
1015f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
1016f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(64 - N));
1017f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N));
1018f4a2713aSLionel Sambuc     Inst = TmpInst;
1019f4a2713aSLionel Sambuc     break;
1020f4a2713aSLionel Sambuc   }
1021f4a2713aSLionel Sambuc   case PPC::CLRRDI:
1022f4a2713aSLionel Sambuc   case PPC::CLRRDIo: {
1023f4a2713aSLionel Sambuc     MCInst TmpInst;
1024f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(2).getImm();
1025f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::CLRRDI? PPC::RLDICR : PPC::RLDICRo);
1026f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
1027f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
1028f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(0));
1029f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(63 - N));
1030f4a2713aSLionel Sambuc     Inst = TmpInst;
1031f4a2713aSLionel Sambuc     break;
1032f4a2713aSLionel Sambuc   }
1033f4a2713aSLionel Sambuc   case PPC::CLRLSLDI:
1034f4a2713aSLionel Sambuc   case PPC::CLRLSLDIo: {
1035f4a2713aSLionel Sambuc     MCInst TmpInst;
1036f4a2713aSLionel Sambuc     int64_t B = Inst.getOperand(2).getImm();
1037f4a2713aSLionel Sambuc     int64_t N = Inst.getOperand(3).getImm();
1038f4a2713aSLionel Sambuc     TmpInst.setOpcode(Opcode == PPC::CLRLSLDI? PPC::RLDIC : PPC::RLDICo);
1039f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(0));
1040f4a2713aSLionel Sambuc     TmpInst.addOperand(Inst.getOperand(1));
1041f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(N));
1042f4a2713aSLionel Sambuc     TmpInst.addOperand(MCOperand::CreateImm(B - N));
1043f4a2713aSLionel Sambuc     Inst = TmpInst;
1044f4a2713aSLionel Sambuc     break;
1045f4a2713aSLionel Sambuc   }
1046f4a2713aSLionel Sambuc   }
1047f4a2713aSLionel Sambuc }
1048f4a2713aSLionel Sambuc 
MatchAndEmitInstruction(SMLoc IDLoc,unsigned & Opcode,OperandVector & Operands,MCStreamer & Out,uint64_t & ErrorInfo,bool MatchingInlineAsm)1049*0a6a1f1dSLionel Sambuc bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1050*0a6a1f1dSLionel Sambuc                                            OperandVector &Operands,
1051*0a6a1f1dSLionel Sambuc                                            MCStreamer &Out, uint64_t &ErrorInfo,
1052f4a2713aSLionel Sambuc                                            bool MatchingInlineAsm) {
1053f4a2713aSLionel Sambuc   MCInst Inst;
1054f4a2713aSLionel Sambuc 
1055f4a2713aSLionel Sambuc   switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
1056f4a2713aSLionel Sambuc   case Match_Success:
1057f4a2713aSLionel Sambuc     // Post-process instructions (typically extended mnemonics)
1058f4a2713aSLionel Sambuc     ProcessInstruction(Inst, Operands);
1059f4a2713aSLionel Sambuc     Inst.setLoc(IDLoc);
1060*0a6a1f1dSLionel Sambuc     Out.EmitInstruction(Inst, STI);
1061f4a2713aSLionel Sambuc     return false;
1062f4a2713aSLionel Sambuc   case Match_MissingFeature:
1063f4a2713aSLionel Sambuc     return Error(IDLoc, "instruction use requires an option to be enabled");
1064f4a2713aSLionel Sambuc   case Match_MnemonicFail:
1065f4a2713aSLionel Sambuc     return Error(IDLoc, "unrecognized instruction mnemonic");
1066f4a2713aSLionel Sambuc   case Match_InvalidOperand: {
1067f4a2713aSLionel Sambuc     SMLoc ErrorLoc = IDLoc;
1068*0a6a1f1dSLionel Sambuc     if (ErrorInfo != ~0ULL) {
1069f4a2713aSLionel Sambuc       if (ErrorInfo >= Operands.size())
1070f4a2713aSLionel Sambuc         return Error(IDLoc, "too few operands for instruction");
1071f4a2713aSLionel Sambuc 
1072*0a6a1f1dSLionel Sambuc       ErrorLoc = ((PPCOperand &)*Operands[ErrorInfo]).getStartLoc();
1073f4a2713aSLionel Sambuc       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1074f4a2713aSLionel Sambuc     }
1075f4a2713aSLionel Sambuc 
1076f4a2713aSLionel Sambuc     return Error(ErrorLoc, "invalid operand for instruction");
1077f4a2713aSLionel Sambuc   }
1078f4a2713aSLionel Sambuc   }
1079f4a2713aSLionel Sambuc 
1080f4a2713aSLionel Sambuc   llvm_unreachable("Implement any new match types added!");
1081f4a2713aSLionel Sambuc }
1082f4a2713aSLionel Sambuc 
1083f4a2713aSLionel Sambuc bool PPCAsmParser::
MatchRegisterName(const AsmToken & Tok,unsigned & RegNo,int64_t & IntVal)1084f4a2713aSLionel Sambuc MatchRegisterName(const AsmToken &Tok, unsigned &RegNo, int64_t &IntVal) {
1085f4a2713aSLionel Sambuc   if (Tok.is(AsmToken::Identifier)) {
1086f4a2713aSLionel Sambuc     StringRef Name = Tok.getString();
1087f4a2713aSLionel Sambuc 
1088f4a2713aSLionel Sambuc     if (Name.equals_lower("lr")) {
1089f4a2713aSLionel Sambuc       RegNo = isPPC64()? PPC::LR8 : PPC::LR;
1090f4a2713aSLionel Sambuc       IntVal = 8;
1091f4a2713aSLionel Sambuc       return false;
1092f4a2713aSLionel Sambuc     } else if (Name.equals_lower("ctr")) {
1093f4a2713aSLionel Sambuc       RegNo = isPPC64()? PPC::CTR8 : PPC::CTR;
1094f4a2713aSLionel Sambuc       IntVal = 9;
1095f4a2713aSLionel Sambuc       return false;
1096f4a2713aSLionel Sambuc     } else if (Name.equals_lower("vrsave")) {
1097f4a2713aSLionel Sambuc       RegNo = PPC::VRSAVE;
1098f4a2713aSLionel Sambuc       IntVal = 256;
1099f4a2713aSLionel Sambuc       return false;
1100f4a2713aSLionel Sambuc     } else if (Name.startswith_lower("r") &&
1101f4a2713aSLionel Sambuc                !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
1102f4a2713aSLionel Sambuc       RegNo = isPPC64()? XRegs[IntVal] : RRegs[IntVal];
1103f4a2713aSLionel Sambuc       return false;
1104f4a2713aSLionel Sambuc     } else if (Name.startswith_lower("f") &&
1105f4a2713aSLionel Sambuc                !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
1106f4a2713aSLionel Sambuc       RegNo = FRegs[IntVal];
1107f4a2713aSLionel Sambuc       return false;
1108f4a2713aSLionel Sambuc     } else if (Name.startswith_lower("v") &&
1109f4a2713aSLionel Sambuc                !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
1110f4a2713aSLionel Sambuc       RegNo = VRegs[IntVal];
1111f4a2713aSLionel Sambuc       return false;
1112f4a2713aSLionel Sambuc     } else if (Name.startswith_lower("cr") &&
1113f4a2713aSLionel Sambuc                !Name.substr(2).getAsInteger(10, IntVal) && IntVal < 8) {
1114f4a2713aSLionel Sambuc       RegNo = CRRegs[IntVal];
1115f4a2713aSLionel Sambuc       return false;
1116f4a2713aSLionel Sambuc     }
1117f4a2713aSLionel Sambuc   }
1118f4a2713aSLionel Sambuc 
1119f4a2713aSLionel Sambuc   return true;
1120f4a2713aSLionel Sambuc }
1121f4a2713aSLionel Sambuc 
1122f4a2713aSLionel Sambuc bool PPCAsmParser::
ParseRegister(unsigned & RegNo,SMLoc & StartLoc,SMLoc & EndLoc)1123f4a2713aSLionel Sambuc ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1124*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1125f4a2713aSLionel Sambuc   const AsmToken &Tok = Parser.getTok();
1126f4a2713aSLionel Sambuc   StartLoc = Tok.getLoc();
1127f4a2713aSLionel Sambuc   EndLoc = Tok.getEndLoc();
1128f4a2713aSLionel Sambuc   RegNo = 0;
1129f4a2713aSLionel Sambuc   int64_t IntVal;
1130f4a2713aSLionel Sambuc 
1131f4a2713aSLionel Sambuc   if (!MatchRegisterName(Tok, RegNo, IntVal)) {
1132f4a2713aSLionel Sambuc     Parser.Lex(); // Eat identifier token.
1133f4a2713aSLionel Sambuc     return false;
1134f4a2713aSLionel Sambuc   }
1135f4a2713aSLionel Sambuc 
1136f4a2713aSLionel Sambuc   return Error(StartLoc, "invalid register name");
1137f4a2713aSLionel Sambuc }
1138f4a2713aSLionel Sambuc 
1139f4a2713aSLionel Sambuc /// Extract \code @l/@ha \endcode modifier from expression.  Recursively scan
1140f4a2713aSLionel Sambuc /// the expression and check for VK_PPC_LO/HI/HA
1141f4a2713aSLionel Sambuc /// symbol variants.  If all symbols with modifier use the same
1142f4a2713aSLionel Sambuc /// variant, return the corresponding PPCMCExpr::VariantKind,
1143f4a2713aSLionel Sambuc /// and a modified expression using the default symbol variant.
1144f4a2713aSLionel Sambuc /// Otherwise, return NULL.
1145f4a2713aSLionel Sambuc const MCExpr *PPCAsmParser::
ExtractModifierFromExpr(const MCExpr * E,PPCMCExpr::VariantKind & Variant)1146f4a2713aSLionel Sambuc ExtractModifierFromExpr(const MCExpr *E,
1147f4a2713aSLionel Sambuc                         PPCMCExpr::VariantKind &Variant) {
1148f4a2713aSLionel Sambuc   MCContext &Context = getParser().getContext();
1149f4a2713aSLionel Sambuc   Variant = PPCMCExpr::VK_PPC_None;
1150f4a2713aSLionel Sambuc 
1151f4a2713aSLionel Sambuc   switch (E->getKind()) {
1152f4a2713aSLionel Sambuc   case MCExpr::Target:
1153f4a2713aSLionel Sambuc   case MCExpr::Constant:
1154*0a6a1f1dSLionel Sambuc     return nullptr;
1155f4a2713aSLionel Sambuc 
1156f4a2713aSLionel Sambuc   case MCExpr::SymbolRef: {
1157f4a2713aSLionel Sambuc     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1158f4a2713aSLionel Sambuc 
1159f4a2713aSLionel Sambuc     switch (SRE->getKind()) {
1160f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_LO:
1161f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_LO;
1162f4a2713aSLionel Sambuc       break;
1163f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_HI:
1164f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HI;
1165f4a2713aSLionel Sambuc       break;
1166f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_HA:
1167f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HA;
1168f4a2713aSLionel Sambuc       break;
1169f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_HIGHER:
1170f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HIGHER;
1171f4a2713aSLionel Sambuc       break;
1172f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_HIGHERA:
1173f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HIGHERA;
1174f4a2713aSLionel Sambuc       break;
1175f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_HIGHEST:
1176f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HIGHEST;
1177f4a2713aSLionel Sambuc       break;
1178f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_PPC_HIGHESTA:
1179f4a2713aSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HIGHESTA;
1180f4a2713aSLionel Sambuc       break;
1181f4a2713aSLionel Sambuc     default:
1182*0a6a1f1dSLionel Sambuc       return nullptr;
1183f4a2713aSLionel Sambuc     }
1184f4a2713aSLionel Sambuc 
1185f4a2713aSLionel Sambuc     return MCSymbolRefExpr::Create(&SRE->getSymbol(), Context);
1186f4a2713aSLionel Sambuc   }
1187f4a2713aSLionel Sambuc 
1188f4a2713aSLionel Sambuc   case MCExpr::Unary: {
1189f4a2713aSLionel Sambuc     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
1190f4a2713aSLionel Sambuc     const MCExpr *Sub = ExtractModifierFromExpr(UE->getSubExpr(), Variant);
1191f4a2713aSLionel Sambuc     if (!Sub)
1192*0a6a1f1dSLionel Sambuc       return nullptr;
1193f4a2713aSLionel Sambuc     return MCUnaryExpr::Create(UE->getOpcode(), Sub, Context);
1194f4a2713aSLionel Sambuc   }
1195f4a2713aSLionel Sambuc 
1196f4a2713aSLionel Sambuc   case MCExpr::Binary: {
1197f4a2713aSLionel Sambuc     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1198f4a2713aSLionel Sambuc     PPCMCExpr::VariantKind LHSVariant, RHSVariant;
1199f4a2713aSLionel Sambuc     const MCExpr *LHS = ExtractModifierFromExpr(BE->getLHS(), LHSVariant);
1200f4a2713aSLionel Sambuc     const MCExpr *RHS = ExtractModifierFromExpr(BE->getRHS(), RHSVariant);
1201f4a2713aSLionel Sambuc 
1202f4a2713aSLionel Sambuc     if (!LHS && !RHS)
1203*0a6a1f1dSLionel Sambuc       return nullptr;
1204f4a2713aSLionel Sambuc 
1205f4a2713aSLionel Sambuc     if (!LHS) LHS = BE->getLHS();
1206f4a2713aSLionel Sambuc     if (!RHS) RHS = BE->getRHS();
1207f4a2713aSLionel Sambuc 
1208f4a2713aSLionel Sambuc     if (LHSVariant == PPCMCExpr::VK_PPC_None)
1209f4a2713aSLionel Sambuc       Variant = RHSVariant;
1210f4a2713aSLionel Sambuc     else if (RHSVariant == PPCMCExpr::VK_PPC_None)
1211f4a2713aSLionel Sambuc       Variant = LHSVariant;
1212f4a2713aSLionel Sambuc     else if (LHSVariant == RHSVariant)
1213f4a2713aSLionel Sambuc       Variant = LHSVariant;
1214f4a2713aSLionel Sambuc     else
1215*0a6a1f1dSLionel Sambuc       return nullptr;
1216f4a2713aSLionel Sambuc 
1217f4a2713aSLionel Sambuc     return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, Context);
1218f4a2713aSLionel Sambuc   }
1219f4a2713aSLionel Sambuc   }
1220f4a2713aSLionel Sambuc 
1221f4a2713aSLionel Sambuc   llvm_unreachable("Invalid expression kind!");
1222f4a2713aSLionel Sambuc }
1223f4a2713aSLionel Sambuc 
1224f4a2713aSLionel Sambuc /// Find all VK_TLSGD/VK_TLSLD symbol references in expression and replace
1225f4a2713aSLionel Sambuc /// them by VK_PPC_TLSGD/VK_PPC_TLSLD.  This is necessary to avoid having
1226f4a2713aSLionel Sambuc /// _GLOBAL_OFFSET_TABLE_ created via ELFObjectWriter::RelocNeedsGOT.
1227f4a2713aSLionel Sambuc /// FIXME: This is a hack.
1228f4a2713aSLionel Sambuc const MCExpr *PPCAsmParser::
FixupVariantKind(const MCExpr * E)1229f4a2713aSLionel Sambuc FixupVariantKind(const MCExpr *E) {
1230f4a2713aSLionel Sambuc   MCContext &Context = getParser().getContext();
1231f4a2713aSLionel Sambuc 
1232f4a2713aSLionel Sambuc   switch (E->getKind()) {
1233f4a2713aSLionel Sambuc   case MCExpr::Target:
1234f4a2713aSLionel Sambuc   case MCExpr::Constant:
1235f4a2713aSLionel Sambuc     return E;
1236f4a2713aSLionel Sambuc 
1237f4a2713aSLionel Sambuc   case MCExpr::SymbolRef: {
1238f4a2713aSLionel Sambuc     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1239f4a2713aSLionel Sambuc     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1240f4a2713aSLionel Sambuc 
1241f4a2713aSLionel Sambuc     switch (SRE->getKind()) {
1242f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_TLSGD:
1243f4a2713aSLionel Sambuc       Variant = MCSymbolRefExpr::VK_PPC_TLSGD;
1244f4a2713aSLionel Sambuc       break;
1245f4a2713aSLionel Sambuc     case MCSymbolRefExpr::VK_TLSLD:
1246f4a2713aSLionel Sambuc       Variant = MCSymbolRefExpr::VK_PPC_TLSLD;
1247f4a2713aSLionel Sambuc       break;
1248f4a2713aSLionel Sambuc     default:
1249f4a2713aSLionel Sambuc       return E;
1250f4a2713aSLionel Sambuc     }
1251f4a2713aSLionel Sambuc     return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, Context);
1252f4a2713aSLionel Sambuc   }
1253f4a2713aSLionel Sambuc 
1254f4a2713aSLionel Sambuc   case MCExpr::Unary: {
1255f4a2713aSLionel Sambuc     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
1256f4a2713aSLionel Sambuc     const MCExpr *Sub = FixupVariantKind(UE->getSubExpr());
1257f4a2713aSLionel Sambuc     if (Sub == UE->getSubExpr())
1258f4a2713aSLionel Sambuc       return E;
1259f4a2713aSLionel Sambuc     return MCUnaryExpr::Create(UE->getOpcode(), Sub, Context);
1260f4a2713aSLionel Sambuc   }
1261f4a2713aSLionel Sambuc 
1262f4a2713aSLionel Sambuc   case MCExpr::Binary: {
1263f4a2713aSLionel Sambuc     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1264f4a2713aSLionel Sambuc     const MCExpr *LHS = FixupVariantKind(BE->getLHS());
1265f4a2713aSLionel Sambuc     const MCExpr *RHS = FixupVariantKind(BE->getRHS());
1266f4a2713aSLionel Sambuc     if (LHS == BE->getLHS() && RHS == BE->getRHS())
1267f4a2713aSLionel Sambuc       return E;
1268f4a2713aSLionel Sambuc     return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, Context);
1269f4a2713aSLionel Sambuc   }
1270f4a2713aSLionel Sambuc   }
1271f4a2713aSLionel Sambuc 
1272f4a2713aSLionel Sambuc   llvm_unreachable("Invalid expression kind!");
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc 
1275*0a6a1f1dSLionel Sambuc /// ParseExpression.  This differs from the default "parseExpression" in that
1276*0a6a1f1dSLionel Sambuc /// it handles modifiers.
1277f4a2713aSLionel Sambuc bool PPCAsmParser::
ParseExpression(const MCExpr * & EVal)1278f4a2713aSLionel Sambuc ParseExpression(const MCExpr *&EVal) {
1279*0a6a1f1dSLionel Sambuc 
1280*0a6a1f1dSLionel Sambuc   if (isDarwin())
1281*0a6a1f1dSLionel Sambuc     return ParseDarwinExpression(EVal);
1282*0a6a1f1dSLionel Sambuc 
1283*0a6a1f1dSLionel Sambuc   // (ELF Platforms)
1284*0a6a1f1dSLionel Sambuc   // Handle \code @l/@ha \endcode
1285f4a2713aSLionel Sambuc   if (getParser().parseExpression(EVal))
1286f4a2713aSLionel Sambuc     return true;
1287f4a2713aSLionel Sambuc 
1288f4a2713aSLionel Sambuc   EVal = FixupVariantKind(EVal);
1289f4a2713aSLionel Sambuc 
1290f4a2713aSLionel Sambuc   PPCMCExpr::VariantKind Variant;
1291f4a2713aSLionel Sambuc   const MCExpr *E = ExtractModifierFromExpr(EVal, Variant);
1292f4a2713aSLionel Sambuc   if (E)
1293f4a2713aSLionel Sambuc     EVal = PPCMCExpr::Create(Variant, E, false, getParser().getContext());
1294f4a2713aSLionel Sambuc 
1295f4a2713aSLionel Sambuc   return false;
1296f4a2713aSLionel Sambuc }
1297f4a2713aSLionel Sambuc 
1298*0a6a1f1dSLionel Sambuc /// ParseDarwinExpression.  (MachO Platforms)
1299*0a6a1f1dSLionel Sambuc /// This differs from the default "parseExpression" in that it handles detection
1300*0a6a1f1dSLionel Sambuc /// of the \code hi16(), ha16() and lo16() \endcode modifiers.  At present,
1301*0a6a1f1dSLionel Sambuc /// parseExpression() doesn't recognise the modifiers when in the Darwin/MachO
1302*0a6a1f1dSLionel Sambuc /// syntax form so it is done here.  TODO: Determine if there is merit in arranging
1303*0a6a1f1dSLionel Sambuc /// for this to be done at a higher level.
1304f4a2713aSLionel Sambuc bool PPCAsmParser::
ParseDarwinExpression(const MCExpr * & EVal)1305*0a6a1f1dSLionel Sambuc ParseDarwinExpression(const MCExpr *&EVal) {
1306*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1307*0a6a1f1dSLionel Sambuc   PPCMCExpr::VariantKind Variant = PPCMCExpr::VK_PPC_None;
1308*0a6a1f1dSLionel Sambuc   switch (getLexer().getKind()) {
1309*0a6a1f1dSLionel Sambuc   default:
1310*0a6a1f1dSLionel Sambuc     break;
1311*0a6a1f1dSLionel Sambuc   case AsmToken::Identifier:
1312*0a6a1f1dSLionel Sambuc     // Compiler-generated Darwin identifiers begin with L,l,_ or "; thus
1313*0a6a1f1dSLionel Sambuc     // something starting with any other char should be part of the
1314*0a6a1f1dSLionel Sambuc     // asm syntax.  If handwritten asm includes an identifier like lo16,
1315*0a6a1f1dSLionel Sambuc     // then all bets are off - but no-one would do that, right?
1316*0a6a1f1dSLionel Sambuc     StringRef poss = Parser.getTok().getString();
1317*0a6a1f1dSLionel Sambuc     if (poss.equals_lower("lo16")) {
1318*0a6a1f1dSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_LO;
1319*0a6a1f1dSLionel Sambuc     } else if (poss.equals_lower("hi16")) {
1320*0a6a1f1dSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HI;
1321*0a6a1f1dSLionel Sambuc     } else if (poss.equals_lower("ha16")) {
1322*0a6a1f1dSLionel Sambuc       Variant = PPCMCExpr::VK_PPC_HA;
1323*0a6a1f1dSLionel Sambuc     }
1324*0a6a1f1dSLionel Sambuc     if (Variant != PPCMCExpr::VK_PPC_None) {
1325*0a6a1f1dSLionel Sambuc       Parser.Lex(); // Eat the xx16
1326*0a6a1f1dSLionel Sambuc       if (getLexer().isNot(AsmToken::LParen))
1327*0a6a1f1dSLionel Sambuc         return Error(Parser.getTok().getLoc(), "expected '('");
1328*0a6a1f1dSLionel Sambuc       Parser.Lex(); // Eat the '('
1329*0a6a1f1dSLionel Sambuc     }
1330*0a6a1f1dSLionel Sambuc     break;
1331*0a6a1f1dSLionel Sambuc   }
1332*0a6a1f1dSLionel Sambuc 
1333*0a6a1f1dSLionel Sambuc   if (getParser().parseExpression(EVal))
1334*0a6a1f1dSLionel Sambuc     return true;
1335*0a6a1f1dSLionel Sambuc 
1336*0a6a1f1dSLionel Sambuc   if (Variant != PPCMCExpr::VK_PPC_None) {
1337*0a6a1f1dSLionel Sambuc     if (getLexer().isNot(AsmToken::RParen))
1338*0a6a1f1dSLionel Sambuc       return Error(Parser.getTok().getLoc(), "expected ')'");
1339*0a6a1f1dSLionel Sambuc     Parser.Lex(); // Eat the ')'
1340*0a6a1f1dSLionel Sambuc     EVal = PPCMCExpr::Create(Variant, EVal, false, getParser().getContext());
1341*0a6a1f1dSLionel Sambuc   }
1342*0a6a1f1dSLionel Sambuc   return false;
1343*0a6a1f1dSLionel Sambuc }
1344*0a6a1f1dSLionel Sambuc 
1345*0a6a1f1dSLionel Sambuc /// ParseOperand
1346*0a6a1f1dSLionel Sambuc /// This handles registers in the form 'NN', '%rNN' for ELF platforms and
1347*0a6a1f1dSLionel Sambuc /// rNN for MachO.
ParseOperand(OperandVector & Operands)1348*0a6a1f1dSLionel Sambuc bool PPCAsmParser::ParseOperand(OperandVector &Operands) {
1349*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1350f4a2713aSLionel Sambuc   SMLoc S = Parser.getTok().getLoc();
1351f4a2713aSLionel Sambuc   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1352f4a2713aSLionel Sambuc   const MCExpr *EVal;
1353f4a2713aSLionel Sambuc 
1354f4a2713aSLionel Sambuc   // Attempt to parse the next token as an immediate
1355f4a2713aSLionel Sambuc   switch (getLexer().getKind()) {
1356f4a2713aSLionel Sambuc   // Special handling for register names.  These are interpreted
1357f4a2713aSLionel Sambuc   // as immediates corresponding to the register number.
1358f4a2713aSLionel Sambuc   case AsmToken::Percent:
1359f4a2713aSLionel Sambuc     Parser.Lex(); // Eat the '%'.
1360f4a2713aSLionel Sambuc     unsigned RegNo;
1361f4a2713aSLionel Sambuc     int64_t IntVal;
1362f4a2713aSLionel Sambuc     if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) {
1363f4a2713aSLionel Sambuc       Parser.Lex(); // Eat the identifier token.
1364*0a6a1f1dSLionel Sambuc       Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
1365f4a2713aSLionel Sambuc       return false;
1366f4a2713aSLionel Sambuc     }
1367f4a2713aSLionel Sambuc     return Error(S, "invalid register name");
1368f4a2713aSLionel Sambuc 
1369*0a6a1f1dSLionel Sambuc   case AsmToken::Identifier:
1370*0a6a1f1dSLionel Sambuc     // Note that non-register-name identifiers from the compiler will begin
1371*0a6a1f1dSLionel Sambuc     // with '_', 'L'/'l' or '"'.  Of course, handwritten asm could include
1372*0a6a1f1dSLionel Sambuc     // identifiers like r31foo - so we fall through in the event that parsing
1373*0a6a1f1dSLionel Sambuc     // a register name fails.
1374*0a6a1f1dSLionel Sambuc     if (isDarwin()) {
1375*0a6a1f1dSLionel Sambuc       unsigned RegNo;
1376*0a6a1f1dSLionel Sambuc       int64_t IntVal;
1377*0a6a1f1dSLionel Sambuc       if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) {
1378*0a6a1f1dSLionel Sambuc         Parser.Lex(); // Eat the identifier token.
1379*0a6a1f1dSLionel Sambuc         Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
1380*0a6a1f1dSLionel Sambuc         return false;
1381*0a6a1f1dSLionel Sambuc       }
1382*0a6a1f1dSLionel Sambuc     }
1383*0a6a1f1dSLionel Sambuc   // Fall-through to process non-register-name identifiers as expression.
1384f4a2713aSLionel Sambuc   // All other expressions
1385f4a2713aSLionel Sambuc   case AsmToken::LParen:
1386f4a2713aSLionel Sambuc   case AsmToken::Plus:
1387f4a2713aSLionel Sambuc   case AsmToken::Minus:
1388f4a2713aSLionel Sambuc   case AsmToken::Integer:
1389f4a2713aSLionel Sambuc   case AsmToken::Dot:
1390f4a2713aSLionel Sambuc   case AsmToken::Dollar:
1391*0a6a1f1dSLionel Sambuc   case AsmToken::Exclaim:
1392*0a6a1f1dSLionel Sambuc   case AsmToken::Tilde:
1393f4a2713aSLionel Sambuc     if (!ParseExpression(EVal))
1394f4a2713aSLionel Sambuc       break;
1395f4a2713aSLionel Sambuc     /* fall through */
1396f4a2713aSLionel Sambuc   default:
1397f4a2713aSLionel Sambuc     return Error(S, "unknown operand");
1398f4a2713aSLionel Sambuc   }
1399f4a2713aSLionel Sambuc 
1400f4a2713aSLionel Sambuc   // Push the parsed operand into the list of operands
1401*0a6a1f1dSLionel Sambuc   Operands.push_back(PPCOperand::CreateFromMCExpr(EVal, S, E, isPPC64()));
1402f4a2713aSLionel Sambuc 
1403f4a2713aSLionel Sambuc   // Check whether this is a TLS call expression
1404f4a2713aSLionel Sambuc   bool TLSCall = false;
1405f4a2713aSLionel Sambuc   if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(EVal))
1406f4a2713aSLionel Sambuc     TLSCall = Ref->getSymbol().getName() == "__tls_get_addr";
1407f4a2713aSLionel Sambuc 
1408f4a2713aSLionel Sambuc   if (TLSCall && getLexer().is(AsmToken::LParen)) {
1409f4a2713aSLionel Sambuc     const MCExpr *TLSSym;
1410f4a2713aSLionel Sambuc 
1411f4a2713aSLionel Sambuc     Parser.Lex(); // Eat the '('.
1412f4a2713aSLionel Sambuc     S = Parser.getTok().getLoc();
1413f4a2713aSLionel Sambuc     if (ParseExpression(TLSSym))
1414f4a2713aSLionel Sambuc       return Error(S, "invalid TLS call expression");
1415f4a2713aSLionel Sambuc     if (getLexer().isNot(AsmToken::RParen))
1416f4a2713aSLionel Sambuc       return Error(Parser.getTok().getLoc(), "missing ')'");
1417f4a2713aSLionel Sambuc     E = Parser.getTok().getLoc();
1418f4a2713aSLionel Sambuc     Parser.Lex(); // Eat the ')'.
1419f4a2713aSLionel Sambuc 
1420*0a6a1f1dSLionel Sambuc     Operands.push_back(PPCOperand::CreateFromMCExpr(TLSSym, S, E, isPPC64()));
1421f4a2713aSLionel Sambuc   }
1422f4a2713aSLionel Sambuc 
1423f4a2713aSLionel Sambuc   // Otherwise, check for D-form memory operands
1424f4a2713aSLionel Sambuc   if (!TLSCall && getLexer().is(AsmToken::LParen)) {
1425f4a2713aSLionel Sambuc     Parser.Lex(); // Eat the '('.
1426f4a2713aSLionel Sambuc     S = Parser.getTok().getLoc();
1427f4a2713aSLionel Sambuc 
1428f4a2713aSLionel Sambuc     int64_t IntVal;
1429f4a2713aSLionel Sambuc     switch (getLexer().getKind()) {
1430f4a2713aSLionel Sambuc     case AsmToken::Percent:
1431f4a2713aSLionel Sambuc       Parser.Lex(); // Eat the '%'.
1432f4a2713aSLionel Sambuc       unsigned RegNo;
1433f4a2713aSLionel Sambuc       if (MatchRegisterName(Parser.getTok(), RegNo, IntVal))
1434f4a2713aSLionel Sambuc         return Error(S, "invalid register name");
1435f4a2713aSLionel Sambuc       Parser.Lex(); // Eat the identifier token.
1436f4a2713aSLionel Sambuc       break;
1437f4a2713aSLionel Sambuc 
1438f4a2713aSLionel Sambuc     case AsmToken::Integer:
1439*0a6a1f1dSLionel Sambuc       if (!isDarwin()) {
1440f4a2713aSLionel Sambuc         if (getParser().parseAbsoluteExpression(IntVal) ||
1441f4a2713aSLionel Sambuc           IntVal < 0 || IntVal > 31)
1442f4a2713aSLionel Sambuc         return Error(S, "invalid register number");
1443*0a6a1f1dSLionel Sambuc       } else {
1444*0a6a1f1dSLionel Sambuc         return Error(S, "unexpected integer value");
1445*0a6a1f1dSLionel Sambuc       }
1446f4a2713aSLionel Sambuc       break;
1447f4a2713aSLionel Sambuc 
1448*0a6a1f1dSLionel Sambuc    case AsmToken::Identifier:
1449*0a6a1f1dSLionel Sambuc     if (isDarwin()) {
1450*0a6a1f1dSLionel Sambuc       unsigned RegNo;
1451*0a6a1f1dSLionel Sambuc       if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) {
1452*0a6a1f1dSLionel Sambuc         Parser.Lex(); // Eat the identifier token.
1453*0a6a1f1dSLionel Sambuc         break;
1454*0a6a1f1dSLionel Sambuc       }
1455*0a6a1f1dSLionel Sambuc     }
1456*0a6a1f1dSLionel Sambuc     // Fall-through..
1457*0a6a1f1dSLionel Sambuc 
1458f4a2713aSLionel Sambuc     default:
1459f4a2713aSLionel Sambuc       return Error(S, "invalid memory operand");
1460f4a2713aSLionel Sambuc     }
1461f4a2713aSLionel Sambuc 
1462f4a2713aSLionel Sambuc     if (getLexer().isNot(AsmToken::RParen))
1463f4a2713aSLionel Sambuc       return Error(Parser.getTok().getLoc(), "missing ')'");
1464f4a2713aSLionel Sambuc     E = Parser.getTok().getLoc();
1465f4a2713aSLionel Sambuc     Parser.Lex(); // Eat the ')'.
1466f4a2713aSLionel Sambuc 
1467*0a6a1f1dSLionel Sambuc     Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
1468f4a2713aSLionel Sambuc   }
1469f4a2713aSLionel Sambuc 
1470f4a2713aSLionel Sambuc   return false;
1471f4a2713aSLionel Sambuc }
1472f4a2713aSLionel Sambuc 
1473f4a2713aSLionel Sambuc /// Parse an instruction mnemonic followed by its operands.
ParseInstruction(ParseInstructionInfo & Info,StringRef Name,SMLoc NameLoc,OperandVector & Operands)1474*0a6a1f1dSLionel Sambuc bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
1475*0a6a1f1dSLionel Sambuc                                     SMLoc NameLoc, OperandVector &Operands) {
1476f4a2713aSLionel Sambuc   // The first operand is the token for the instruction name.
1477f4a2713aSLionel Sambuc   // If the next character is a '+' or '-', we need to add it to the
1478f4a2713aSLionel Sambuc   // instruction name, to match what TableGen is doing.
1479f4a2713aSLionel Sambuc   std::string NewOpcode;
1480f4a2713aSLionel Sambuc   if (getLexer().is(AsmToken::Plus)) {
1481f4a2713aSLionel Sambuc     getLexer().Lex();
1482f4a2713aSLionel Sambuc     NewOpcode = Name;
1483f4a2713aSLionel Sambuc     NewOpcode += '+';
1484f4a2713aSLionel Sambuc     Name = NewOpcode;
1485f4a2713aSLionel Sambuc   }
1486f4a2713aSLionel Sambuc   if (getLexer().is(AsmToken::Minus)) {
1487f4a2713aSLionel Sambuc     getLexer().Lex();
1488f4a2713aSLionel Sambuc     NewOpcode = Name;
1489f4a2713aSLionel Sambuc     NewOpcode += '-';
1490f4a2713aSLionel Sambuc     Name = NewOpcode;
1491f4a2713aSLionel Sambuc   }
1492f4a2713aSLionel Sambuc   // If the instruction ends in a '.', we need to create a separate
1493f4a2713aSLionel Sambuc   // token for it, to match what TableGen is doing.
1494f4a2713aSLionel Sambuc   size_t Dot = Name.find('.');
1495f4a2713aSLionel Sambuc   StringRef Mnemonic = Name.slice(0, Dot);
1496f4a2713aSLionel Sambuc   if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
1497f4a2713aSLionel Sambuc     Operands.push_back(
1498f4a2713aSLionel Sambuc         PPCOperand::CreateTokenWithStringCopy(Mnemonic, NameLoc, isPPC64()));
1499f4a2713aSLionel Sambuc   else
1500f4a2713aSLionel Sambuc     Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64()));
1501f4a2713aSLionel Sambuc   if (Dot != StringRef::npos) {
1502f4a2713aSLionel Sambuc     SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot);
1503f4a2713aSLionel Sambuc     StringRef DotStr = Name.slice(Dot, StringRef::npos);
1504f4a2713aSLionel Sambuc     if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
1505f4a2713aSLionel Sambuc       Operands.push_back(
1506f4a2713aSLionel Sambuc           PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64()));
1507f4a2713aSLionel Sambuc     else
1508f4a2713aSLionel Sambuc       Operands.push_back(PPCOperand::CreateToken(DotStr, DotLoc, isPPC64()));
1509f4a2713aSLionel Sambuc   }
1510f4a2713aSLionel Sambuc 
1511f4a2713aSLionel Sambuc   // If there are no more operands then finish
1512f4a2713aSLionel Sambuc   if (getLexer().is(AsmToken::EndOfStatement))
1513f4a2713aSLionel Sambuc     return false;
1514f4a2713aSLionel Sambuc 
1515f4a2713aSLionel Sambuc   // Parse the first operand
1516f4a2713aSLionel Sambuc   if (ParseOperand(Operands))
1517f4a2713aSLionel Sambuc     return true;
1518f4a2713aSLionel Sambuc 
1519f4a2713aSLionel Sambuc   while (getLexer().isNot(AsmToken::EndOfStatement) &&
1520f4a2713aSLionel Sambuc          getLexer().is(AsmToken::Comma)) {
1521f4a2713aSLionel Sambuc     // Consume the comma token
1522f4a2713aSLionel Sambuc     getLexer().Lex();
1523f4a2713aSLionel Sambuc 
1524f4a2713aSLionel Sambuc     // Parse the next operand
1525f4a2713aSLionel Sambuc     if (ParseOperand(Operands))
1526f4a2713aSLionel Sambuc       return true;
1527f4a2713aSLionel Sambuc   }
1528f4a2713aSLionel Sambuc 
1529f4a2713aSLionel Sambuc   return false;
1530f4a2713aSLionel Sambuc }
1531f4a2713aSLionel Sambuc 
1532f4a2713aSLionel Sambuc /// ParseDirective parses the PPC specific directives
ParseDirective(AsmToken DirectiveID)1533f4a2713aSLionel Sambuc bool PPCAsmParser::ParseDirective(AsmToken DirectiveID) {
1534f4a2713aSLionel Sambuc   StringRef IDVal = DirectiveID.getIdentifier();
1535*0a6a1f1dSLionel Sambuc   if (!isDarwin()) {
1536f4a2713aSLionel Sambuc     if (IDVal == ".word")
1537f4a2713aSLionel Sambuc       return ParseDirectiveWord(2, DirectiveID.getLoc());
1538f4a2713aSLionel Sambuc     if (IDVal == ".llong")
1539f4a2713aSLionel Sambuc       return ParseDirectiveWord(8, DirectiveID.getLoc());
1540f4a2713aSLionel Sambuc     if (IDVal == ".tc")
1541f4a2713aSLionel Sambuc       return ParseDirectiveTC(isPPC64()? 8 : 4, DirectiveID.getLoc());
1542f4a2713aSLionel Sambuc     if (IDVal == ".machine")
1543f4a2713aSLionel Sambuc       return ParseDirectiveMachine(DirectiveID.getLoc());
1544*0a6a1f1dSLionel Sambuc     if (IDVal == ".abiversion")
1545*0a6a1f1dSLionel Sambuc       return ParseDirectiveAbiVersion(DirectiveID.getLoc());
1546*0a6a1f1dSLionel Sambuc     if (IDVal == ".localentry")
1547*0a6a1f1dSLionel Sambuc       return ParseDirectiveLocalEntry(DirectiveID.getLoc());
1548*0a6a1f1dSLionel Sambuc   } else {
1549*0a6a1f1dSLionel Sambuc     if (IDVal == ".machine")
1550*0a6a1f1dSLionel Sambuc       return ParseDarwinDirectiveMachine(DirectiveID.getLoc());
1551*0a6a1f1dSLionel Sambuc   }
1552f4a2713aSLionel Sambuc   return true;
1553f4a2713aSLionel Sambuc }
1554f4a2713aSLionel Sambuc 
1555f4a2713aSLionel Sambuc /// ParseDirectiveWord
1556f4a2713aSLionel Sambuc ///  ::= .word [ expression (, expression)* ]
ParseDirectiveWord(unsigned Size,SMLoc L)1557f4a2713aSLionel Sambuc bool PPCAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1558*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1559f4a2713aSLionel Sambuc   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1560f4a2713aSLionel Sambuc     for (;;) {
1561f4a2713aSLionel Sambuc       const MCExpr *Value;
1562f4a2713aSLionel Sambuc       if (getParser().parseExpression(Value))
1563*0a6a1f1dSLionel Sambuc         return false;
1564f4a2713aSLionel Sambuc 
1565f4a2713aSLionel Sambuc       getParser().getStreamer().EmitValue(Value, Size);
1566f4a2713aSLionel Sambuc 
1567f4a2713aSLionel Sambuc       if (getLexer().is(AsmToken::EndOfStatement))
1568f4a2713aSLionel Sambuc         break;
1569f4a2713aSLionel Sambuc 
1570f4a2713aSLionel Sambuc       if (getLexer().isNot(AsmToken::Comma))
1571f4a2713aSLionel Sambuc         return Error(L, "unexpected token in directive");
1572f4a2713aSLionel Sambuc       Parser.Lex();
1573f4a2713aSLionel Sambuc     }
1574f4a2713aSLionel Sambuc   }
1575f4a2713aSLionel Sambuc 
1576f4a2713aSLionel Sambuc   Parser.Lex();
1577f4a2713aSLionel Sambuc   return false;
1578f4a2713aSLionel Sambuc }
1579f4a2713aSLionel Sambuc 
1580f4a2713aSLionel Sambuc /// ParseDirectiveTC
1581f4a2713aSLionel Sambuc ///  ::= .tc [ symbol (, expression)* ]
ParseDirectiveTC(unsigned Size,SMLoc L)1582f4a2713aSLionel Sambuc bool PPCAsmParser::ParseDirectiveTC(unsigned Size, SMLoc L) {
1583*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1584f4a2713aSLionel Sambuc   // Skip TC symbol, which is only used with XCOFF.
1585f4a2713aSLionel Sambuc   while (getLexer().isNot(AsmToken::EndOfStatement)
1586f4a2713aSLionel Sambuc          && getLexer().isNot(AsmToken::Comma))
1587f4a2713aSLionel Sambuc     Parser.Lex();
1588*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::Comma)) {
1589*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1590*0a6a1f1dSLionel Sambuc     return false;
1591*0a6a1f1dSLionel Sambuc   }
1592f4a2713aSLionel Sambuc   Parser.Lex();
1593f4a2713aSLionel Sambuc 
1594f4a2713aSLionel Sambuc   // Align to word size.
1595f4a2713aSLionel Sambuc   getParser().getStreamer().EmitValueToAlignment(Size);
1596f4a2713aSLionel Sambuc 
1597f4a2713aSLionel Sambuc   // Emit expressions.
1598f4a2713aSLionel Sambuc   return ParseDirectiveWord(Size, L);
1599f4a2713aSLionel Sambuc }
1600f4a2713aSLionel Sambuc 
1601*0a6a1f1dSLionel Sambuc /// ParseDirectiveMachine (ELF platforms)
1602f4a2713aSLionel Sambuc ///  ::= .machine [ cpu | "push" | "pop" ]
ParseDirectiveMachine(SMLoc L)1603f4a2713aSLionel Sambuc bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) {
1604*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1605f4a2713aSLionel Sambuc   if (getLexer().isNot(AsmToken::Identifier) &&
1606*0a6a1f1dSLionel Sambuc       getLexer().isNot(AsmToken::String)) {
1607*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1608*0a6a1f1dSLionel Sambuc     return false;
1609*0a6a1f1dSLionel Sambuc   }
1610f4a2713aSLionel Sambuc 
1611f4a2713aSLionel Sambuc   StringRef CPU = Parser.getTok().getIdentifier();
1612f4a2713aSLionel Sambuc   Parser.Lex();
1613f4a2713aSLionel Sambuc 
1614f4a2713aSLionel Sambuc   // FIXME: Right now, the parser always allows any available
1615f4a2713aSLionel Sambuc   // instruction, so the .machine directive is not useful.
1616f4a2713aSLionel Sambuc   // Implement ".machine any" (by doing nothing) for the benefit
1617f4a2713aSLionel Sambuc   // of existing assembler code.  Likewise, we can then implement
1618f4a2713aSLionel Sambuc   // ".machine push" and ".machine pop" as no-op.
1619*0a6a1f1dSLionel Sambuc   if (CPU != "any" && CPU != "push" && CPU != "pop") {
1620*0a6a1f1dSLionel Sambuc     Error(L, "unrecognized machine type");
1621*0a6a1f1dSLionel Sambuc     return false;
1622*0a6a1f1dSLionel Sambuc   }
1623f4a2713aSLionel Sambuc 
1624*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1625*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1626*0a6a1f1dSLionel Sambuc     return false;
1627*0a6a1f1dSLionel Sambuc   }
1628*0a6a1f1dSLionel Sambuc   PPCTargetStreamer &TStreamer =
1629*0a6a1f1dSLionel Sambuc       *static_cast<PPCTargetStreamer *>(
1630*0a6a1f1dSLionel Sambuc            getParser().getStreamer().getTargetStreamer());
1631*0a6a1f1dSLionel Sambuc   TStreamer.emitMachine(CPU);
1632f4a2713aSLionel Sambuc 
1633f4a2713aSLionel Sambuc   return false;
1634f4a2713aSLionel Sambuc }
1635f4a2713aSLionel Sambuc 
1636*0a6a1f1dSLionel Sambuc /// ParseDarwinDirectiveMachine (Mach-o platforms)
1637*0a6a1f1dSLionel Sambuc ///  ::= .machine cpu-identifier
ParseDarwinDirectiveMachine(SMLoc L)1638*0a6a1f1dSLionel Sambuc bool PPCAsmParser::ParseDarwinDirectiveMachine(SMLoc L) {
1639*0a6a1f1dSLionel Sambuc   MCAsmParser &Parser = getParser();
1640*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::Identifier) &&
1641*0a6a1f1dSLionel Sambuc       getLexer().isNot(AsmToken::String)) {
1642*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1643*0a6a1f1dSLionel Sambuc     return false;
1644*0a6a1f1dSLionel Sambuc   }
1645*0a6a1f1dSLionel Sambuc 
1646*0a6a1f1dSLionel Sambuc   StringRef CPU = Parser.getTok().getIdentifier();
1647*0a6a1f1dSLionel Sambuc   Parser.Lex();
1648*0a6a1f1dSLionel Sambuc 
1649*0a6a1f1dSLionel Sambuc   // FIXME: this is only the 'default' set of cpu variants.
1650*0a6a1f1dSLionel Sambuc   // However we don't act on this information at present, this is simply
1651*0a6a1f1dSLionel Sambuc   // allowing parsing to proceed with minimal sanity checking.
1652*0a6a1f1dSLionel Sambuc   if (CPU != "ppc7400" && CPU != "ppc" && CPU != "ppc64") {
1653*0a6a1f1dSLionel Sambuc     Error(L, "unrecognized cpu type");
1654*0a6a1f1dSLionel Sambuc     return false;
1655*0a6a1f1dSLionel Sambuc   }
1656*0a6a1f1dSLionel Sambuc 
1657*0a6a1f1dSLionel Sambuc   if (isPPC64() && (CPU == "ppc7400" || CPU == "ppc")) {
1658*0a6a1f1dSLionel Sambuc     Error(L, "wrong cpu type specified for 64bit");
1659*0a6a1f1dSLionel Sambuc     return false;
1660*0a6a1f1dSLionel Sambuc   }
1661*0a6a1f1dSLionel Sambuc   if (!isPPC64() && CPU == "ppc64") {
1662*0a6a1f1dSLionel Sambuc     Error(L, "wrong cpu type specified for 32bit");
1663*0a6a1f1dSLionel Sambuc     return false;
1664*0a6a1f1dSLionel Sambuc   }
1665*0a6a1f1dSLionel Sambuc 
1666*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1667*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1668*0a6a1f1dSLionel Sambuc     return false;
1669*0a6a1f1dSLionel Sambuc   }
1670*0a6a1f1dSLionel Sambuc 
1671*0a6a1f1dSLionel Sambuc   return false;
1672*0a6a1f1dSLionel Sambuc }
1673*0a6a1f1dSLionel Sambuc 
1674*0a6a1f1dSLionel Sambuc /// ParseDirectiveAbiVersion
1675*0a6a1f1dSLionel Sambuc ///  ::= .abiversion constant-expression
ParseDirectiveAbiVersion(SMLoc L)1676*0a6a1f1dSLionel Sambuc bool PPCAsmParser::ParseDirectiveAbiVersion(SMLoc L) {
1677*0a6a1f1dSLionel Sambuc   int64_t AbiVersion;
1678*0a6a1f1dSLionel Sambuc   if (getParser().parseAbsoluteExpression(AbiVersion)){
1679*0a6a1f1dSLionel Sambuc     Error(L, "expected constant expression");
1680*0a6a1f1dSLionel Sambuc     return false;
1681*0a6a1f1dSLionel Sambuc   }
1682*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1683*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1684*0a6a1f1dSLionel Sambuc     return false;
1685*0a6a1f1dSLionel Sambuc   }
1686*0a6a1f1dSLionel Sambuc 
1687*0a6a1f1dSLionel Sambuc   PPCTargetStreamer &TStreamer =
1688*0a6a1f1dSLionel Sambuc       *static_cast<PPCTargetStreamer *>(
1689*0a6a1f1dSLionel Sambuc            getParser().getStreamer().getTargetStreamer());
1690*0a6a1f1dSLionel Sambuc   TStreamer.emitAbiVersion(AbiVersion);
1691*0a6a1f1dSLionel Sambuc 
1692*0a6a1f1dSLionel Sambuc   return false;
1693*0a6a1f1dSLionel Sambuc }
1694*0a6a1f1dSLionel Sambuc 
1695*0a6a1f1dSLionel Sambuc /// ParseDirectiveLocalEntry
1696*0a6a1f1dSLionel Sambuc ///  ::= .localentry symbol, expression
ParseDirectiveLocalEntry(SMLoc L)1697*0a6a1f1dSLionel Sambuc bool PPCAsmParser::ParseDirectiveLocalEntry(SMLoc L) {
1698*0a6a1f1dSLionel Sambuc   StringRef Name;
1699*0a6a1f1dSLionel Sambuc   if (getParser().parseIdentifier(Name)) {
1700*0a6a1f1dSLionel Sambuc     Error(L, "expected identifier in directive");
1701*0a6a1f1dSLionel Sambuc     return false;
1702*0a6a1f1dSLionel Sambuc   }
1703*0a6a1f1dSLionel Sambuc   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1704*0a6a1f1dSLionel Sambuc 
1705*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::Comma)) {
1706*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1707*0a6a1f1dSLionel Sambuc     return false;
1708*0a6a1f1dSLionel Sambuc   }
1709*0a6a1f1dSLionel Sambuc   Lex();
1710*0a6a1f1dSLionel Sambuc 
1711*0a6a1f1dSLionel Sambuc   const MCExpr *Expr;
1712*0a6a1f1dSLionel Sambuc   if (getParser().parseExpression(Expr)) {
1713*0a6a1f1dSLionel Sambuc     Error(L, "expected expression");
1714*0a6a1f1dSLionel Sambuc     return false;
1715*0a6a1f1dSLionel Sambuc   }
1716*0a6a1f1dSLionel Sambuc 
1717*0a6a1f1dSLionel Sambuc   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1718*0a6a1f1dSLionel Sambuc     Error(L, "unexpected token in directive");
1719*0a6a1f1dSLionel Sambuc     return false;
1720*0a6a1f1dSLionel Sambuc   }
1721*0a6a1f1dSLionel Sambuc 
1722*0a6a1f1dSLionel Sambuc   PPCTargetStreamer &TStreamer =
1723*0a6a1f1dSLionel Sambuc       *static_cast<PPCTargetStreamer *>(
1724*0a6a1f1dSLionel Sambuc            getParser().getStreamer().getTargetStreamer());
1725*0a6a1f1dSLionel Sambuc   TStreamer.emitLocalEntry(Sym, Expr);
1726*0a6a1f1dSLionel Sambuc 
1727*0a6a1f1dSLionel Sambuc   return false;
1728*0a6a1f1dSLionel Sambuc }
1729*0a6a1f1dSLionel Sambuc 
1730*0a6a1f1dSLionel Sambuc 
1731*0a6a1f1dSLionel Sambuc 
1732f4a2713aSLionel Sambuc /// Force static initialization.
LLVMInitializePowerPCAsmParser()1733f4a2713aSLionel Sambuc extern "C" void LLVMInitializePowerPCAsmParser() {
1734f4a2713aSLionel Sambuc   RegisterMCAsmParser<PPCAsmParser> A(ThePPC32Target);
1735f4a2713aSLionel Sambuc   RegisterMCAsmParser<PPCAsmParser> B(ThePPC64Target);
1736f4a2713aSLionel Sambuc   RegisterMCAsmParser<PPCAsmParser> C(ThePPC64LETarget);
1737f4a2713aSLionel Sambuc }
1738f4a2713aSLionel Sambuc 
1739f4a2713aSLionel Sambuc #define GET_REGISTER_MATCHER
1740f4a2713aSLionel Sambuc #define GET_MATCHER_IMPLEMENTATION
1741f4a2713aSLionel Sambuc #include "PPCGenAsmMatcher.inc"
1742f4a2713aSLionel Sambuc 
1743f4a2713aSLionel Sambuc // Define this matcher function after the auto-generated include so we
1744f4a2713aSLionel Sambuc // have the match class enum definitions.
validateTargetOperandClass(MCParsedAsmOperand & AsmOp,unsigned Kind)1745*0a6a1f1dSLionel Sambuc unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1746f4a2713aSLionel Sambuc                                                   unsigned Kind) {
1747f4a2713aSLionel Sambuc   // If the kind is a token for a literal immediate, check if our asm
1748f4a2713aSLionel Sambuc   // operand matches. This is for InstAliases which have a fixed-value
1749f4a2713aSLionel Sambuc   // immediate in the syntax.
1750f4a2713aSLionel Sambuc   int64_t ImmVal;
1751f4a2713aSLionel Sambuc   switch (Kind) {
1752f4a2713aSLionel Sambuc     case MCK_0: ImmVal = 0; break;
1753f4a2713aSLionel Sambuc     case MCK_1: ImmVal = 1; break;
1754f4a2713aSLionel Sambuc     case MCK_2: ImmVal = 2; break;
1755f4a2713aSLionel Sambuc     case MCK_3: ImmVal = 3; break;
1756*0a6a1f1dSLionel Sambuc     case MCK_4: ImmVal = 4; break;
1757*0a6a1f1dSLionel Sambuc     case MCK_5: ImmVal = 5; break;
1758*0a6a1f1dSLionel Sambuc     case MCK_6: ImmVal = 6; break;
1759*0a6a1f1dSLionel Sambuc     case MCK_7: ImmVal = 7; break;
1760f4a2713aSLionel Sambuc     default: return Match_InvalidOperand;
1761f4a2713aSLionel Sambuc   }
1762f4a2713aSLionel Sambuc 
1763*0a6a1f1dSLionel Sambuc   PPCOperand &Op = static_cast<PPCOperand &>(AsmOp);
1764*0a6a1f1dSLionel Sambuc   if (Op.isImm() && Op.getImm() == ImmVal)
1765f4a2713aSLionel Sambuc     return Match_Success;
1766f4a2713aSLionel Sambuc 
1767f4a2713aSLionel Sambuc   return Match_InvalidOperand;
1768f4a2713aSLionel Sambuc }
1769f4a2713aSLionel Sambuc 
1770f4a2713aSLionel Sambuc const MCExpr *
applyModifierToExpr(const MCExpr * E,MCSymbolRefExpr::VariantKind Variant,MCContext & Ctx)1771f4a2713aSLionel Sambuc PPCAsmParser::applyModifierToExpr(const MCExpr *E,
1772f4a2713aSLionel Sambuc                                   MCSymbolRefExpr::VariantKind Variant,
1773f4a2713aSLionel Sambuc                                   MCContext &Ctx) {
1774f4a2713aSLionel Sambuc   switch (Variant) {
1775f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_LO:
1776f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_LO, E, false, Ctx);
1777f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_HI:
1778f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HI, E, false, Ctx);
1779f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_HA:
1780f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HA, E, false, Ctx);
1781f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_HIGHER:
1782f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHER, E, false, Ctx);
1783f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_HIGHERA:
1784f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHERA, E, false, Ctx);
1785f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_HIGHEST:
1786f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHEST, E, false, Ctx);
1787f4a2713aSLionel Sambuc   case MCSymbolRefExpr::VK_PPC_HIGHESTA:
1788f4a2713aSLionel Sambuc     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHESTA, E, false, Ctx);
1789f4a2713aSLionel Sambuc   default:
1790*0a6a1f1dSLionel Sambuc     return nullptr;
1791f4a2713aSLionel Sambuc   }
1792f4a2713aSLionel Sambuc }
1793