Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 1 | //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===// |
| 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 | // This file implements the PPCMCCodeEmitter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "mccodeemitter" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
Evan Cheng | 61d4a20 | 2011-07-25 19:53:23 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/PPCFixupKinds.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCCodeEmitter.h" |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInst.h" |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSubtargetInfo.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Bill Schmidt | c763c22 | 2013-09-16 17:25:12 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOpcodes.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); |
| 30 | |
| 31 | namespace { |
| 32 | class PPCMCCodeEmitter : public MCCodeEmitter { |
Craig Topper | a60c0f1 | 2012-09-15 17:09:36 +0000 | [diff] [blame] | 33 | PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
| 34 | void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
| 35 | |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 36 | const MCInstrInfo &MCII; |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 37 | const MCContext &CTX; |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 38 | bool IsLittleEndian; |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 40 | public: |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 41 | PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx, bool isLittle) |
| 42 | : MCII(mcii), CTX(ctx), IsLittleEndian(isLittle) { |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ~PPCMCCodeEmitter() {} |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 47 | unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 48 | SmallVectorImpl<MCFixup> &Fixups, |
| 49 | const MCSubtargetInfo &STI) const; |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 50 | unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 51 | SmallVectorImpl<MCFixup> &Fixups, |
| 52 | const MCSubtargetInfo &STI) const; |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 53 | unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 54 | SmallVectorImpl<MCFixup> &Fixups, |
| 55 | const MCSubtargetInfo &STI) const; |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 56 | unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 57 | SmallVectorImpl<MCFixup> &Fixups, |
| 58 | const MCSubtargetInfo &STI) const; |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 59 | unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 60 | SmallVectorImpl<MCFixup> &Fixups, |
| 61 | const MCSubtargetInfo &STI) const; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 62 | unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 63 | SmallVectorImpl<MCFixup> &Fixups, |
| 64 | const MCSubtargetInfo &STI) const; |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 65 | unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 66 | SmallVectorImpl<MCFixup> &Fixups, |
| 67 | const MCSubtargetInfo &STI) const; |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 68 | unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 69 | SmallVectorImpl<MCFixup> &Fixups, |
| 70 | const MCSubtargetInfo &STI) const; |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 71 | unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 72 | SmallVectorImpl<MCFixup> &Fixups, |
| 73 | const MCSubtargetInfo &STI) const; |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 74 | unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 75 | SmallVectorImpl<MCFixup> &Fixups, |
| 76 | const MCSubtargetInfo &STI) const; |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 78 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 79 | /// operand requires relocation, record the relocation and return zero. |
| 80 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 81 | SmallVectorImpl<MCFixup> &Fixups, |
| 82 | const MCSubtargetInfo &STI) const; |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 83 | |
| 84 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 85 | // binary encoding for an instruction. |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 86 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 87 | SmallVectorImpl<MCFixup> &Fixups, |
| 88 | const MCSubtargetInfo &STI) const; |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 89 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 90 | SmallVectorImpl<MCFixup> &Fixups, |
| 91 | const MCSubtargetInfo &STI) const { |
Bill Schmidt | c763c22 | 2013-09-16 17:25:12 +0000 | [diff] [blame] | 92 | // For fast-isel, a float COPY_TO_REGCLASS can survive this long. |
| 93 | // It's just a nop to keep the register classes happy, so don't |
| 94 | // generate anything. |
| 95 | unsigned Opcode = MI.getOpcode(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 96 | const MCInstrDesc &Desc = MCII.get(Opcode); |
Bill Schmidt | c763c22 | 2013-09-16 17:25:12 +0000 | [diff] [blame] | 97 | if (Opcode == TargetOpcode::COPY_TO_REGCLASS) |
| 98 | return; |
| 99 | |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 100 | uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI); |
Adhemerval Zanella | 1be10dc | 2012-10-25 14:29:13 +0000 | [diff] [blame] | 101 | |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 102 | // Output the constant in big/little endian byte order. |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 103 | unsigned Size = Desc.getSize(); |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 104 | if (IsLittleEndian) { |
| 105 | for (unsigned i = 0; i != Size; ++i) { |
| 106 | OS << (char)Bits; |
| 107 | Bits >>= 8; |
| 108 | } |
| 109 | } else { |
| 110 | int ShiftValue = (Size * 8) - 8; |
| 111 | for (unsigned i = 0; i != Size; ++i) { |
| 112 | OS << (char)(Bits >> ShiftValue); |
| 113 | Bits <<= 8; |
| 114 | } |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
| 118 | } |
| 119 | |
| 120 | }; |
| 121 | |
| 122 | } // end anonymous namespace |
| 123 | |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 124 | MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII, |
Jim Grosbach | c3b0427 | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 125 | const MCRegisterInfo &MRI, |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 126 | const MCSubtargetInfo &STI, |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 127 | MCContext &Ctx) { |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 128 | Triple TT(STI.getTargetTriple()); |
| 129 | bool IsLittleEndian = TT.getArch() == Triple::ppc64le; |
| 130 | return new PPCMCCodeEmitter(MCII, Ctx, IsLittleEndian); |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 134 | getDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 135 | SmallVectorImpl<MCFixup> &Fixups, |
| 136 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 137 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 138 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 139 | |
| 140 | // Add a fixup for the branch target. |
| 141 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 142 | (MCFixupKind)PPC::fixup_ppc_br24)); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 146 | unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 147 | SmallVectorImpl<MCFixup> &Fixups, |
| 148 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 149 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 150 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 85e3768 | 2010-11-15 06:12:22 +0000 | [diff] [blame] | 152 | // Add a fixup for the branch target. |
| 153 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 154 | (MCFixupKind)PPC::fixup_ppc_brcond14)); |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 158 | unsigned PPCMCCodeEmitter:: |
| 159 | getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 160 | SmallVectorImpl<MCFixup> &Fixups, |
| 161 | const MCSubtargetInfo &STI) const { |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 162 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 163 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 164 | |
| 165 | // Add a fixup for the branch target. |
| 166 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 167 | (MCFixupKind)PPC::fixup_ppc_br24abs)); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | unsigned PPCMCCodeEmitter:: |
| 172 | getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 173 | SmallVectorImpl<MCFixup> &Fixups, |
| 174 | const MCSubtargetInfo &STI) const { |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 175 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 176 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 177 | |
| 178 | // Add a fixup for the branch target. |
| 179 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 180 | (MCFixupKind)PPC::fixup_ppc_brcond14abs)); |
| 181 | return 0; |
| 182 | } |
| 183 | |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 184 | unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 185 | SmallVectorImpl<MCFixup> &Fixups, |
| 186 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 187 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 188 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 189 | |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 190 | // Add a fixup for the immediate field. |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 191 | Fixups.push_back(MCFixup::Create(IsLittleEndian? 0 : 2, MO.getExpr(), |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 192 | (MCFixupKind)PPC::fixup_ppc_half16)); |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 196 | unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 197 | SmallVectorImpl<MCFixup> &Fixups, |
| 198 | const MCSubtargetInfo &STI) const { |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 199 | // Encode (imm, reg) as a memri, which has the low 16-bits as the |
| 200 | // displacement and the next 5 bits as the register #. |
| 201 | assert(MI.getOperand(OpNo+1).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 202 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 203 | |
| 204 | const MCOperand &MO = MI.getOperand(OpNo); |
| 205 | if (MO.isImm()) |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 206 | return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 207 | |
| 208 | // Add a fixup for the displacement field. |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 209 | Fixups.push_back(MCFixup::Create(IsLittleEndian? 0 : 2, MO.getExpr(), |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 210 | (MCFixupKind)PPC::fixup_ppc_half16)); |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 211 | return RegBits; |
| 212 | } |
| 213 | |
| 214 | |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 215 | unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 216 | SmallVectorImpl<MCFixup> &Fixups, |
| 217 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 218 | // Encode (imm, reg) as a memrix, which has the low 14-bits as the |
| 219 | // displacement and the next 5 bits as the register #. |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 220 | assert(MI.getOperand(OpNo+1).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 221 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14; |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 223 | const MCOperand &MO = MI.getOperand(OpNo); |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 224 | if (MO.isImm()) |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 225 | return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 226 | |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 227 | // Add a fixup for the displacement field. |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame^] | 228 | Fixups.push_back(MCFixup::Create(IsLittleEndian? 0 : 2, MO.getExpr(), |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 229 | (MCFixupKind)PPC::fixup_ppc_half16ds)); |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 230 | return RegBits; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 233 | |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 234 | unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 235 | SmallVectorImpl<MCFixup> &Fixups, |
| 236 | const MCSubtargetInfo &STI) const { |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 237 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 238 | if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI); |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 239 | |
| 240 | // Add a fixup for the TLS register, which simply provides a relocation |
| 241 | // hint to the linker that this statement is part of a relocation sequence. |
| 242 | // Return the thread-pointer register's encoding. |
| 243 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
Ulrich Weigand | 5b42759 | 2013-07-05 12:22:36 +0000 | [diff] [blame] | 244 | (MCFixupKind)PPC::fixup_ppc_nofixup)); |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 245 | Triple TT(STI.getTargetTriple()); |
Roman Divacky | bc1655b4 | 2013-12-22 10:45:37 +0000 | [diff] [blame] | 246 | bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le; |
| 247 | return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2); |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 250 | unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 251 | SmallVectorImpl<MCFixup> &Fixups, |
| 252 | const MCSubtargetInfo &STI) const { |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 253 | // For special TLS calls, we need two fixups; one for the branch target |
| 254 | // (__tls_get_addr), which we create via getDirectBrEncoding as usual, |
| 255 | // and one for the TLSGD or TLSLD symbol, which is emitted here. |
| 256 | const MCOperand &MO = MI.getOperand(OpNo+1); |
| 257 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 258 | (MCFixupKind)PPC::fixup_ppc_nofixup)); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 259 | return getDirectBrEncoding(MI, OpNo, Fixups, STI); |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 262 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 263 | get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 264 | SmallVectorImpl<MCFixup> &Fixups, |
| 265 | const MCSubtargetInfo &STI) const { |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 266 | const MCOperand &MO = MI.getOperand(OpNo); |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 267 | assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 || |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 268 | MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) && |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 269 | (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 270 | return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
| 274 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 275 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 276 | SmallVectorImpl<MCFixup> &Fixups, |
| 277 | const MCSubtargetInfo &STI) const { |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 278 | if (MO.isReg()) { |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 279 | // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand. |
Chris Lattner | 7b25d6f | 2010-11-16 00:57:32 +0000 | [diff] [blame] | 280 | // The GPR operand should come through here though. |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 281 | assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 && |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 282 | MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) || |
Chris Lattner | 73716a6 | 2010-11-16 00:55:51 +0000 | [diff] [blame] | 283 | MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 284 | return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | c877d8f | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 286 | |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 287 | assert(MO.isImm() && |
| 288 | "Relocation required in an instruction that we cannot encode!"); |
| 289 | return MO.getImm(); |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| 293 | #include "PPCGenMCCodeEmitter.inc" |