Home | History | Annotate | Download | only in MCTargetDesc
      1 //===-- MBlazeMCCodeEmitter.cpp - Convert MBlaze 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 MBlazeMCCodeEmitter class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #define DEBUG_TYPE "mccodeemitter"
     15 #include "MCTargetDesc/MBlazeBaseInfo.h"
     16 #include "MCTargetDesc/MBlazeMCTargetDesc.h"
     17 #include "llvm/MC/MCCodeEmitter.h"
     18 #include "llvm/MC/MCExpr.h"
     19 #include "llvm/MC/MCInst.h"
     20 #include "llvm/MC/MCInstrInfo.h"
     21 #include "llvm/MC/MCSubtargetInfo.h"
     22 #include "llvm/MC/MCSymbol.h"
     23 #include "llvm/MC/MCFixup.h"
     24 #include "llvm/ADT/Statistic.h"
     25 #include "llvm/Support/raw_ostream.h"
     26 using namespace llvm;
     27 
     28 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
     29 
     30 namespace {
     31 class MBlazeMCCodeEmitter : public MCCodeEmitter {
     32   MBlazeMCCodeEmitter(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
     33   void operator=(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
     34   const MCInstrInfo &MCII;
     35 
     36 public:
     37   MBlazeMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
     38                       MCContext &ctx)
     39     : MCII(mcii) {
     40   }
     41 
     42   ~MBlazeMCCodeEmitter() {}
     43 
     44   // getBinaryCodeForInstr - TableGen'erated function for getting the
     45   // binary encoding for an instruction.
     46   unsigned getBinaryCodeForInstr(const MCInst &MI) const;
     47 
     48   /// getMachineOpValue - Return binary encoding of operand. If the machine
     49   /// operand requires relocation, record the relocation and return zero.
     50   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
     51   unsigned getMachineOpValue(const MCInst &MI, unsigned OpIdx) const {
     52     return getMachineOpValue(MI, MI.getOperand(OpIdx));
     53   }
     54 
     55   static unsigned GetMBlazeRegNum(const MCOperand &MO) {
     56     // FIXME: getMBlazeRegisterNumbering() is sufficient?
     57     assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
     58     return 0;
     59   }
     60 
     61   void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
     62     // The MicroBlaze uses a bit reversed format so we need to reverse the
     63     // order of the bits. Taken from:
     64     // http://graphics.stanford.edu/~seander/bithacks.html
     65     C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
     66 
     67     OS << (char)C;
     68     ++CurByte;
     69   }
     70 
     71   void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
     72     OS << (char)C;
     73     ++CurByte;
     74   }
     75 
     76   void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
     77                     raw_ostream &OS) const {
     78     assert(Size <= 8 && "size too big in emit constant");
     79 
     80     for (unsigned i = 0; i != Size; ++i) {
     81       EmitByte(Val & 255, CurByte, OS);
     82       Val >>= 8;
     83     }
     84   }
     85 
     86   void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
     87   void EmitIMM(const MCInst &MI, unsigned &CurByte, raw_ostream &OS) const;
     88 
     89   void EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel,
     90                      unsigned &CurByte, raw_ostream &OS,
     91                      SmallVectorImpl<MCFixup> &Fixups) const;
     92 
     93   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
     94                          SmallVectorImpl<MCFixup> &Fixups) const;
     95 };
     96 
     97 } // end anonymous namespace
     98 
     99 
    100 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const MCInstrInfo &MCII,
    101                                                const MCSubtargetInfo &STI,
    102                                                MCContext &Ctx) {
    103   return new MBlazeMCCodeEmitter(MCII, STI, Ctx);
    104 }
    105 
    106 /// getMachineOpValue - Return binary encoding of operand. If the machine
    107 /// operand requires relocation, record the relocation and return zero.
    108 unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
    109                                              const MCOperand &MO) const {
    110   if (MO.isReg())
    111     return getMBlazeRegisterNumbering(MO.getReg());
    112   else if (MO.isImm())
    113     return static_cast<unsigned>(MO.getImm());
    114   else if (MO.isExpr())
    115       return 0; // The relocation has already been recorded at this point.
    116   else {
    117 #ifndef NDEBUG
    118     errs() << MO;
    119 #endif
    120     llvm_unreachable(0);
    121   }
    122   return 0;
    123 }
    124 
    125 void MBlazeMCCodeEmitter::
    126 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
    127   int32_t val = (int32_t)imm.getImm();
    128   if (val > 32767 || val < -32768) {
    129     EmitByte(0x0D, CurByte, OS);
    130     EmitByte(0x00, CurByte, OS);
    131     EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
    132     EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
    133   }
    134 }
    135 
    136 void MBlazeMCCodeEmitter::
    137 EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
    138   switch (MI.getOpcode()) {
    139   default: break;
    140 
    141   case MBlaze::ADDIK32:
    142   case MBlaze::ORI32:
    143   case MBlaze::BRLID32:
    144     EmitByte(0x0D, CurByte, OS);
    145     EmitByte(0x00, CurByte, OS);
    146     EmitRawByte(0, CurByte, OS);
    147     EmitRawByte(0, CurByte, OS);
    148   }
    149 }
    150 
    151 void MBlazeMCCodeEmitter::
    152 EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel, unsigned &CurByte,
    153               raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups) const {
    154   assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
    155 
    156   MCOperand oper = MI.getOperand(opNo);
    157 
    158   if (oper.isImm()) {
    159     EmitIMM(oper, CurByte, OS);
    160   } else if (oper.isExpr()) {
    161     MCFixupKind FixupKind;
    162     switch (MI.getOpcode()) {
    163     default:
    164       FixupKind = pcrel ? FK_PCRel_2 : FK_Data_2;
    165       Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
    166       break;
    167     case MBlaze::ORI32:
    168     case MBlaze::ADDIK32:
    169     case MBlaze::BRLID32:
    170       FixupKind = pcrel ? FK_PCRel_4 : FK_Data_4;
    171       Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
    172       break;
    173     }
    174   }
    175 }
    176 
    177 
    178 
    179 void MBlazeMCCodeEmitter::
    180 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
    181                   SmallVectorImpl<MCFixup> &Fixups) const {
    182   unsigned Opcode = MI.getOpcode();
    183   const MCInstrDesc &Desc = MCII.get(Opcode);
    184   uint64_t TSFlags = Desc.TSFlags;
    185   // Keep track of the current byte being emitted.
    186   unsigned CurByte = 0;
    187 
    188   // Emit an IMM instruction if the instruction we are encoding requires it
    189   EmitIMM(MI,CurByte,OS);
    190 
    191   switch ((TSFlags & MBlazeII::FormMask)) {
    192   default: break;
    193   case MBlazeII::FPseudo:
    194     // Pseudo instructions don't get encoded.
    195     return;
    196   case MBlazeII::FRRI:
    197     EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
    198     break;
    199   case MBlazeII::FRIR:
    200     EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
    201     break;
    202   case MBlazeII::FCRI:
    203     EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
    204     break;
    205   case MBlazeII::FRCI:
    206     EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
    207   case MBlazeII::FCCI:
    208     EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
    209     break;
    210   }
    211 
    212   ++MCNumEmitted;  // Keep track of the # of mi's emitted
    213   unsigned Value = getBinaryCodeForInstr(MI);
    214   EmitConstant(Value, 4, CurByte, OS);
    215 }
    216 
    217 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
    218 // be able to generate code emitter helpers for either variant, like it
    219 // does for the AsmWriter.
    220 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
    221 #define MachineInstr MCInst
    222 #include "MBlazeGenCodeEmitter.inc"
    223 #undef MBlazeCodeEmitter
    224 #undef MachineInstr
    225