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 | |
Evan Cheng | 61d4a20 | 2011-07-25 19:53:23 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/PPCFixupKinds.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 15 | #include "PPCInstrInfo.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCCodeEmitter.h" |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCFixup.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInst.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrDesc.h" |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCInstrInfo.h" |
Pete Cooper | 3de83e4 | 2015-05-15 21:58:42 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCRegisterInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSubtargetInfo.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Endian.h" |
Benjamin Kramer | 50e2a29 | 2015-06-04 15:03:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/EndianStream.h" |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 33 | #include <cassert> |
| 34 | #include <cstdint> |
| 35 | |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "mccodeemitter" |
| 39 | |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 40 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); |
| 41 | |
| 42 | namespace { |
Craig Topper | a60c0f1 | 2012-09-15 17:09:36 +0000 | [diff] [blame] | 43 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 44 | class PPCMCCodeEmitter : public MCCodeEmitter { |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 45 | const MCInstrInfo &MCII; |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 46 | const MCContext &CTX; |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 47 | bool IsLittleEndian; |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 49 | public: |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 50 | PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx) |
| 51 | : MCII(mcii), CTX(ctx), |
| 52 | IsLittleEndian(ctx.getAsmInfo()->isLittleEndian()) {} |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 53 | PPCMCCodeEmitter(const PPCMCCodeEmitter &) = delete; |
| 54 | void operator=(const PPCMCCodeEmitter &) = delete; |
| 55 | ~PPCMCCodeEmitter() override = default; |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 57 | unsigned getDirectBrEncoding(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; |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 60 | unsigned getCondBrEncoding(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; |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 63 | unsigned getAbsDirectBrEncoding(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; |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 66 | unsigned getAbsCondBrEncoding(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; |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 69 | unsigned getImm16Encoding(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; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 72 | unsigned getMemRIEncoding(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 | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 75 | unsigned getMemRIXEncoding(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; |
Kit Barton | ba532dc | 2016-03-08 03:49:13 +0000 | [diff] [blame] | 78 | unsigned getMemRIX16Encoding(const MCInst &MI, unsigned OpNo, |
| 79 | SmallVectorImpl<MCFixup> &Fixups, |
| 80 | const MCSubtargetInfo &STI) const; |
Joerg Sonnenberger | 0013b92 | 2014-08-08 16:43:49 +0000 | [diff] [blame] | 81 | unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo, |
| 82 | SmallVectorImpl<MCFixup> &Fixups, |
| 83 | const MCSubtargetInfo &STI) const; |
| 84 | unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo, |
| 85 | SmallVectorImpl<MCFixup> &Fixups, |
| 86 | const MCSubtargetInfo &STI) const; |
| 87 | unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo, |
| 88 | SmallVectorImpl<MCFixup> &Fixups, |
| 89 | const MCSubtargetInfo &STI) const; |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 90 | unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 91 | SmallVectorImpl<MCFixup> &Fixups, |
| 92 | const MCSubtargetInfo &STI) const; |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 93 | unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 94 | SmallVectorImpl<MCFixup> &Fixups, |
| 95 | const MCSubtargetInfo &STI) const; |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 96 | unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 97 | SmallVectorImpl<MCFixup> &Fixups, |
| 98 | const MCSubtargetInfo &STI) const; |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 100 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 101 | /// operand requires relocation, record the relocation and return zero. |
| 102 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 103 | SmallVectorImpl<MCFixup> &Fixups, |
| 104 | const MCSubtargetInfo &STI) const; |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 105 | |
| 106 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 107 | // binary encoding for an instruction. |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 108 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 109 | SmallVectorImpl<MCFixup> &Fixups, |
| 110 | const MCSubtargetInfo &STI) const; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 111 | |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame] | 112 | void encodeInstruction(const MCInst &MI, raw_ostream &OS, |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 113 | SmallVectorImpl<MCFixup> &Fixups, |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 114 | const MCSubtargetInfo &STI) const override { |
Daniel Sanders | 72db2a3 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 115 | verifyInstructionPredicates(MI, |
| 116 | computeAvailableFeatures(STI.getFeatureBits())); |
| 117 | |
Bill Schmidt | c763c22 | 2013-09-16 17:25:12 +0000 | [diff] [blame] | 118 | unsigned Opcode = MI.getOpcode(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 119 | const MCInstrDesc &Desc = MCII.get(Opcode); |
Bill Schmidt | c763c22 | 2013-09-16 17:25:12 +0000 | [diff] [blame] | 120 | |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 121 | uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI); |
Adhemerval Zanella | 1be10dc | 2012-10-25 14:29:13 +0000 | [diff] [blame] | 122 | |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 123 | // Output the constant in big/little endian byte order. |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 124 | unsigned Size = Desc.getSize(); |
Ulrich Weigand | 7c3f0dc | 2014-06-18 15:37:07 +0000 | [diff] [blame] | 125 | switch (Size) { |
Marcin Koscielnicki | 7b32957 | 2016-04-28 21:24:37 +0000 | [diff] [blame] | 126 | case 0: |
| 127 | break; |
Ulrich Weigand | 7c3f0dc | 2014-06-18 15:37:07 +0000 | [diff] [blame] | 128 | case 4: |
| 129 | if (IsLittleEndian) { |
Benjamin Kramer | 50e2a29 | 2015-06-04 15:03:02 +0000 | [diff] [blame] | 130 | support::endian::Writer<support::little>(OS).write<uint32_t>(Bits); |
Ulrich Weigand | 7c3f0dc | 2014-06-18 15:37:07 +0000 | [diff] [blame] | 131 | } else { |
Benjamin Kramer | 50e2a29 | 2015-06-04 15:03:02 +0000 | [diff] [blame] | 132 | support::endian::Writer<support::big>(OS).write<uint32_t>(Bits); |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 133 | } |
Ulrich Weigand | 7c3f0dc | 2014-06-18 15:37:07 +0000 | [diff] [blame] | 134 | break; |
| 135 | case 8: |
| 136 | // If we emit a pair of instructions, the first one is |
| 137 | // always in the top 32 bits, even on little-endian. |
| 138 | if (IsLittleEndian) { |
Benjamin Kramer | 50e2a29 | 2015-06-04 15:03:02 +0000 | [diff] [blame] | 139 | uint64_t Swapped = (Bits << 32) | (Bits >> 32); |
| 140 | support::endian::Writer<support::little>(OS).write<uint64_t>(Swapped); |
Ulrich Weigand | 7c3f0dc | 2014-06-18 15:37:07 +0000 | [diff] [blame] | 141 | } else { |
Benjamin Kramer | 50e2a29 | 2015-06-04 15:03:02 +0000 | [diff] [blame] | 142 | support::endian::Writer<support::big>(OS).write<uint64_t>(Bits); |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 143 | } |
Ulrich Weigand | 7c3f0dc | 2014-06-18 15:37:07 +0000 | [diff] [blame] | 144 | break; |
| 145 | default: |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 146 | llvm_unreachable("Invalid instruction size"); |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
| 150 | } |
Daniel Sanders | 72db2a3 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | uint64_t computeAvailableFeatures(const FeatureBitset &FB) const; |
| 154 | void verifyInstructionPredicates(const MCInst &MI, |
| 155 | uint64_t AvailableFeatures) const; |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | } // end anonymous namespace |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 159 | |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 160 | MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII, |
Jim Grosbach | c3b0427 | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 161 | const MCRegisterInfo &MRI, |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 162 | MCContext &Ctx) { |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 163 | return new PPCMCCodeEmitter(MCII, Ctx); |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 167 | getDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 168 | SmallVectorImpl<MCFixup> &Fixups, |
| 169 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 170 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 171 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 172 | |
| 173 | // Add a fixup for the branch target. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 174 | Fixups.push_back(MCFixup::create(0, MO.getExpr(), |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 175 | (MCFixupKind)PPC::fixup_ppc_br24)); |
| 176 | return 0; |
| 177 | } |
| 178 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 179 | unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 180 | SmallVectorImpl<MCFixup> &Fixups, |
| 181 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 182 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 183 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 85e3768 | 2010-11-15 06:12:22 +0000 | [diff] [blame] | 185 | // Add a fixup for the branch target. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 186 | Fixups.push_back(MCFixup::create(0, MO.getExpr(), |
Chris Lattner | 85e3768 | 2010-11-15 06:12:22 +0000 | [diff] [blame] | 187 | (MCFixupKind)PPC::fixup_ppc_brcond14)); |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 191 | unsigned PPCMCCodeEmitter:: |
| 192 | getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 193 | SmallVectorImpl<MCFixup> &Fixups, |
| 194 | const MCSubtargetInfo &STI) const { |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 195 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 196 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 197 | |
| 198 | // Add a fixup for the branch target. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 199 | Fixups.push_back(MCFixup::create(0, MO.getExpr(), |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 200 | (MCFixupKind)PPC::fixup_ppc_br24abs)); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | unsigned PPCMCCodeEmitter:: |
| 205 | getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 206 | SmallVectorImpl<MCFixup> &Fixups, |
| 207 | const MCSubtargetInfo &STI) const { |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 208 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 209 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 210 | |
| 211 | // Add a fixup for the branch target. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 212 | Fixups.push_back(MCFixup::create(0, MO.getExpr(), |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 213 | (MCFixupKind)PPC::fixup_ppc_brcond14abs)); |
| 214 | return 0; |
| 215 | } |
| 216 | |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 217 | unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 218 | SmallVectorImpl<MCFixup> &Fixups, |
| 219 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 220 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 221 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 222 | |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 223 | // Add a fixup for the immediate field. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 224 | Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 225 | (MCFixupKind)PPC::fixup_ppc_half16)); |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 229 | unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 230 | SmallVectorImpl<MCFixup> &Fixups, |
| 231 | const MCSubtargetInfo &STI) const { |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 232 | // Encode (imm, reg) as a memri, which has the low 16-bits as the |
| 233 | // displacement and the next 5 bits as the register #. |
| 234 | assert(MI.getOperand(OpNo+1).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 235 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 236 | |
| 237 | const MCOperand &MO = MI.getOperand(OpNo); |
| 238 | if (MO.isImm()) |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 239 | return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 240 | |
| 241 | // Add a fixup for the displacement field. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 242 | Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 243 | (MCFixupKind)PPC::fixup_ppc_half16)); |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 244 | return RegBits; |
| 245 | } |
| 246 | |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 247 | unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 248 | SmallVectorImpl<MCFixup> &Fixups, |
| 249 | const MCSubtargetInfo &STI) const { |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 250 | // Encode (imm, reg) as a memrix, which has the low 14-bits as the |
| 251 | // displacement and the next 5 bits as the register #. |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 252 | assert(MI.getOperand(OpNo+1).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 253 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14; |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 255 | const MCOperand &MO = MI.getOperand(OpNo); |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 256 | if (MO.isImm()) |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 257 | return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 258 | |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 259 | // Add a fixup for the displacement field. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 260 | Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 261 | (MCFixupKind)PPC::fixup_ppc_half16ds)); |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 262 | return RegBits; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Kit Barton | ba532dc | 2016-03-08 03:49:13 +0000 | [diff] [blame] | 265 | unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo, |
| 266 | SmallVectorImpl<MCFixup> &Fixups, |
| 267 | const MCSubtargetInfo &STI) const { |
| 268 | // Encode (imm, reg) as a memrix16, which has the low 12-bits as the |
| 269 | // displacement and the next 5 bits as the register #. |
| 270 | assert(MI.getOperand(OpNo+1).isReg()); |
| 271 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12; |
| 272 | |
| 273 | const MCOperand &MO = MI.getOperand(OpNo); |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 274 | assert(MO.isImm() && !(MO.getImm() % 16) && |
| 275 | "Expecting an immediate that is a multiple of 16"); |
Kit Barton | ba532dc | 2016-03-08 03:49:13 +0000 | [diff] [blame] | 276 | |
| 277 | return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits; |
| 278 | } |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 279 | |
Joerg Sonnenberger | 0013b92 | 2014-08-08 16:43:49 +0000 | [diff] [blame] | 280 | unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo, |
| 281 | SmallVectorImpl<MCFixup> &Fixups, |
| 282 | const MCSubtargetInfo &STI) |
| 283 | const { |
| 284 | // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8) |
| 285 | // as the displacement and the next 5 bits as the register #. |
| 286 | assert(MI.getOperand(OpNo+1).isReg()); |
| 287 | uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5; |
| 288 | |
| 289 | const MCOperand &MO = MI.getOperand(OpNo); |
| 290 | assert(MO.isImm()); |
| 291 | uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3; |
| 292 | return reverseBits(Imm | RegBits) >> 22; |
| 293 | } |
| 294 | |
Joerg Sonnenberger | 0013b92 | 2014-08-08 16:43:49 +0000 | [diff] [blame] | 295 | unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo, |
| 296 | SmallVectorImpl<MCFixup> &Fixups, |
| 297 | const MCSubtargetInfo &STI) |
| 298 | const { |
| 299 | // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4) |
| 300 | // as the displacement and the next 5 bits as the register #. |
| 301 | assert(MI.getOperand(OpNo+1).isReg()); |
| 302 | uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5; |
| 303 | |
| 304 | const MCOperand &MO = MI.getOperand(OpNo); |
| 305 | assert(MO.isImm()); |
| 306 | uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2; |
| 307 | return reverseBits(Imm | RegBits) >> 22; |
| 308 | } |
| 309 | |
Joerg Sonnenberger | 0013b92 | 2014-08-08 16:43:49 +0000 | [diff] [blame] | 310 | unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo, |
| 311 | SmallVectorImpl<MCFixup> &Fixups, |
| 312 | const MCSubtargetInfo &STI) |
| 313 | const { |
| 314 | // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2) |
| 315 | // as the displacement and the next 5 bits as the register #. |
| 316 | assert(MI.getOperand(OpNo+1).isReg()); |
| 317 | uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5; |
| 318 | |
| 319 | const MCOperand &MO = MI.getOperand(OpNo); |
| 320 | assert(MO.isImm()); |
| 321 | uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1; |
| 322 | return reverseBits(Imm | RegBits) >> 22; |
| 323 | } |
| 324 | |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 325 | unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 326 | SmallVectorImpl<MCFixup> &Fixups, |
| 327 | const MCSubtargetInfo &STI) const { |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 328 | const MCOperand &MO = MI.getOperand(OpNo); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 329 | if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI); |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 330 | |
| 331 | // Add a fixup for the TLS register, which simply provides a relocation |
| 332 | // hint to the linker that this statement is part of a relocation sequence. |
| 333 | // Return the thread-pointer register's encoding. |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 334 | Fixups.push_back(MCFixup::create(0, MO.getExpr(), |
Ulrich Weigand | 5b42759 | 2013-07-05 12:22:36 +0000 | [diff] [blame] | 335 | (MCFixupKind)PPC::fixup_ppc_nofixup)); |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 336 | const Triple &TT = STI.getTargetTriple(); |
| 337 | bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le; |
Roman Divacky | bc1655b4 | 2013-12-22 10:45:37 +0000 | [diff] [blame] | 338 | return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2); |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 341 | unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 342 | SmallVectorImpl<MCFixup> &Fixups, |
| 343 | const MCSubtargetInfo &STI) const { |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 344 | // For special TLS calls, we need two fixups; one for the branch target |
| 345 | // (__tls_get_addr), which we create via getDirectBrEncoding as usual, |
| 346 | // and one for the TLSGD or TLSLD symbol, which is emitted here. |
| 347 | const MCOperand &MO = MI.getOperand(OpNo+1); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 348 | Fixups.push_back(MCFixup::create(0, MO.getExpr(), |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 349 | (MCFixupKind)PPC::fixup_ppc_nofixup)); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 350 | return getDirectBrEncoding(MI, OpNo, Fixups, STI); |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 353 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 354 | get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 355 | SmallVectorImpl<MCFixup> &Fixups, |
| 356 | const MCSubtargetInfo &STI) const { |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 357 | const MCOperand &MO = MI.getOperand(OpNo); |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 358 | assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 || |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 359 | MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) && |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 360 | (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 361 | return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 364 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 365 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 366 | SmallVectorImpl<MCFixup> &Fixups, |
| 367 | const MCSubtargetInfo &STI) const { |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 368 | if (MO.isReg()) { |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 369 | // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand. |
Chris Lattner | 7b25d6f | 2010-11-16 00:57:32 +0000 | [diff] [blame] | 370 | // The GPR operand should come through here though. |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 371 | assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 && |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 372 | MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) || |
Chris Lattner | 73716a6 | 2010-11-16 00:55:51 +0000 | [diff] [blame] | 373 | MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7); |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 374 | unsigned Reg = MO.getReg(); |
| 375 | unsigned Encode = CTX.getRegisterInfo()->getEncodingValue(Reg); |
| 376 | |
| 377 | if ((MCII.get(MI.getOpcode()).TSFlags & PPCII::UseVSXReg)) |
| 378 | if (PPCInstrInfo::isVRRegister(Reg)) |
| 379 | Encode += 32; |
| 380 | |
| 381 | return Encode; |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 382 | } |
Chris Lattner | c877d8f | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 383 | |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 384 | assert(MO.isImm() && |
| 385 | "Relocation required in an instruction that we cannot encode!"); |
| 386 | return MO.getImm(); |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Sanders | 72db2a3 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 389 | #define ENABLE_INSTR_PREDICATE_VERIFIER |
Chris Lattner | 9ec375c | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 390 | #include "PPCGenMCCodeEmitter.inc" |