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