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