1 //===-- PPCAsmParser.cpp - Parse PowerPC asm to MCInst instructions -------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/PPCMCExpr.h"
10 #include "MCTargetDesc/PPCMCTargetDesc.h"
11 #include "PPCTargetStreamer.h"
12 #include "TargetInfo/PowerPCTargetInfo.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCParser/MCAsmLexer.h"
21 #include "llvm/MC/MCParser/MCAsmParser.h"
22 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCSymbolELF.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Support/raw_ostream.h"
33 DEFINE_PPC_REGCLASSES
;
35 // Evaluate an expression containing condition register
36 // or condition register field symbols. Returns positive
37 // value on success, or -1 on error.
39 EvaluateCRExpr(const MCExpr
*E
) {
40 switch (E
->getKind()) {
44 case MCExpr::Constant
: {
45 int64_t Res
= cast
<MCConstantExpr
>(E
)->getValue();
46 return Res
< 0 ? -1 : Res
;
49 case MCExpr::SymbolRef
: {
50 const MCSymbolRefExpr
*SRE
= cast
<MCSymbolRefExpr
>(E
);
51 StringRef Name
= SRE
->getSymbol().getName();
53 if (Name
== "lt") return 0;
54 if (Name
== "gt") return 1;
55 if (Name
== "eq") return 2;
56 if (Name
== "so") return 3;
57 if (Name
== "un") return 3;
59 if (Name
== "cr0") return 0;
60 if (Name
== "cr1") return 1;
61 if (Name
== "cr2") return 2;
62 if (Name
== "cr3") return 3;
63 if (Name
== "cr4") return 4;
64 if (Name
== "cr5") return 5;
65 if (Name
== "cr6") return 6;
66 if (Name
== "cr7") return 7;
74 case MCExpr::Binary
: {
75 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(E
);
76 int64_t LHSVal
= EvaluateCRExpr(BE
->getLHS());
77 int64_t RHSVal
= EvaluateCRExpr(BE
->getRHS());
80 if (LHSVal
< 0 || RHSVal
< 0)
83 switch (BE
->getOpcode()) {
85 case MCBinaryExpr::Add
: Res
= LHSVal
+ RHSVal
; break;
86 case MCBinaryExpr::Mul
: Res
= LHSVal
* RHSVal
; break;
89 return Res
< 0 ? -1 : Res
;
93 llvm_unreachable("Invalid expression kind!");
100 class PPCAsmParser
: public MCTargetAsmParser
{
104 void Warning(SMLoc L
, const Twine
&Msg
) { getParser().Warning(L
, Msg
); }
106 bool isPPC64() const { return IsPPC64
; }
107 bool isDarwin() const { return IsDarwin
; }
109 bool MatchRegisterName(unsigned &RegNo
, int64_t &IntVal
);
111 bool ParseRegister(unsigned &RegNo
, SMLoc
&StartLoc
, SMLoc
&EndLoc
) override
;
113 const MCExpr
*ExtractModifierFromExpr(const MCExpr
*E
,
114 PPCMCExpr::VariantKind
&Variant
);
115 const MCExpr
*FixupVariantKind(const MCExpr
*E
);
116 bool ParseExpression(const MCExpr
*&EVal
);
117 bool ParseDarwinExpression(const MCExpr
*&EVal
);
119 bool ParseOperand(OperandVector
&Operands
);
121 bool ParseDirectiveWord(unsigned Size
, AsmToken ID
);
122 bool ParseDirectiveTC(unsigned Size
, AsmToken ID
);
123 bool ParseDirectiveMachine(SMLoc L
);
124 bool ParseDarwinDirectiveMachine(SMLoc L
);
125 bool ParseDirectiveAbiVersion(SMLoc L
);
126 bool ParseDirectiveLocalEntry(SMLoc L
);
128 bool MatchAndEmitInstruction(SMLoc IDLoc
, unsigned &Opcode
,
129 OperandVector
&Operands
, MCStreamer
&Out
,
131 bool MatchingInlineAsm
) override
;
133 void ProcessInstruction(MCInst
&Inst
, const OperandVector
&Ops
);
135 /// @name Auto-generated Match Functions
138 #define GET_ASSEMBLER_HEADER
139 #include "PPCGenAsmMatcher.inc"
145 PPCAsmParser(const MCSubtargetInfo
&STI
, MCAsmParser
&,
146 const MCInstrInfo
&MII
, const MCTargetOptions
&Options
)
147 : MCTargetAsmParser(Options
, STI
, MII
) {
148 // Check for 64-bit vs. 32-bit pointer mode.
149 const Triple
&TheTriple
= STI
.getTargetTriple();
150 IsPPC64
= TheTriple
.isPPC64();
151 IsDarwin
= TheTriple
.isMacOSX();
152 // Initialize the set of available features.
153 setAvailableFeatures(ComputeAvailableFeatures(STI
.getFeatureBits()));
156 bool ParseInstruction(ParseInstructionInfo
&Info
, StringRef Name
,
157 SMLoc NameLoc
, OperandVector
&Operands
) override
;
159 bool ParseDirective(AsmToken DirectiveID
) override
;
161 unsigned validateTargetOperandClass(MCParsedAsmOperand
&Op
,
162 unsigned Kind
) override
;
164 const MCExpr
*applyModifierToExpr(const MCExpr
*E
,
165 MCSymbolRefExpr::VariantKind
,
166 MCContext
&Ctx
) override
;
169 /// PPCOperand - Instances of this class represent a parsed PowerPC machine
171 struct PPCOperand
: public MCParsedAsmOperand
{
180 SMLoc StartLoc
, EndLoc
;
194 int64_t CRVal
; // Cached result of EvaluateCRExpr(Val)
198 const MCSymbolRefExpr
*Sym
;
205 struct TLSRegOp TLSReg
;
208 PPCOperand(KindTy K
) : MCParsedAsmOperand(), Kind(K
) {}
210 PPCOperand(const PPCOperand
&o
) : MCParsedAsmOperand() {
212 StartLoc
= o
.StartLoc
;
220 case ContextImmediate
:
232 // Disable use of sized deallocation due to overallocation of PPCOperand
233 // objects in CreateTokenWithStringCopy.
234 void operator delete(void *p
) { ::operator delete(p
); }
236 /// getStartLoc - Get the location of the first token of this operand.
237 SMLoc
getStartLoc() const override
{ return StartLoc
; }
239 /// getEndLoc - Get the location of the last token of this operand.
240 SMLoc
getEndLoc() const override
{ return EndLoc
; }
242 /// getLocRange - Get the range between the first and last token of this
244 SMRange
getLocRange() const { return SMRange(StartLoc
, EndLoc
); }
246 /// isPPC64 - True if this operand is for an instruction in 64-bit mode.
247 bool isPPC64() const { return IsPPC64
; }
249 int64_t getImm() const {
250 assert(Kind
== Immediate
&& "Invalid access!");
253 int64_t getImmS16Context() const {
254 assert((Kind
== Immediate
|| Kind
== ContextImmediate
) &&
256 if (Kind
== Immediate
)
258 return static_cast<int16_t>(Imm
.Val
);
260 int64_t getImmU16Context() const {
261 assert((Kind
== Immediate
|| Kind
== ContextImmediate
) &&
266 const MCExpr
*getExpr() const {
267 assert(Kind
== Expression
&& "Invalid access!");
271 int64_t getExprCRVal() const {
272 assert(Kind
== Expression
&& "Invalid access!");
276 const MCExpr
*getTLSReg() const {
277 assert(Kind
== TLSRegister
&& "Invalid access!");
281 unsigned getReg() const override
{
282 assert(isRegNumber() && "Invalid access!");
283 return (unsigned) Imm
.Val
;
286 unsigned getVSReg() const {
287 assert(isVSRegNumber() && "Invalid access!");
288 return (unsigned) Imm
.Val
;
291 unsigned getCCReg() const {
292 assert(isCCRegNumber() && "Invalid access!");
293 return (unsigned) (Kind
== Immediate
? Imm
.Val
: Expr
.CRVal
);
296 unsigned getCRBit() const {
297 assert(isCRBitNumber() && "Invalid access!");
298 return (unsigned) (Kind
== Immediate
? Imm
.Val
: Expr
.CRVal
);
301 unsigned getCRBitMask() const {
302 assert(isCRBitMask() && "Invalid access!");
303 return 7 - countTrailingZeros
<uint64_t>(Imm
.Val
);
306 bool isToken() const override
{ return Kind
== Token
; }
307 bool isImm() const override
{
308 return Kind
== Immediate
|| Kind
== Expression
;
310 bool isU1Imm() const { return Kind
== Immediate
&& isUInt
<1>(getImm()); }
311 bool isU2Imm() const { return Kind
== Immediate
&& isUInt
<2>(getImm()); }
312 bool isU3Imm() const { return Kind
== Immediate
&& isUInt
<3>(getImm()); }
313 bool isU4Imm() const { return Kind
== Immediate
&& isUInt
<4>(getImm()); }
314 bool isU5Imm() const { return Kind
== Immediate
&& isUInt
<5>(getImm()); }
315 bool isS5Imm() const { return Kind
== Immediate
&& isInt
<5>(getImm()); }
316 bool isU6Imm() const { return Kind
== Immediate
&& isUInt
<6>(getImm()); }
317 bool isU6ImmX2() const { return Kind
== Immediate
&&
318 isUInt
<6>(getImm()) &&
319 (getImm() & 1) == 0; }
320 bool isU7Imm() const { return Kind
== Immediate
&& isUInt
<7>(getImm()); }
321 bool isU7ImmX4() const { return Kind
== Immediate
&&
322 isUInt
<7>(getImm()) &&
323 (getImm() & 3) == 0; }
324 bool isU8Imm() const { return Kind
== Immediate
&& isUInt
<8>(getImm()); }
325 bool isU8ImmX8() const { return Kind
== Immediate
&&
326 isUInt
<8>(getImm()) &&
327 (getImm() & 7) == 0; }
329 bool isU10Imm() const { return Kind
== Immediate
&& isUInt
<10>(getImm()); }
330 bool isU12Imm() const { return Kind
== Immediate
&& isUInt
<12>(getImm()); }
331 bool isU16Imm() const {
336 case ContextImmediate
:
337 return isUInt
<16>(getImmU16Context());
342 bool isS16Imm() const {
347 case ContextImmediate
:
348 return isInt
<16>(getImmS16Context());
353 bool isS16ImmX4() const { return Kind
== Expression
||
354 (Kind
== Immediate
&& isInt
<16>(getImm()) &&
355 (getImm() & 3) == 0); }
356 bool isS16ImmX16() const { return Kind
== Expression
||
357 (Kind
== Immediate
&& isInt
<16>(getImm()) &&
358 (getImm() & 15) == 0); }
359 bool isS17Imm() const {
364 case ContextImmediate
:
365 return isInt
<17>(getImmS16Context());
370 bool isTLSReg() const { return Kind
== TLSRegister
; }
371 bool isDirectBr() const {
372 if (Kind
== Expression
)
374 if (Kind
!= Immediate
)
376 // Operand must be 64-bit aligned, signed 27-bit immediate.
377 if ((getImm() & 3) != 0)
379 if (isInt
<26>(getImm()))
382 // In 32-bit mode, large 32-bit quantities wrap around.
383 if (isUInt
<32>(getImm()) && isInt
<26>(static_cast<int32_t>(getImm())))
388 bool isCondBr() const { return Kind
== Expression
||
389 (Kind
== Immediate
&& isInt
<16>(getImm()) &&
390 (getImm() & 3) == 0); }
391 bool isRegNumber() const { return Kind
== Immediate
&& isUInt
<5>(getImm()); }
392 bool isVSRegNumber() const {
393 return Kind
== Immediate
&& isUInt
<6>(getImm());
395 bool isCCRegNumber() const { return (Kind
== Expression
396 && isUInt
<3>(getExprCRVal())) ||
398 && isUInt
<3>(getImm())); }
399 bool isCRBitNumber() const { return (Kind
== Expression
400 && isUInt
<5>(getExprCRVal())) ||
402 && isUInt
<5>(getImm())); }
403 bool isCRBitMask() const { return Kind
== Immediate
&& isUInt
<8>(getImm()) &&
404 isPowerOf2_32(getImm()); }
405 bool isATBitsAsHint() const { return false; }
406 bool isMem() const override
{ return false; }
407 bool isReg() const override
{ return false; }
409 void addRegOperands(MCInst
&Inst
, unsigned N
) const {
410 llvm_unreachable("addRegOperands");
413 void addRegGPRCOperands(MCInst
&Inst
, unsigned N
) const {
414 assert(N
== 1 && "Invalid number of operands!");
415 Inst
.addOperand(MCOperand::createReg(RRegs
[getReg()]));
418 void addRegGPRCNoR0Operands(MCInst
&Inst
, unsigned N
) const {
419 assert(N
== 1 && "Invalid number of operands!");
420 Inst
.addOperand(MCOperand::createReg(RRegsNoR0
[getReg()]));
423 void addRegG8RCOperands(MCInst
&Inst
, unsigned N
) const {
424 assert(N
== 1 && "Invalid number of operands!");
425 Inst
.addOperand(MCOperand::createReg(XRegs
[getReg()]));
428 void addRegG8RCNoX0Operands(MCInst
&Inst
, unsigned N
) const {
429 assert(N
== 1 && "Invalid number of operands!");
430 Inst
.addOperand(MCOperand::createReg(XRegsNoX0
[getReg()]));
433 void addRegGxRCOperands(MCInst
&Inst
, unsigned N
) const {
435 addRegG8RCOperands(Inst
, N
);
437 addRegGPRCOperands(Inst
, N
);
440 void addRegGxRCNoR0Operands(MCInst
&Inst
, unsigned N
) const {
442 addRegG8RCNoX0Operands(Inst
, N
);
444 addRegGPRCNoR0Operands(Inst
, N
);
447 void addRegF4RCOperands(MCInst
&Inst
, unsigned N
) const {
448 assert(N
== 1 && "Invalid number of operands!");
449 Inst
.addOperand(MCOperand::createReg(FRegs
[getReg()]));
452 void addRegF8RCOperands(MCInst
&Inst
, unsigned N
) const {
453 assert(N
== 1 && "Invalid number of operands!");
454 Inst
.addOperand(MCOperand::createReg(FRegs
[getReg()]));
457 void addRegVFRCOperands(MCInst
&Inst
, unsigned N
) const {
458 assert(N
== 1 && "Invalid number of operands!");
459 Inst
.addOperand(MCOperand::createReg(VFRegs
[getReg()]));
462 void addRegVRRCOperands(MCInst
&Inst
, unsigned N
) const {
463 assert(N
== 1 && "Invalid number of operands!");
464 Inst
.addOperand(MCOperand::createReg(VRegs
[getReg()]));
467 void addRegVSRCOperands(MCInst
&Inst
, unsigned N
) const {
468 assert(N
== 1 && "Invalid number of operands!");
469 Inst
.addOperand(MCOperand::createReg(VSRegs
[getVSReg()]));
472 void addRegVSFRCOperands(MCInst
&Inst
, unsigned N
) const {
473 assert(N
== 1 && "Invalid number of operands!");
474 Inst
.addOperand(MCOperand::createReg(VSFRegs
[getVSReg()]));
477 void addRegVSSRCOperands(MCInst
&Inst
, unsigned N
) const {
478 assert(N
== 1 && "Invalid number of operands!");
479 Inst
.addOperand(MCOperand::createReg(VSSRegs
[getVSReg()]));
482 void addRegQFRCOperands(MCInst
&Inst
, unsigned N
) const {
483 assert(N
== 1 && "Invalid number of operands!");
484 Inst
.addOperand(MCOperand::createReg(QFRegs
[getReg()]));
487 void addRegQSRCOperands(MCInst
&Inst
, unsigned N
) const {
488 assert(N
== 1 && "Invalid number of operands!");
489 Inst
.addOperand(MCOperand::createReg(QFRegs
[getReg()]));
492 void addRegQBRCOperands(MCInst
&Inst
, unsigned N
) const {
493 assert(N
== 1 && "Invalid number of operands!");
494 Inst
.addOperand(MCOperand::createReg(QFRegs
[getReg()]));
497 void addRegSPE4RCOperands(MCInst
&Inst
, unsigned N
) const {
498 assert(N
== 1 && "Invalid number of operands!");
499 Inst
.addOperand(MCOperand::createReg(RRegs
[getReg()]));
502 void addRegSPERCOperands(MCInst
&Inst
, unsigned N
) const {
503 assert(N
== 1 && "Invalid number of operands!");
504 Inst
.addOperand(MCOperand::createReg(SPERegs
[getReg()]));
507 void addRegCRBITRCOperands(MCInst
&Inst
, unsigned N
) const {
508 assert(N
== 1 && "Invalid number of operands!");
509 Inst
.addOperand(MCOperand::createReg(CRBITRegs
[getCRBit()]));
512 void addRegCRRCOperands(MCInst
&Inst
, unsigned N
) const {
513 assert(N
== 1 && "Invalid number of operands!");
514 Inst
.addOperand(MCOperand::createReg(CRRegs
[getCCReg()]));
517 void addCRBitMaskOperands(MCInst
&Inst
, unsigned N
) const {
518 assert(N
== 1 && "Invalid number of operands!");
519 Inst
.addOperand(MCOperand::createReg(CRRegs
[getCRBitMask()]));
522 void addImmOperands(MCInst
&Inst
, unsigned N
) const {
523 assert(N
== 1 && "Invalid number of operands!");
524 if (Kind
== Immediate
)
525 Inst
.addOperand(MCOperand::createImm(getImm()));
527 Inst
.addOperand(MCOperand::createExpr(getExpr()));
530 void addS16ImmOperands(MCInst
&Inst
, unsigned N
) const {
531 assert(N
== 1 && "Invalid number of operands!");
534 Inst
.addOperand(MCOperand::createImm(getImm()));
536 case ContextImmediate
:
537 Inst
.addOperand(MCOperand::createImm(getImmS16Context()));
540 Inst
.addOperand(MCOperand::createExpr(getExpr()));
545 void addU16ImmOperands(MCInst
&Inst
, unsigned N
) const {
546 assert(N
== 1 && "Invalid number of operands!");
549 Inst
.addOperand(MCOperand::createImm(getImm()));
551 case ContextImmediate
:
552 Inst
.addOperand(MCOperand::createImm(getImmU16Context()));
555 Inst
.addOperand(MCOperand::createExpr(getExpr()));
560 void addBranchTargetOperands(MCInst
&Inst
, unsigned N
) const {
561 assert(N
== 1 && "Invalid number of operands!");
562 if (Kind
== Immediate
)
563 Inst
.addOperand(MCOperand::createImm(getImm() / 4));
565 Inst
.addOperand(MCOperand::createExpr(getExpr()));
568 void addTLSRegOperands(MCInst
&Inst
, unsigned N
) const {
569 assert(N
== 1 && "Invalid number of operands!");
570 Inst
.addOperand(MCOperand::createExpr(getTLSReg()));
573 StringRef
getToken() const {
574 assert(Kind
== Token
&& "Invalid access!");
575 return StringRef(Tok
.Data
, Tok
.Length
);
578 void print(raw_ostream
&OS
) const override
;
580 static std::unique_ptr
<PPCOperand
> CreateToken(StringRef Str
, SMLoc S
,
582 auto Op
= std::make_unique
<PPCOperand
>(Token
);
583 Op
->Tok
.Data
= Str
.data();
584 Op
->Tok
.Length
= Str
.size();
587 Op
->IsPPC64
= IsPPC64
;
591 static std::unique_ptr
<PPCOperand
>
592 CreateTokenWithStringCopy(StringRef Str
, SMLoc S
, bool IsPPC64
) {
593 // Allocate extra memory for the string and copy it.
594 // FIXME: This is incorrect, Operands are owned by unique_ptr with a default
595 // deleter which will destroy them by simply using "delete", not correctly
596 // calling operator delete on this extra memory after calling the dtor
598 void *Mem
= ::operator new(sizeof(PPCOperand
) + Str
.size());
599 std::unique_ptr
<PPCOperand
> Op(new (Mem
) PPCOperand(Token
));
600 Op
->Tok
.Data
= reinterpret_cast<const char *>(Op
.get() + 1);
601 Op
->Tok
.Length
= Str
.size();
602 std::memcpy(const_cast<char *>(Op
->Tok
.Data
), Str
.data(), Str
.size());
605 Op
->IsPPC64
= IsPPC64
;
609 static std::unique_ptr
<PPCOperand
> CreateImm(int64_t Val
, SMLoc S
, SMLoc E
,
611 auto Op
= std::make_unique
<PPCOperand
>(Immediate
);
615 Op
->IsPPC64
= IsPPC64
;
619 static std::unique_ptr
<PPCOperand
> CreateExpr(const MCExpr
*Val
, SMLoc S
,
620 SMLoc E
, bool IsPPC64
) {
621 auto Op
= std::make_unique
<PPCOperand
>(Expression
);
623 Op
->Expr
.CRVal
= EvaluateCRExpr(Val
);
626 Op
->IsPPC64
= IsPPC64
;
630 static std::unique_ptr
<PPCOperand
>
631 CreateTLSReg(const MCSymbolRefExpr
*Sym
, SMLoc S
, SMLoc E
, bool IsPPC64
) {
632 auto Op
= std::make_unique
<PPCOperand
>(TLSRegister
);
633 Op
->TLSReg
.Sym
= Sym
;
636 Op
->IsPPC64
= IsPPC64
;
640 static std::unique_ptr
<PPCOperand
>
641 CreateContextImm(int64_t Val
, SMLoc S
, SMLoc E
, bool IsPPC64
) {
642 auto Op
= std::make_unique
<PPCOperand
>(ContextImmediate
);
646 Op
->IsPPC64
= IsPPC64
;
650 static std::unique_ptr
<PPCOperand
>
651 CreateFromMCExpr(const MCExpr
*Val
, SMLoc S
, SMLoc E
, bool IsPPC64
) {
652 if (const MCConstantExpr
*CE
= dyn_cast
<MCConstantExpr
>(Val
))
653 return CreateImm(CE
->getValue(), S
, E
, IsPPC64
);
655 if (const MCSymbolRefExpr
*SRE
= dyn_cast
<MCSymbolRefExpr
>(Val
))
656 if (SRE
->getKind() == MCSymbolRefExpr::VK_PPC_TLS
)
657 return CreateTLSReg(SRE
, S
, E
, IsPPC64
);
659 if (const PPCMCExpr
*TE
= dyn_cast
<PPCMCExpr
>(Val
)) {
661 if (TE
->evaluateAsConstant(Res
))
662 return CreateContextImm(Res
, S
, E
, IsPPC64
);
665 return CreateExpr(Val
, S
, E
, IsPPC64
);
669 } // end anonymous namespace.
671 void PPCOperand::print(raw_ostream
&OS
) const {
674 OS
<< "'" << getToken() << "'";
677 case ContextImmediate
:
690 addNegOperand(MCInst
&Inst
, MCOperand
&Op
, MCContext
&Ctx
) {
692 Inst
.addOperand(MCOperand::createImm(-Op
.getImm()));
695 const MCExpr
*Expr
= Op
.getExpr();
696 if (const MCUnaryExpr
*UnExpr
= dyn_cast
<MCUnaryExpr
>(Expr
)) {
697 if (UnExpr
->getOpcode() == MCUnaryExpr::Minus
) {
698 Inst
.addOperand(MCOperand::createExpr(UnExpr
->getSubExpr()));
701 } else if (const MCBinaryExpr
*BinExpr
= dyn_cast
<MCBinaryExpr
>(Expr
)) {
702 if (BinExpr
->getOpcode() == MCBinaryExpr::Sub
) {
703 const MCExpr
*NE
= MCBinaryExpr::createSub(BinExpr
->getRHS(),
704 BinExpr
->getLHS(), Ctx
);
705 Inst
.addOperand(MCOperand::createExpr(NE
));
709 Inst
.addOperand(MCOperand::createExpr(MCUnaryExpr::createMinus(Expr
, Ctx
)));
712 void PPCAsmParser::ProcessInstruction(MCInst
&Inst
,
713 const OperandVector
&Operands
) {
714 int Opcode
= Inst
.getOpcode();
721 TmpInst
.setOpcode((Opcode
== PPC::DCBTx
|| Opcode
== PPC::DCBTT
) ?
722 PPC::DCBT
: PPC::DCBTST
);
723 TmpInst
.addOperand(MCOperand::createImm(
724 (Opcode
== PPC::DCBTx
|| Opcode
== PPC::DCBTSTx
) ? 0 : 16));
725 TmpInst
.addOperand(Inst
.getOperand(0));
726 TmpInst
.addOperand(Inst
.getOperand(1));
733 TmpInst
.setOpcode(PPC::DCBT
);
734 TmpInst
.addOperand(Inst
.getOperand(2));
735 TmpInst
.addOperand(Inst
.getOperand(0));
736 TmpInst
.addOperand(Inst
.getOperand(1));
741 case PPC::DCBTSTDS
: {
743 TmpInst
.setOpcode(PPC::DCBTST
);
744 TmpInst
.addOperand(Inst
.getOperand(2));
745 TmpInst
.addOperand(Inst
.getOperand(0));
746 TmpInst
.addOperand(Inst
.getOperand(1));
754 if (Opcode
== PPC::DCBFL
)
756 else if (Opcode
== PPC::DCBFLP
)
760 TmpInst
.setOpcode(PPC::DCBF
);
761 TmpInst
.addOperand(MCOperand::createImm(L
));
762 TmpInst
.addOperand(Inst
.getOperand(0));
763 TmpInst
.addOperand(Inst
.getOperand(1));
769 TmpInst
.setOpcode(PPC::LA
);
770 TmpInst
.addOperand(Inst
.getOperand(0));
771 TmpInst
.addOperand(Inst
.getOperand(2));
772 TmpInst
.addOperand(Inst
.getOperand(1));
778 TmpInst
.setOpcode(PPC::ADDI
);
779 TmpInst
.addOperand(Inst
.getOperand(0));
780 TmpInst
.addOperand(Inst
.getOperand(1));
781 addNegOperand(TmpInst
, Inst
.getOperand(2), getContext());
787 TmpInst
.setOpcode(PPC::ADDIS
);
788 TmpInst
.addOperand(Inst
.getOperand(0));
789 TmpInst
.addOperand(Inst
.getOperand(1));
790 addNegOperand(TmpInst
, Inst
.getOperand(2), getContext());
796 TmpInst
.setOpcode(PPC::ADDIC
);
797 TmpInst
.addOperand(Inst
.getOperand(0));
798 TmpInst
.addOperand(Inst
.getOperand(1));
799 addNegOperand(TmpInst
, Inst
.getOperand(2), getContext());
805 TmpInst
.setOpcode(PPC::ADDICo
);
806 TmpInst
.addOperand(Inst
.getOperand(0));
807 TmpInst
.addOperand(Inst
.getOperand(1));
808 addNegOperand(TmpInst
, Inst
.getOperand(2), getContext());
815 int64_t N
= Inst
.getOperand(2).getImm();
816 int64_t B
= Inst
.getOperand(3).getImm();
817 TmpInst
.setOpcode(Opcode
== PPC::EXTLWI
? PPC::RLWINM
: PPC::RLWINMo
);
818 TmpInst
.addOperand(Inst
.getOperand(0));
819 TmpInst
.addOperand(Inst
.getOperand(1));
820 TmpInst
.addOperand(MCOperand::createImm(B
));
821 TmpInst
.addOperand(MCOperand::createImm(0));
822 TmpInst
.addOperand(MCOperand::createImm(N
- 1));
829 int64_t N
= Inst
.getOperand(2).getImm();
830 int64_t B
= Inst
.getOperand(3).getImm();
831 TmpInst
.setOpcode(Opcode
== PPC::EXTRWI
? PPC::RLWINM
: PPC::RLWINMo
);
832 TmpInst
.addOperand(Inst
.getOperand(0));
833 TmpInst
.addOperand(Inst
.getOperand(1));
834 TmpInst
.addOperand(MCOperand::createImm(B
+ N
));
835 TmpInst
.addOperand(MCOperand::createImm(32 - N
));
836 TmpInst
.addOperand(MCOperand::createImm(31));
843 int64_t N
= Inst
.getOperand(2).getImm();
844 int64_t B
= Inst
.getOperand(3).getImm();
845 TmpInst
.setOpcode(Opcode
== PPC::INSLWI
? PPC::RLWIMI
: PPC::RLWIMIo
);
846 TmpInst
.addOperand(Inst
.getOperand(0));
847 TmpInst
.addOperand(Inst
.getOperand(0));
848 TmpInst
.addOperand(Inst
.getOperand(1));
849 TmpInst
.addOperand(MCOperand::createImm(32 - B
));
850 TmpInst
.addOperand(MCOperand::createImm(B
));
851 TmpInst
.addOperand(MCOperand::createImm((B
+ N
) - 1));
858 int64_t N
= Inst
.getOperand(2).getImm();
859 int64_t B
= Inst
.getOperand(3).getImm();
860 TmpInst
.setOpcode(Opcode
== PPC::INSRWI
? PPC::RLWIMI
: PPC::RLWIMIo
);
861 TmpInst
.addOperand(Inst
.getOperand(0));
862 TmpInst
.addOperand(Inst
.getOperand(0));
863 TmpInst
.addOperand(Inst
.getOperand(1));
864 TmpInst
.addOperand(MCOperand::createImm(32 - (B
+ N
)));
865 TmpInst
.addOperand(MCOperand::createImm(B
));
866 TmpInst
.addOperand(MCOperand::createImm((B
+ N
) - 1));
873 int64_t N
= Inst
.getOperand(2).getImm();
874 TmpInst
.setOpcode(Opcode
== PPC::ROTRWI
? PPC::RLWINM
: PPC::RLWINMo
);
875 TmpInst
.addOperand(Inst
.getOperand(0));
876 TmpInst
.addOperand(Inst
.getOperand(1));
877 TmpInst
.addOperand(MCOperand::createImm(32 - N
));
878 TmpInst
.addOperand(MCOperand::createImm(0));
879 TmpInst
.addOperand(MCOperand::createImm(31));
886 int64_t N
= Inst
.getOperand(2).getImm();
887 TmpInst
.setOpcode(Opcode
== PPC::SLWI
? PPC::RLWINM
: PPC::RLWINMo
);
888 TmpInst
.addOperand(Inst
.getOperand(0));
889 TmpInst
.addOperand(Inst
.getOperand(1));
890 TmpInst
.addOperand(MCOperand::createImm(N
));
891 TmpInst
.addOperand(MCOperand::createImm(0));
892 TmpInst
.addOperand(MCOperand::createImm(31 - N
));
899 int64_t N
= Inst
.getOperand(2).getImm();
900 TmpInst
.setOpcode(Opcode
== PPC::SRWI
? PPC::RLWINM
: PPC::RLWINMo
);
901 TmpInst
.addOperand(Inst
.getOperand(0));
902 TmpInst
.addOperand(Inst
.getOperand(1));
903 TmpInst
.addOperand(MCOperand::createImm(32 - N
));
904 TmpInst
.addOperand(MCOperand::createImm(N
));
905 TmpInst
.addOperand(MCOperand::createImm(31));
912 int64_t N
= Inst
.getOperand(2).getImm();
913 TmpInst
.setOpcode(Opcode
== PPC::CLRRWI
? PPC::RLWINM
: PPC::RLWINMo
);
914 TmpInst
.addOperand(Inst
.getOperand(0));
915 TmpInst
.addOperand(Inst
.getOperand(1));
916 TmpInst
.addOperand(MCOperand::createImm(0));
917 TmpInst
.addOperand(MCOperand::createImm(0));
918 TmpInst
.addOperand(MCOperand::createImm(31 - N
));
923 case PPC::CLRLSLWIo
: {
925 int64_t B
= Inst
.getOperand(2).getImm();
926 int64_t N
= Inst
.getOperand(3).getImm();
927 TmpInst
.setOpcode(Opcode
== PPC::CLRLSLWI
? PPC::RLWINM
: PPC::RLWINMo
);
928 TmpInst
.addOperand(Inst
.getOperand(0));
929 TmpInst
.addOperand(Inst
.getOperand(1));
930 TmpInst
.addOperand(MCOperand::createImm(N
));
931 TmpInst
.addOperand(MCOperand::createImm(B
- N
));
932 TmpInst
.addOperand(MCOperand::createImm(31 - N
));
939 int64_t N
= Inst
.getOperand(2).getImm();
940 int64_t B
= Inst
.getOperand(3).getImm();
941 TmpInst
.setOpcode(Opcode
== PPC::EXTLDI
? PPC::RLDICR
: PPC::RLDICRo
);
942 TmpInst
.addOperand(Inst
.getOperand(0));
943 TmpInst
.addOperand(Inst
.getOperand(1));
944 TmpInst
.addOperand(MCOperand::createImm(B
));
945 TmpInst
.addOperand(MCOperand::createImm(N
- 1));
952 int64_t N
= Inst
.getOperand(2).getImm();
953 int64_t B
= Inst
.getOperand(3).getImm();
954 TmpInst
.setOpcode(Opcode
== PPC::EXTRDI
? PPC::RLDICL
: PPC::RLDICLo
);
955 TmpInst
.addOperand(Inst
.getOperand(0));
956 TmpInst
.addOperand(Inst
.getOperand(1));
957 TmpInst
.addOperand(MCOperand::createImm(B
+ N
));
958 TmpInst
.addOperand(MCOperand::createImm(64 - N
));
965 int64_t N
= Inst
.getOperand(2).getImm();
966 int64_t B
= Inst
.getOperand(3).getImm();
967 TmpInst
.setOpcode(Opcode
== PPC::INSRDI
? PPC::RLDIMI
: PPC::RLDIMIo
);
968 TmpInst
.addOperand(Inst
.getOperand(0));
969 TmpInst
.addOperand(Inst
.getOperand(0));
970 TmpInst
.addOperand(Inst
.getOperand(1));
971 TmpInst
.addOperand(MCOperand::createImm(64 - (B
+ N
)));
972 TmpInst
.addOperand(MCOperand::createImm(B
));
979 int64_t N
= Inst
.getOperand(2).getImm();
980 TmpInst
.setOpcode(Opcode
== PPC::ROTRDI
? PPC::RLDICL
: PPC::RLDICLo
);
981 TmpInst
.addOperand(Inst
.getOperand(0));
982 TmpInst
.addOperand(Inst
.getOperand(1));
983 TmpInst
.addOperand(MCOperand::createImm(64 - N
));
984 TmpInst
.addOperand(MCOperand::createImm(0));
991 int64_t N
= Inst
.getOperand(2).getImm();
992 TmpInst
.setOpcode(Opcode
== PPC::SLDI
? PPC::RLDICR
: PPC::RLDICRo
);
993 TmpInst
.addOperand(Inst
.getOperand(0));
994 TmpInst
.addOperand(Inst
.getOperand(1));
995 TmpInst
.addOperand(MCOperand::createImm(N
));
996 TmpInst
.addOperand(MCOperand::createImm(63 - N
));
1000 case PPC::SUBPCIS
: {
1002 int64_t N
= Inst
.getOperand(1).getImm();
1003 TmpInst
.setOpcode(PPC::ADDPCIS
);
1004 TmpInst
.addOperand(Inst
.getOperand(0));
1005 TmpInst
.addOperand(MCOperand::createImm(-N
));
1012 int64_t N
= Inst
.getOperand(2).getImm();
1013 TmpInst
.setOpcode(Opcode
== PPC::SRDI
? PPC::RLDICL
: PPC::RLDICLo
);
1014 TmpInst
.addOperand(Inst
.getOperand(0));
1015 TmpInst
.addOperand(Inst
.getOperand(1));
1016 TmpInst
.addOperand(MCOperand::createImm(64 - N
));
1017 TmpInst
.addOperand(MCOperand::createImm(N
));
1022 case PPC::CLRRDIo
: {
1024 int64_t N
= Inst
.getOperand(2).getImm();
1025 TmpInst
.setOpcode(Opcode
== PPC::CLRRDI
? PPC::RLDICR
: PPC::RLDICRo
);
1026 TmpInst
.addOperand(Inst
.getOperand(0));
1027 TmpInst
.addOperand(Inst
.getOperand(1));
1028 TmpInst
.addOperand(MCOperand::createImm(0));
1029 TmpInst
.addOperand(MCOperand::createImm(63 - N
));
1034 case PPC::CLRLSLDIo
: {
1036 int64_t B
= Inst
.getOperand(2).getImm();
1037 int64_t N
= Inst
.getOperand(3).getImm();
1038 TmpInst
.setOpcode(Opcode
== PPC::CLRLSLDI
? PPC::RLDIC
: PPC::RLDICo
);
1039 TmpInst
.addOperand(Inst
.getOperand(0));
1040 TmpInst
.addOperand(Inst
.getOperand(1));
1041 TmpInst
.addOperand(MCOperand::createImm(N
));
1042 TmpInst
.addOperand(MCOperand::createImm(B
- N
));
1047 case PPC::RLWINMobm
: {
1049 int64_t BM
= Inst
.getOperand(3).getImm();
1050 if (!isRunOfOnes(BM
, MB
, ME
))
1054 TmpInst
.setOpcode(Opcode
== PPC::RLWINMbm
? PPC::RLWINM
: PPC::RLWINMo
);
1055 TmpInst
.addOperand(Inst
.getOperand(0));
1056 TmpInst
.addOperand(Inst
.getOperand(1));
1057 TmpInst
.addOperand(Inst
.getOperand(2));
1058 TmpInst
.addOperand(MCOperand::createImm(MB
));
1059 TmpInst
.addOperand(MCOperand::createImm(ME
));
1064 case PPC::RLWIMIobm
: {
1066 int64_t BM
= Inst
.getOperand(3).getImm();
1067 if (!isRunOfOnes(BM
, MB
, ME
))
1071 TmpInst
.setOpcode(Opcode
== PPC::RLWIMIbm
? PPC::RLWIMI
: PPC::RLWIMIo
);
1072 TmpInst
.addOperand(Inst
.getOperand(0));
1073 TmpInst
.addOperand(Inst
.getOperand(0)); // The tied operand.
1074 TmpInst
.addOperand(Inst
.getOperand(1));
1075 TmpInst
.addOperand(Inst
.getOperand(2));
1076 TmpInst
.addOperand(MCOperand::createImm(MB
));
1077 TmpInst
.addOperand(MCOperand::createImm(ME
));
1082 case PPC::RLWNMobm
: {
1084 int64_t BM
= Inst
.getOperand(3).getImm();
1085 if (!isRunOfOnes(BM
, MB
, ME
))
1089 TmpInst
.setOpcode(Opcode
== PPC::RLWNMbm
? PPC::RLWNM
: PPC::RLWNMo
);
1090 TmpInst
.addOperand(Inst
.getOperand(0));
1091 TmpInst
.addOperand(Inst
.getOperand(1));
1092 TmpInst
.addOperand(Inst
.getOperand(2));
1093 TmpInst
.addOperand(MCOperand::createImm(MB
));
1094 TmpInst
.addOperand(MCOperand::createImm(ME
));
1099 if (getSTI().getFeatureBits()[PPC::FeatureMFTB
]) {
1100 assert(Inst
.getNumOperands() == 2 && "Expecting two operands");
1101 Inst
.setOpcode(PPC::MFSPR
);
1106 case PPC::CP_COPY_FIRST
: {
1108 TmpInst
.setOpcode(PPC::CP_COPY
);
1109 TmpInst
.addOperand(Inst
.getOperand(0));
1110 TmpInst
.addOperand(Inst
.getOperand(1));
1111 TmpInst
.addOperand(MCOperand::createImm(Opcode
== PPC::CP_COPYx
? 0 : 1));
1116 case PPC::CP_PASTEx
:
1117 case PPC::CP_PASTE_LAST
: {
1119 TmpInst
.setOpcode(Opcode
== PPC::CP_PASTEx
?
1120 PPC::CP_PASTE
: PPC::CP_PASTEo
);
1121 TmpInst
.addOperand(Inst
.getOperand(0));
1122 TmpInst
.addOperand(Inst
.getOperand(1));
1123 TmpInst
.addOperand(MCOperand::createImm(Opcode
== PPC::CP_PASTEx
? 0 : 1));
1131 static std::string
PPCMnemonicSpellCheck(StringRef S
, const FeatureBitset
&FBS
,
1132 unsigned VariantID
= 0);
1134 bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc
, unsigned &Opcode
,
1135 OperandVector
&Operands
,
1136 MCStreamer
&Out
, uint64_t &ErrorInfo
,
1137 bool MatchingInlineAsm
) {
1140 switch (MatchInstructionImpl(Operands
, Inst
, ErrorInfo
, MatchingInlineAsm
)) {
1142 // Post-process instructions (typically extended mnemonics)
1143 ProcessInstruction(Inst
, Operands
);
1145 Out
.EmitInstruction(Inst
, getSTI());
1147 case Match_MissingFeature
:
1148 return Error(IDLoc
, "instruction use requires an option to be enabled");
1149 case Match_MnemonicFail
: {
1150 FeatureBitset FBS
= ComputeAvailableFeatures(getSTI().getFeatureBits());
1151 std::string Suggestion
= PPCMnemonicSpellCheck(
1152 ((PPCOperand
&)*Operands
[0]).getToken(), FBS
);
1153 return Error(IDLoc
, "invalid instruction" + Suggestion
,
1154 ((PPCOperand
&)*Operands
[0]).getLocRange());
1156 case Match_InvalidOperand
: {
1157 SMLoc ErrorLoc
= IDLoc
;
1158 if (ErrorInfo
!= ~0ULL) {
1159 if (ErrorInfo
>= Operands
.size())
1160 return Error(IDLoc
, "too few operands for instruction");
1162 ErrorLoc
= ((PPCOperand
&)*Operands
[ErrorInfo
]).getStartLoc();
1163 if (ErrorLoc
== SMLoc()) ErrorLoc
= IDLoc
;
1166 return Error(ErrorLoc
, "invalid operand for instruction");
1170 llvm_unreachable("Implement any new match types added!");
1173 bool PPCAsmParser::MatchRegisterName(unsigned &RegNo
, int64_t &IntVal
) {
1174 if (getParser().getTok().is(AsmToken::Identifier
)) {
1175 StringRef Name
= getParser().getTok().getString();
1176 if (Name
.equals_lower("lr")) {
1177 RegNo
= isPPC64()? PPC::LR8
: PPC::LR
;
1179 } else if (Name
.equals_lower("ctr")) {
1180 RegNo
= isPPC64()? PPC::CTR8
: PPC::CTR
;
1182 } else if (Name
.equals_lower("vrsave")) {
1183 RegNo
= PPC::VRSAVE
;
1185 } else if (Name
.startswith_lower("r") &&
1186 !Name
.substr(1).getAsInteger(10, IntVal
) && IntVal
< 32) {
1187 RegNo
= isPPC64()? XRegs
[IntVal
] : RRegs
[IntVal
];
1188 } else if (Name
.startswith_lower("f") &&
1189 !Name
.substr(1).getAsInteger(10, IntVal
) && IntVal
< 32) {
1190 RegNo
= FRegs
[IntVal
];
1191 } else if (Name
.startswith_lower("vs") &&
1192 !Name
.substr(2).getAsInteger(10, IntVal
) && IntVal
< 64) {
1193 RegNo
= VSRegs
[IntVal
];
1194 } else if (Name
.startswith_lower("v") &&
1195 !Name
.substr(1).getAsInteger(10, IntVal
) && IntVal
< 32) {
1196 RegNo
= VRegs
[IntVal
];
1197 } else if (Name
.startswith_lower("q") &&
1198 !Name
.substr(1).getAsInteger(10, IntVal
) && IntVal
< 32) {
1199 RegNo
= QFRegs
[IntVal
];
1200 } else if (Name
.startswith_lower("cr") &&
1201 !Name
.substr(2).getAsInteger(10, IntVal
) && IntVal
< 8) {
1202 RegNo
= CRRegs
[IntVal
];
1212 ParseRegister(unsigned &RegNo
, SMLoc
&StartLoc
, SMLoc
&EndLoc
) {
1213 const AsmToken
&Tok
= getParser().getTok();
1214 StartLoc
= Tok
.getLoc();
1215 EndLoc
= Tok
.getEndLoc();
1218 if (MatchRegisterName(RegNo
, IntVal
))
1219 return TokError("invalid register name");
1223 /// Extract \code @l/@ha \endcode modifier from expression. Recursively scan
1224 /// the expression and check for VK_PPC_LO/HI/HA
1225 /// symbol variants. If all symbols with modifier use the same
1226 /// variant, return the corresponding PPCMCExpr::VariantKind,
1227 /// and a modified expression using the default symbol variant.
1228 /// Otherwise, return NULL.
1229 const MCExpr
*PPCAsmParser::
1230 ExtractModifierFromExpr(const MCExpr
*E
,
1231 PPCMCExpr::VariantKind
&Variant
) {
1232 MCContext
&Context
= getParser().getContext();
1233 Variant
= PPCMCExpr::VK_PPC_None
;
1235 switch (E
->getKind()) {
1236 case MCExpr::Target
:
1237 case MCExpr::Constant
:
1240 case MCExpr::SymbolRef
: {
1241 const MCSymbolRefExpr
*SRE
= cast
<MCSymbolRefExpr
>(E
);
1243 switch (SRE
->getKind()) {
1244 case MCSymbolRefExpr::VK_PPC_LO
:
1245 Variant
= PPCMCExpr::VK_PPC_LO
;
1247 case MCSymbolRefExpr::VK_PPC_HI
:
1248 Variant
= PPCMCExpr::VK_PPC_HI
;
1250 case MCSymbolRefExpr::VK_PPC_HA
:
1251 Variant
= PPCMCExpr::VK_PPC_HA
;
1253 case MCSymbolRefExpr::VK_PPC_HIGH
:
1254 Variant
= PPCMCExpr::VK_PPC_HIGH
;
1256 case MCSymbolRefExpr::VK_PPC_HIGHA
:
1257 Variant
= PPCMCExpr::VK_PPC_HIGHA
;
1259 case MCSymbolRefExpr::VK_PPC_HIGHER
:
1260 Variant
= PPCMCExpr::VK_PPC_HIGHER
;
1262 case MCSymbolRefExpr::VK_PPC_HIGHERA
:
1263 Variant
= PPCMCExpr::VK_PPC_HIGHERA
;
1265 case MCSymbolRefExpr::VK_PPC_HIGHEST
:
1266 Variant
= PPCMCExpr::VK_PPC_HIGHEST
;
1268 case MCSymbolRefExpr::VK_PPC_HIGHESTA
:
1269 Variant
= PPCMCExpr::VK_PPC_HIGHESTA
;
1275 return MCSymbolRefExpr::create(&SRE
->getSymbol(), Context
);
1278 case MCExpr::Unary
: {
1279 const MCUnaryExpr
*UE
= cast
<MCUnaryExpr
>(E
);
1280 const MCExpr
*Sub
= ExtractModifierFromExpr(UE
->getSubExpr(), Variant
);
1283 return MCUnaryExpr::create(UE
->getOpcode(), Sub
, Context
);
1286 case MCExpr::Binary
: {
1287 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(E
);
1288 PPCMCExpr::VariantKind LHSVariant
, RHSVariant
;
1289 const MCExpr
*LHS
= ExtractModifierFromExpr(BE
->getLHS(), LHSVariant
);
1290 const MCExpr
*RHS
= ExtractModifierFromExpr(BE
->getRHS(), RHSVariant
);
1295 if (!LHS
) LHS
= BE
->getLHS();
1296 if (!RHS
) RHS
= BE
->getRHS();
1298 if (LHSVariant
== PPCMCExpr::VK_PPC_None
)
1299 Variant
= RHSVariant
;
1300 else if (RHSVariant
== PPCMCExpr::VK_PPC_None
)
1301 Variant
= LHSVariant
;
1302 else if (LHSVariant
== RHSVariant
)
1303 Variant
= LHSVariant
;
1307 return MCBinaryExpr::create(BE
->getOpcode(), LHS
, RHS
, Context
);
1311 llvm_unreachable("Invalid expression kind!");
1314 /// Find all VK_TLSGD/VK_TLSLD symbol references in expression and replace
1315 /// them by VK_PPC_TLSGD/VK_PPC_TLSLD. This is necessary to avoid having
1316 /// _GLOBAL_OFFSET_TABLE_ created via ELFObjectWriter::RelocNeedsGOT.
1317 /// FIXME: This is a hack.
1318 const MCExpr
*PPCAsmParser::
1319 FixupVariantKind(const MCExpr
*E
) {
1320 MCContext
&Context
= getParser().getContext();
1322 switch (E
->getKind()) {
1323 case MCExpr::Target
:
1324 case MCExpr::Constant
:
1327 case MCExpr::SymbolRef
: {
1328 const MCSymbolRefExpr
*SRE
= cast
<MCSymbolRefExpr
>(E
);
1329 MCSymbolRefExpr::VariantKind Variant
= MCSymbolRefExpr::VK_None
;
1331 switch (SRE
->getKind()) {
1332 case MCSymbolRefExpr::VK_TLSGD
:
1333 Variant
= MCSymbolRefExpr::VK_PPC_TLSGD
;
1335 case MCSymbolRefExpr::VK_TLSLD
:
1336 Variant
= MCSymbolRefExpr::VK_PPC_TLSLD
;
1341 return MCSymbolRefExpr::create(&SRE
->getSymbol(), Variant
, Context
);
1344 case MCExpr::Unary
: {
1345 const MCUnaryExpr
*UE
= cast
<MCUnaryExpr
>(E
);
1346 const MCExpr
*Sub
= FixupVariantKind(UE
->getSubExpr());
1347 if (Sub
== UE
->getSubExpr())
1349 return MCUnaryExpr::create(UE
->getOpcode(), Sub
, Context
);
1352 case MCExpr::Binary
: {
1353 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(E
);
1354 const MCExpr
*LHS
= FixupVariantKind(BE
->getLHS());
1355 const MCExpr
*RHS
= FixupVariantKind(BE
->getRHS());
1356 if (LHS
== BE
->getLHS() && RHS
== BE
->getRHS())
1358 return MCBinaryExpr::create(BE
->getOpcode(), LHS
, RHS
, Context
);
1362 llvm_unreachable("Invalid expression kind!");
1365 /// ParseExpression. This differs from the default "parseExpression" in that
1366 /// it handles modifiers.
1368 ParseExpression(const MCExpr
*&EVal
) {
1371 return ParseDarwinExpression(EVal
);
1374 // Handle \code @l/@ha \endcode
1375 if (getParser().parseExpression(EVal
))
1378 EVal
= FixupVariantKind(EVal
);
1380 PPCMCExpr::VariantKind Variant
;
1381 const MCExpr
*E
= ExtractModifierFromExpr(EVal
, Variant
);
1383 EVal
= PPCMCExpr::create(Variant
, E
, false, getParser().getContext());
1388 /// ParseDarwinExpression. (MachO Platforms)
1389 /// This differs from the default "parseExpression" in that it handles detection
1390 /// of the \code hi16(), ha16() and lo16() \endcode modifiers. At present,
1391 /// parseExpression() doesn't recognise the modifiers when in the Darwin/MachO
1392 /// syntax form so it is done here. TODO: Determine if there is merit in
1393 /// arranging for this to be done at a higher level.
1395 ParseDarwinExpression(const MCExpr
*&EVal
) {
1396 MCAsmParser
&Parser
= getParser();
1397 PPCMCExpr::VariantKind Variant
= PPCMCExpr::VK_PPC_None
;
1398 switch (getLexer().getKind()) {
1401 case AsmToken::Identifier
:
1402 // Compiler-generated Darwin identifiers begin with L,l,_ or "; thus
1403 // something starting with any other char should be part of the
1404 // asm syntax. If handwritten asm includes an identifier like lo16,
1405 // then all bets are off - but no-one would do that, right?
1406 StringRef poss
= Parser
.getTok().getString();
1407 if (poss
.equals_lower("lo16")) {
1408 Variant
= PPCMCExpr::VK_PPC_LO
;
1409 } else if (poss
.equals_lower("hi16")) {
1410 Variant
= PPCMCExpr::VK_PPC_HI
;
1411 } else if (poss
.equals_lower("ha16")) {
1412 Variant
= PPCMCExpr::VK_PPC_HA
;
1414 if (Variant
!= PPCMCExpr::VK_PPC_None
) {
1415 Parser
.Lex(); // Eat the xx16
1416 if (getLexer().isNot(AsmToken::LParen
))
1417 return Error(Parser
.getTok().getLoc(), "expected '('");
1418 Parser
.Lex(); // Eat the '('
1423 if (getParser().parseExpression(EVal
))
1426 if (Variant
!= PPCMCExpr::VK_PPC_None
) {
1427 if (getLexer().isNot(AsmToken::RParen
))
1428 return Error(Parser
.getTok().getLoc(), "expected ')'");
1429 Parser
.Lex(); // Eat the ')'
1430 EVal
= PPCMCExpr::create(Variant
, EVal
, false, getParser().getContext());
1436 /// This handles registers in the form 'NN', '%rNN' for ELF platforms and
1438 bool PPCAsmParser::ParseOperand(OperandVector
&Operands
) {
1439 MCAsmParser
&Parser
= getParser();
1440 SMLoc S
= Parser
.getTok().getLoc();
1441 SMLoc E
= SMLoc::getFromPointer(Parser
.getTok().getLoc().getPointer() - 1);
1444 // Attempt to parse the next token as an immediate
1445 switch (getLexer().getKind()) {
1446 // Special handling for register names. These are interpreted
1447 // as immediates corresponding to the register number.
1448 case AsmToken::Percent
:
1449 Parser
.Lex(); // Eat the '%'.
1452 if (MatchRegisterName(RegNo
, IntVal
))
1453 return Error(S
, "invalid register name");
1455 Operands
.push_back(PPCOperand::CreateImm(IntVal
, S
, E
, isPPC64()));
1458 case AsmToken::Identifier
:
1459 case AsmToken::LParen
:
1460 case AsmToken::Plus
:
1461 case AsmToken::Minus
:
1462 case AsmToken::Integer
:
1464 case AsmToken::Dollar
:
1465 case AsmToken::Exclaim
:
1466 case AsmToken::Tilde
:
1467 // Note that non-register-name identifiers from the compiler will begin
1468 // with '_', 'L'/'l' or '"'. Of course, handwritten asm could include
1469 // identifiers like r31foo - so we fall through in the event that parsing
1470 // a register name fails.
1474 if (!MatchRegisterName(RegNo
, IntVal
)) {
1475 Operands
.push_back(PPCOperand::CreateImm(IntVal
, S
, E
, isPPC64()));
1479 // All other expressions
1481 if (!ParseExpression(EVal
))
1486 return Error(S
, "unknown operand");
1489 // Push the parsed operand into the list of operands
1490 Operands
.push_back(PPCOperand::CreateFromMCExpr(EVal
, S
, E
, isPPC64()));
1492 // Check whether this is a TLS call expression
1493 bool TLSCall
= false;
1494 if (const MCSymbolRefExpr
*Ref
= dyn_cast
<MCSymbolRefExpr
>(EVal
))
1495 TLSCall
= Ref
->getSymbol().getName() == "__tls_get_addr";
1497 if (TLSCall
&& getLexer().is(AsmToken::LParen
)) {
1498 const MCExpr
*TLSSym
;
1500 Parser
.Lex(); // Eat the '('.
1501 S
= Parser
.getTok().getLoc();
1502 if (ParseExpression(TLSSym
))
1503 return Error(S
, "invalid TLS call expression");
1504 if (getLexer().isNot(AsmToken::RParen
))
1505 return Error(Parser
.getTok().getLoc(), "missing ')'");
1506 E
= Parser
.getTok().getLoc();
1507 Parser
.Lex(); // Eat the ')'.
1509 Operands
.push_back(PPCOperand::CreateFromMCExpr(TLSSym
, S
, E
, isPPC64()));
1512 // Otherwise, check for D-form memory operands
1513 if (!TLSCall
&& getLexer().is(AsmToken::LParen
)) {
1514 Parser
.Lex(); // Eat the '('.
1515 S
= Parser
.getTok().getLoc();
1518 switch (getLexer().getKind()) {
1519 case AsmToken::Percent
:
1520 Parser
.Lex(); // Eat the '%'.
1522 if (MatchRegisterName(RegNo
, IntVal
))
1523 return Error(S
, "invalid register name");
1526 case AsmToken::Integer
:
1528 return Error(S
, "unexpected integer value");
1529 else if (getParser().parseAbsoluteExpression(IntVal
) || IntVal
< 0 ||
1531 return Error(S
, "invalid register number");
1533 case AsmToken::Identifier
:
1536 if (!MatchRegisterName(RegNo
, IntVal
)) {
1543 return Error(S
, "invalid memory operand");
1546 E
= Parser
.getTok().getLoc();
1547 if (parseToken(AsmToken::RParen
, "missing ')'"))
1549 Operands
.push_back(PPCOperand::CreateImm(IntVal
, S
, E
, isPPC64()));
1555 /// Parse an instruction mnemonic followed by its operands.
1556 bool PPCAsmParser::ParseInstruction(ParseInstructionInfo
&Info
, StringRef Name
,
1557 SMLoc NameLoc
, OperandVector
&Operands
) {
1558 // The first operand is the token for the instruction name.
1559 // If the next character is a '+' or '-', we need to add it to the
1560 // instruction name, to match what TableGen is doing.
1561 std::string NewOpcode
;
1562 if (parseOptionalToken(AsmToken::Plus
)) {
1567 if (parseOptionalToken(AsmToken::Minus
)) {
1572 // If the instruction ends in a '.', we need to create a separate
1573 // token for it, to match what TableGen is doing.
1574 size_t Dot
= Name
.find('.');
1575 StringRef Mnemonic
= Name
.slice(0, Dot
);
1576 if (!NewOpcode
.empty()) // Underlying memory for Name is volatile.
1578 PPCOperand::CreateTokenWithStringCopy(Mnemonic
, NameLoc
, isPPC64()));
1580 Operands
.push_back(PPCOperand::CreateToken(Mnemonic
, NameLoc
, isPPC64()));
1581 if (Dot
!= StringRef::npos
) {
1582 SMLoc DotLoc
= SMLoc::getFromPointer(NameLoc
.getPointer() + Dot
);
1583 StringRef DotStr
= Name
.slice(Dot
, StringRef::npos
);
1584 if (!NewOpcode
.empty()) // Underlying memory for Name is volatile.
1586 PPCOperand::CreateTokenWithStringCopy(DotStr
, DotLoc
, isPPC64()));
1588 Operands
.push_back(PPCOperand::CreateToken(DotStr
, DotLoc
, isPPC64()));
1591 // If there are no more operands then finish
1592 if (parseOptionalToken(AsmToken::EndOfStatement
))
1595 // Parse the first operand
1596 if (ParseOperand(Operands
))
1599 while (!parseOptionalToken(AsmToken::EndOfStatement
)) {
1600 if (parseToken(AsmToken::Comma
) || ParseOperand(Operands
))
1604 // We'll now deal with an unfortunate special case: the syntax for the dcbt
1605 // and dcbtst instructions differs for server vs. embedded cores.
1606 // The syntax for dcbt is:
1607 // dcbt ra, rb, th [server]
1608 // dcbt th, ra, rb [embedded]
1609 // where th can be omitted when it is 0. dcbtst is the same. We take the
1610 // server form to be the default, so swap the operands if we're parsing for
1611 // an embedded core (they'll be swapped again upon printing).
1612 if (getSTI().getFeatureBits()[PPC::FeatureBookE
] &&
1613 Operands
.size() == 4 &&
1614 (Name
== "dcbt" || Name
== "dcbtst")) {
1615 std::swap(Operands
[1], Operands
[3]);
1616 std::swap(Operands
[2], Operands
[1]);
1622 /// ParseDirective parses the PPC specific directives
1623 bool PPCAsmParser::ParseDirective(AsmToken DirectiveID
) {
1624 StringRef IDVal
= DirectiveID
.getIdentifier();
1626 if (IDVal
== ".machine")
1627 ParseDarwinDirectiveMachine(DirectiveID
.getLoc());
1630 } else if (IDVal
== ".word")
1631 ParseDirectiveWord(2, DirectiveID
);
1632 else if (IDVal
== ".llong")
1633 ParseDirectiveWord(8, DirectiveID
);
1634 else if (IDVal
== ".tc")
1635 ParseDirectiveTC(isPPC64() ? 8 : 4, DirectiveID
);
1636 else if (IDVal
== ".machine")
1637 ParseDirectiveMachine(DirectiveID
.getLoc());
1638 else if (IDVal
== ".abiversion")
1639 ParseDirectiveAbiVersion(DirectiveID
.getLoc());
1640 else if (IDVal
== ".localentry")
1641 ParseDirectiveLocalEntry(DirectiveID
.getLoc());
1647 /// ParseDirectiveWord
1648 /// ::= .word [ expression (, expression)* ]
1649 bool PPCAsmParser::ParseDirectiveWord(unsigned Size
, AsmToken ID
) {
1650 auto parseOp
= [&]() -> bool {
1651 const MCExpr
*Value
;
1652 SMLoc ExprLoc
= getParser().getTok().getLoc();
1653 if (getParser().parseExpression(Value
))
1655 if (const auto *MCE
= dyn_cast
<MCConstantExpr
>(Value
)) {
1656 assert(Size
<= 8 && "Invalid size");
1657 uint64_t IntValue
= MCE
->getValue();
1658 if (!isUIntN(8 * Size
, IntValue
) && !isIntN(8 * Size
, IntValue
))
1659 return Error(ExprLoc
, "literal value out of range for '" +
1660 ID
.getIdentifier() + "' directive");
1661 getStreamer().EmitIntValue(IntValue
, Size
);
1663 getStreamer().EmitValue(Value
, Size
, ExprLoc
);
1667 if (parseMany(parseOp
))
1668 return addErrorSuffix(" in '" + ID
.getIdentifier() + "' directive");
1672 /// ParseDirectiveTC
1673 /// ::= .tc [ symbol (, expression)* ]
1674 bool PPCAsmParser::ParseDirectiveTC(unsigned Size
, AsmToken ID
) {
1675 MCAsmParser
&Parser
= getParser();
1676 // Skip TC symbol, which is only used with XCOFF.
1677 while (getLexer().isNot(AsmToken::EndOfStatement
)
1678 && getLexer().isNot(AsmToken::Comma
))
1680 if (parseToken(AsmToken::Comma
))
1681 return addErrorSuffix(" in '.tc' directive");
1683 // Align to word size.
1684 getParser().getStreamer().EmitValueToAlignment(Size
);
1686 // Emit expressions.
1687 return ParseDirectiveWord(Size
, ID
);
1690 /// ParseDirectiveMachine (ELF platforms)
1691 /// ::= .machine [ cpu | "push" | "pop" ]
1692 bool PPCAsmParser::ParseDirectiveMachine(SMLoc L
) {
1693 MCAsmParser
&Parser
= getParser();
1694 if (Parser
.getTok().isNot(AsmToken::Identifier
) &&
1695 Parser
.getTok().isNot(AsmToken::String
))
1696 return Error(L
, "unexpected token in '.machine' directive");
1698 StringRef CPU
= Parser
.getTok().getIdentifier();
1700 // FIXME: Right now, the parser always allows any available
1701 // instruction, so the .machine directive is not useful.
1702 // Implement ".machine any" (by doing nothing) for the benefit
1703 // of existing assembler code. Likewise, we can then implement
1704 // ".machine push" and ".machine pop" as no-op.
1705 if (CPU
!= "any" && CPU
!= "push" && CPU
!= "pop")
1706 return TokError("unrecognized machine type");
1710 if (parseToken(AsmToken::EndOfStatement
))
1711 return addErrorSuffix(" in '.machine' directive");
1713 PPCTargetStreamer
&TStreamer
=
1714 *static_cast<PPCTargetStreamer
*>(
1715 getParser().getStreamer().getTargetStreamer());
1716 TStreamer
.emitMachine(CPU
);
1721 /// ParseDarwinDirectiveMachine (Mach-o platforms)
1722 /// ::= .machine cpu-identifier
1723 bool PPCAsmParser::ParseDarwinDirectiveMachine(SMLoc L
) {
1724 MCAsmParser
&Parser
= getParser();
1725 if (Parser
.getTok().isNot(AsmToken::Identifier
) &&
1726 Parser
.getTok().isNot(AsmToken::String
))
1727 return Error(L
, "unexpected token in directive");
1729 StringRef CPU
= Parser
.getTok().getIdentifier();
1732 // FIXME: this is only the 'default' set of cpu variants.
1733 // However we don't act on this information at present, this is simply
1734 // allowing parsing to proceed with minimal sanity checking.
1735 if (check(CPU
!= "ppc7400" && CPU
!= "ppc" && CPU
!= "ppc64", L
,
1736 "unrecognized cpu type") ||
1737 check(isPPC64() && (CPU
== "ppc7400" || CPU
== "ppc"), L
,
1738 "wrong cpu type specified for 64bit") ||
1739 check(!isPPC64() && CPU
== "ppc64", L
,
1740 "wrong cpu type specified for 32bit") ||
1741 parseToken(AsmToken::EndOfStatement
))
1742 return addErrorSuffix(" in '.machine' directive");
1746 /// ParseDirectiveAbiVersion
1747 /// ::= .abiversion constant-expression
1748 bool PPCAsmParser::ParseDirectiveAbiVersion(SMLoc L
) {
1750 if (check(getParser().parseAbsoluteExpression(AbiVersion
), L
,
1751 "expected constant expression") ||
1752 parseToken(AsmToken::EndOfStatement
))
1753 return addErrorSuffix(" in '.abiversion' directive");
1755 PPCTargetStreamer
&TStreamer
=
1756 *static_cast<PPCTargetStreamer
*>(
1757 getParser().getStreamer().getTargetStreamer());
1758 TStreamer
.emitAbiVersion(AbiVersion
);
1763 /// ParseDirectiveLocalEntry
1764 /// ::= .localentry symbol, expression
1765 bool PPCAsmParser::ParseDirectiveLocalEntry(SMLoc L
) {
1767 if (getParser().parseIdentifier(Name
))
1768 return Error(L
, "expected identifier in '.localentry' directive");
1770 MCSymbolELF
*Sym
= cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(Name
));
1773 if (parseToken(AsmToken::Comma
) ||
1774 check(getParser().parseExpression(Expr
), L
, "expected expression") ||
1775 parseToken(AsmToken::EndOfStatement
))
1776 return addErrorSuffix(" in '.localentry' directive");
1778 PPCTargetStreamer
&TStreamer
=
1779 *static_cast<PPCTargetStreamer
*>(
1780 getParser().getStreamer().getTargetStreamer());
1781 TStreamer
.emitLocalEntry(Sym
, Expr
);
1788 /// Force static initialization.
1789 extern "C" void LLVMInitializePowerPCAsmParser() {
1790 RegisterMCAsmParser
<PPCAsmParser
> A(getThePPC32Target());
1791 RegisterMCAsmParser
<PPCAsmParser
> B(getThePPC64Target());
1792 RegisterMCAsmParser
<PPCAsmParser
> C(getThePPC64LETarget());
1795 #define GET_REGISTER_MATCHER
1796 #define GET_MATCHER_IMPLEMENTATION
1797 #define GET_MNEMONIC_SPELL_CHECKER
1798 #include "PPCGenAsmMatcher.inc"
1800 // Define this matcher function after the auto-generated include so we
1801 // have the match class enum definitions.
1802 unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand
&AsmOp
,
1804 // If the kind is a token for a literal immediate, check if our asm
1805 // operand matches. This is for InstAliases which have a fixed-value
1806 // immediate in the syntax.
1809 case MCK_0
: ImmVal
= 0; break;
1810 case MCK_1
: ImmVal
= 1; break;
1811 case MCK_2
: ImmVal
= 2; break;
1812 case MCK_3
: ImmVal
= 3; break;
1813 case MCK_4
: ImmVal
= 4; break;
1814 case MCK_5
: ImmVal
= 5; break;
1815 case MCK_6
: ImmVal
= 6; break;
1816 case MCK_7
: ImmVal
= 7; break;
1817 default: return Match_InvalidOperand
;
1820 PPCOperand
&Op
= static_cast<PPCOperand
&>(AsmOp
);
1821 if (Op
.isImm() && Op
.getImm() == ImmVal
)
1822 return Match_Success
;
1824 return Match_InvalidOperand
;
1828 PPCAsmParser::applyModifierToExpr(const MCExpr
*E
,
1829 MCSymbolRefExpr::VariantKind Variant
,
1832 case MCSymbolRefExpr::VK_PPC_LO
:
1833 return PPCMCExpr::create(PPCMCExpr::VK_PPC_LO
, E
, false, Ctx
);
1834 case MCSymbolRefExpr::VK_PPC_HI
:
1835 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HI
, E
, false, Ctx
);
1836 case MCSymbolRefExpr::VK_PPC_HA
:
1837 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HA
, E
, false, Ctx
);
1838 case MCSymbolRefExpr::VK_PPC_HIGH
:
1839 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGH
, E
, false, Ctx
);
1840 case MCSymbolRefExpr::VK_PPC_HIGHA
:
1841 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHA
, E
, false, Ctx
);
1842 case MCSymbolRefExpr::VK_PPC_HIGHER
:
1843 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHER
, E
, false, Ctx
);
1844 case MCSymbolRefExpr::VK_PPC_HIGHERA
:
1845 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHERA
, E
, false, Ctx
);
1846 case MCSymbolRefExpr::VK_PPC_HIGHEST
:
1847 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHEST
, E
, false, Ctx
);
1848 case MCSymbolRefExpr::VK_PPC_HIGHESTA
:
1849 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHESTA
, E
, false, Ctx
);