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