LLVM API Documentation

PPCMCCodeEmitter.cpp
Go to the documentation of this file.
00001 //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the PPCMCCodeEmitter class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MCTargetDesc/PPCMCTargetDesc.h"
00015 #include "MCTargetDesc/PPCFixupKinds.h"
00016 #include "llvm/ADT/Statistic.h"
00017 #include "llvm/MC/MCCodeEmitter.h"
00018 #include "llvm/MC/MCContext.h"
00019 #include "llvm/MC/MCExpr.h"
00020 #include "llvm/MC/MCInst.h"
00021 #include "llvm/MC/MCInstrInfo.h"
00022 #include "llvm/MC/MCSubtargetInfo.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 #include "llvm/Target/TargetOpcodes.h"
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "mccodeemitter"
00029 
00030 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
00031 
00032 namespace {
00033 class PPCMCCodeEmitter : public MCCodeEmitter {
00034   PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
00035   void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
00036 
00037   const MCInstrInfo &MCII;
00038   const MCContext &CTX;
00039   bool IsLittleEndian;
00040 
00041 public:
00042   PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx, bool isLittle)
00043     : MCII(mcii), CTX(ctx), IsLittleEndian(isLittle) {
00044   }
00045   
00046   ~PPCMCCodeEmitter() {}
00047 
00048   unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
00049                                SmallVectorImpl<MCFixup> &Fixups,
00050                                const MCSubtargetInfo &STI) const;
00051   unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
00052                              SmallVectorImpl<MCFixup> &Fixups,
00053                              const MCSubtargetInfo &STI) const;
00054   unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
00055                                   SmallVectorImpl<MCFixup> &Fixups,
00056                                   const MCSubtargetInfo &STI) const;
00057   unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
00058                                 SmallVectorImpl<MCFixup> &Fixups,
00059                                 const MCSubtargetInfo &STI) const;
00060   unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
00061                              SmallVectorImpl<MCFixup> &Fixups,
00062                              const MCSubtargetInfo &STI) const;
00063   unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
00064                             SmallVectorImpl<MCFixup> &Fixups,
00065                             const MCSubtargetInfo &STI) const;
00066   unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
00067                              SmallVectorImpl<MCFixup> &Fixups,
00068                              const MCSubtargetInfo &STI) const;
00069   unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
00070                               SmallVectorImpl<MCFixup> &Fixups,
00071                               const MCSubtargetInfo &STI) const;
00072   unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
00073                               SmallVectorImpl<MCFixup> &Fixups,
00074                               const MCSubtargetInfo &STI) const;
00075   unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
00076                               SmallVectorImpl<MCFixup> &Fixups,
00077                               const MCSubtargetInfo &STI) const;
00078   unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
00079                              SmallVectorImpl<MCFixup> &Fixups,
00080                              const MCSubtargetInfo &STI) const;
00081   unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
00082                               SmallVectorImpl<MCFixup> &Fixups,
00083                               const MCSubtargetInfo &STI) const;
00084   unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
00085                                SmallVectorImpl<MCFixup> &Fixups,
00086                                const MCSubtargetInfo &STI) const;
00087 
00088   /// getMachineOpValue - Return binary encoding of operand. If the machine
00089   /// operand requires relocation, record the relocation and return zero.
00090   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
00091                              SmallVectorImpl<MCFixup> &Fixups,
00092                              const MCSubtargetInfo &STI) const;
00093   
00094   // getBinaryCodeForInstr - TableGen'erated function for getting the
00095   // binary encoding for an instruction.
00096   uint64_t getBinaryCodeForInstr(const MCInst &MI,
00097                                  SmallVectorImpl<MCFixup> &Fixups,
00098                                  const MCSubtargetInfo &STI) const;
00099   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
00100                          SmallVectorImpl<MCFixup> &Fixups,
00101                          const MCSubtargetInfo &STI) const override {
00102     // For fast-isel, a float COPY_TO_REGCLASS can survive this long.
00103     // It's just a nop to keep the register classes happy, so don't
00104     // generate anything.
00105     unsigned Opcode = MI.getOpcode();
00106     const MCInstrDesc &Desc = MCII.get(Opcode);
00107     if (Opcode == TargetOpcode::COPY_TO_REGCLASS)
00108       return;
00109 
00110     uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
00111 
00112     // Output the constant in big/little endian byte order.
00113     unsigned Size = Desc.getSize();
00114     switch (Size) {
00115     case 4:
00116       if (IsLittleEndian) {
00117         OS << (char)(Bits);
00118         OS << (char)(Bits >> 8);
00119         OS << (char)(Bits >> 16);
00120         OS << (char)(Bits >> 24);
00121       } else {
00122         OS << (char)(Bits >> 24);
00123         OS << (char)(Bits >> 16);
00124         OS << (char)(Bits >> 8);
00125         OS << (char)(Bits);
00126       }
00127       break;
00128     case 8:
00129       // If we emit a pair of instructions, the first one is
00130       // always in the top 32 bits, even on little-endian.
00131       if (IsLittleEndian) {
00132         OS << (char)(Bits >> 32);
00133         OS << (char)(Bits >> 40);
00134         OS << (char)(Bits >> 48);
00135         OS << (char)(Bits >> 56);
00136         OS << (char)(Bits);
00137         OS << (char)(Bits >> 8);
00138         OS << (char)(Bits >> 16);
00139         OS << (char)(Bits >> 24);
00140       } else {
00141         OS << (char)(Bits >> 56);
00142         OS << (char)(Bits >> 48);
00143         OS << (char)(Bits >> 40);
00144         OS << (char)(Bits >> 32);
00145         OS << (char)(Bits >> 24);
00146         OS << (char)(Bits >> 16);
00147         OS << (char)(Bits >> 8);
00148         OS << (char)(Bits);
00149       }
00150       break;
00151     default:
00152       llvm_unreachable ("Invalid instruction size");
00153     }
00154     
00155     ++MCNumEmitted;  // Keep track of the # of mi's emitted.
00156   }
00157   
00158 };
00159   
00160 } // end anonymous namespace
00161   
00162 MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
00163                                             const MCRegisterInfo &MRI,
00164                                             const MCSubtargetInfo &STI,
00165                                             MCContext &Ctx) {
00166   Triple TT(STI.getTargetTriple());
00167   bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
00168   return new PPCMCCodeEmitter(MCII, Ctx, IsLittleEndian);
00169 }
00170 
00171 unsigned PPCMCCodeEmitter::
00172 getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
00173                     SmallVectorImpl<MCFixup> &Fixups,
00174                     const MCSubtargetInfo &STI) const {
00175   const MCOperand &MO = MI.getOperand(OpNo);
00176   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
00177   
00178   // Add a fixup for the branch target.
00179   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
00180                                    (MCFixupKind)PPC::fixup_ppc_br24));
00181   return 0;
00182 }
00183 
00184 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
00185                                      SmallVectorImpl<MCFixup> &Fixups,
00186                                      const MCSubtargetInfo &STI) const {
00187   const MCOperand &MO = MI.getOperand(OpNo);
00188   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
00189 
00190   // Add a fixup for the branch target.
00191   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
00192                                    (MCFixupKind)PPC::fixup_ppc_brcond14));
00193   return 0;
00194 }
00195 
00196 unsigned PPCMCCodeEmitter::
00197 getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
00198                        SmallVectorImpl<MCFixup> &Fixups,
00199                        const MCSubtargetInfo &STI) const {
00200   const MCOperand &MO = MI.getOperand(OpNo);
00201   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
00202 
00203   // Add a fixup for the branch target.
00204   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
00205                                    (MCFixupKind)PPC::fixup_ppc_br24abs));
00206   return 0;
00207 }
00208 
00209 unsigned PPCMCCodeEmitter::
00210 getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
00211                      SmallVectorImpl<MCFixup> &Fixups,
00212                      const MCSubtargetInfo &STI) const {
00213   const MCOperand &MO = MI.getOperand(OpNo);
00214   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
00215 
00216   // Add a fixup for the branch target.
00217   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
00218                                    (MCFixupKind)PPC::fixup_ppc_brcond14abs));
00219   return 0;
00220 }
00221 
00222 unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
00223                                        SmallVectorImpl<MCFixup> &Fixups,
00224                                        const MCSubtargetInfo &STI) const {
00225   const MCOperand &MO = MI.getOperand(OpNo);
00226   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
00227   
00228   // Add a fixup for the immediate field.
00229   Fixups.push_back(MCFixup::Create(IsLittleEndian? 0 : 2, MO.getExpr(),
00230                                    (MCFixupKind)PPC::fixup_ppc_half16));
00231   return 0;
00232 }
00233 
00234 unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
00235                                             SmallVectorImpl<MCFixup> &Fixups,
00236                                             const MCSubtargetInfo &STI) const {
00237   // Encode (imm, reg) as a memri, which has the low 16-bits as the
00238   // displacement and the next 5 bits as the register #.
00239   assert(MI.getOperand(OpNo+1).isReg());
00240   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
00241   
00242   const MCOperand &MO = MI.getOperand(OpNo);
00243   if (MO.isImm())
00244     return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
00245   
00246   // Add a fixup for the displacement field.
00247   Fixups.push_back(MCFixup::Create(IsLittleEndian? 0 : 2, MO.getExpr(),
00248                                    (MCFixupKind)PPC::fixup_ppc_half16));
00249   return RegBits;
00250 }
00251 
00252 
00253 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
00254                                        SmallVectorImpl<MCFixup> &Fixups,
00255                                        const MCSubtargetInfo &STI) const {
00256   // Encode (imm, reg) as a memrix, which has the low 14-bits as the
00257   // displacement and the next 5 bits as the register #.
00258   assert(MI.getOperand(OpNo+1).isReg());
00259   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
00260   
00261   const MCOperand &MO = MI.getOperand(OpNo);
00262   if (MO.isImm())
00263     return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
00264   
00265   // Add a fixup for the displacement field.
00266   Fixups.push_back(MCFixup::Create(IsLittleEndian? 0 : 2, MO.getExpr(),
00267                                    (MCFixupKind)PPC::fixup_ppc_half16ds));
00268   return RegBits;
00269 }
00270 
00271 
00272 unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
00273                                               SmallVectorImpl<MCFixup> &Fixups,
00274                                               const MCSubtargetInfo &STI)
00275                                               const {
00276   // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
00277   // as the displacement and the next 5 bits as the register #.
00278   assert(MI.getOperand(OpNo+1).isReg());
00279   uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
00280 
00281   const MCOperand &MO = MI.getOperand(OpNo);
00282   assert(MO.isImm());
00283   uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
00284   return reverseBits(Imm | RegBits) >> 22;
00285 }
00286 
00287 
00288 unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
00289                                               SmallVectorImpl<MCFixup> &Fixups,
00290                                               const MCSubtargetInfo &STI)
00291                                               const {
00292   // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
00293   // as the displacement and the next 5 bits as the register #.
00294   assert(MI.getOperand(OpNo+1).isReg());
00295   uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
00296 
00297   const MCOperand &MO = MI.getOperand(OpNo);
00298   assert(MO.isImm());
00299   uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
00300   return reverseBits(Imm | RegBits) >> 22;
00301 }
00302 
00303 
00304 unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
00305                                               SmallVectorImpl<MCFixup> &Fixups,
00306                                               const MCSubtargetInfo &STI)
00307                                               const {
00308   // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
00309   // as the displacement and the next 5 bits as the register #.
00310   assert(MI.getOperand(OpNo+1).isReg());
00311   uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
00312 
00313   const MCOperand &MO = MI.getOperand(OpNo);
00314   assert(MO.isImm());
00315   uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
00316   return reverseBits(Imm | RegBits) >> 22;
00317 }
00318 
00319 
00320 unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
00321                                        SmallVectorImpl<MCFixup> &Fixups,
00322                                        const MCSubtargetInfo &STI) const {
00323   const MCOperand &MO = MI.getOperand(OpNo);
00324   if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
00325   
00326   // Add a fixup for the TLS register, which simply provides a relocation
00327   // hint to the linker that this statement is part of a relocation sequence.
00328   // Return the thread-pointer register's encoding.
00329   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
00330                                    (MCFixupKind)PPC::fixup_ppc_nofixup));
00331   Triple TT(STI.getTargetTriple());
00332   bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
00333   return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
00334 }
00335 
00336 unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
00337                                        SmallVectorImpl<MCFixup> &Fixups,
00338                                        const MCSubtargetInfo &STI) const {
00339   // For special TLS calls, we need two fixups; one for the branch target
00340   // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
00341   // and one for the TLSGD or TLSLD symbol, which is emitted here.
00342   const MCOperand &MO = MI.getOperand(OpNo+1);
00343   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
00344                                    (MCFixupKind)PPC::fixup_ppc_nofixup));
00345   return getDirectBrEncoding(MI, OpNo, Fixups, STI);
00346 }
00347 
00348 unsigned PPCMCCodeEmitter::
00349 get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
00350                     SmallVectorImpl<MCFixup> &Fixups,
00351                     const MCSubtargetInfo &STI) const {
00352   const MCOperand &MO = MI.getOperand(OpNo);
00353   assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
00354           MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
00355          (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
00356   return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
00357 }
00358 
00359 
00360 unsigned PPCMCCodeEmitter::
00361 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
00362                   SmallVectorImpl<MCFixup> &Fixups,
00363                   const MCSubtargetInfo &STI) const {
00364   if (MO.isReg()) {
00365     // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
00366     // The GPR operand should come through here though.
00367     assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
00368             MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
00369            MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
00370     return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
00371   }
00372   
00373   assert(MO.isImm() &&
00374          "Relocation required in an instruction that we cannot encode!");
00375   return MO.getImm();
00376 }
00377 
00378 
00379 #include "PPCGenMCCodeEmitter.inc"