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