Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 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 "mblaze-emitter" |
| 15 | #include "MBlaze.h" |
| 16 | #include "MBlazeInstrInfo.h" |
| 17 | #include "MBlazeFixupKinds.h" |
| 18 | #include "llvm/MC/MCCodeEmitter.h" |
| 19 | #include "llvm/MC/MCExpr.h" |
| 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include "llvm/MC/MCSymbol.h" |
| 22 | #include "llvm/MC/MCFixup.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); |
| 28 | |
| 29 | namespace { |
| 30 | class MBlazeMCCodeEmitter : public MCCodeEmitter { |
| 31 | MBlazeMCCodeEmitter(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT |
| 32 | void operator=(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT |
| 33 | const TargetMachine &TM; |
| 34 | const TargetInstrInfo &TII; |
| 35 | MCContext &Ctx; |
| 36 | |
| 37 | public: |
| 38 | MBlazeMCCodeEmitter(TargetMachine &tm, MCContext &ctx) |
| 39 | : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) { |
| 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 | unsigned getNumFixupKinds() const { |
| 56 | return 2; |
| 57 | } |
| 58 | |
| 59 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 60 | const static MCFixupKindInfo Infos[] = { |
| 61 | { "reloc_pcrel_4byte", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel }, |
| 62 | { "reloc_pcrel_2byte", 0, 2 * 8, MCFixupKindInfo::FKF_IsPCRel } }; |
| 63 | |
| 64 | if (Kind < FirstTargetFixupKind) |
| 65 | return MCCodeEmitter::getFixupKindInfo(Kind); |
| 66 | |
| 67 | if (unsigned(Kind-FirstTargetFixupKind) < getNumFixupKinds()) |
| 68 | return Infos[Kind - FirstTargetFixupKind]; |
| 69 | |
| 70 | assert(0 && "Invalid fixup kind."); |
| 71 | return Infos[0]; |
| 72 | } |
| 73 | |
| 74 | static unsigned GetMBlazeRegNum(const MCOperand &MO) { |
| 75 | // FIXME: getMBlazeRegisterNumbering() is sufficient? |
| 76 | assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented."); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const { |
| 81 | // The MicroBlaze uses a bit reversed format so we need to reverse the |
| 82 | // order of the bits. Taken from: |
| 83 | // http://graphics.stanford.edu/~seander/bithacks.html |
| 84 | C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; |
| 85 | |
| 86 | OS << (char)C; |
| 87 | ++CurByte; |
| 88 | } |
| 89 | |
| 90 | void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const { |
| 91 | OS << (char)C; |
| 92 | ++CurByte; |
| 93 | } |
| 94 | |
| 95 | void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte, |
| 96 | raw_ostream &OS) const { |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 97 | assert(Size <= 8 && "size too big in emit constant"); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 98 | |
| 99 | for (unsigned i = 0; i != Size; ++i) { |
| 100 | EmitByte(Val & 255, CurByte, OS); |
| 101 | Val >>= 8; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const; |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 106 | void EmitIMM(const MCInst &MI, unsigned op, unsigned &CurByte, |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 107 | raw_ostream &OS) const; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 108 | |
| 109 | void EmitImmediate(const MCInst &MI, |
| 110 | unsigned opNo, MCFixupKind FixupKind, |
| 111 | unsigned &CurByte, raw_ostream &OS, |
| 112 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 113 | |
| 114 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 115 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 116 | }; |
| 117 | |
| 118 | } // end anonymous namespace |
| 119 | |
| 120 | |
| 121 | MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &, |
| 122 | TargetMachine &TM, |
| 123 | MCContext &Ctx) { |
| 124 | return new MBlazeMCCodeEmitter(TM, Ctx); |
| 125 | } |
| 126 | |
| 127 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 128 | /// operand requires relocation, record the relocation and return zero. |
| 129 | unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI, |
| 130 | const MCOperand &MO) const { |
| 131 | if (MO.isReg()) |
| 132 | return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg()); |
| 133 | else if (MO.isImm()) |
| 134 | return static_cast<unsigned>(MO.getImm()); |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 135 | else if (MO.isExpr()) |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 136 | return 0; // The relocation has already been recorded at this point. |
| 137 | else { |
| 138 | #ifndef NDEBUG |
| 139 | errs() << MO; |
| 140 | #endif |
| 141 | llvm_unreachable(0); |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | void MBlazeMCCodeEmitter:: |
| 147 | EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const { |
| 148 | int32_t val = (int32_t)imm.getImm(); |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 149 | if (val > 32767 || val < -32678) { |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 150 | EmitByte(0x0D, CurByte, OS); |
| 151 | EmitByte(0x00, CurByte, OS); |
| 152 | EmitRawByte((val >> 24) & 0xFF, CurByte, OS); |
| 153 | EmitRawByte((val >> 16) & 0xFF, CurByte, OS); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void MBlazeMCCodeEmitter:: |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 158 | EmitIMM(const MCInst &MI, unsigned op, unsigned &CurByte, |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 159 | raw_ostream &OS) const { |
| 160 | MCOperand mcop = MI.getOperand(op); |
| 161 | if (mcop.isExpr()) { |
| 162 | EmitByte(0x0D, CurByte, OS); |
| 163 | EmitByte(0x00, CurByte, OS); |
| 164 | EmitRawByte(0, CurByte, OS); |
| 165 | EmitRawByte(0, CurByte, OS); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void MBlazeMCCodeEmitter:: |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 170 | EmitImmediate(const MCInst &MI, unsigned opNo, MCFixupKind FixupKind, |
| 171 | unsigned &CurByte, raw_ostream &OS, |
| 172 | SmallVectorImpl<MCFixup> &Fixups) const { |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 173 | assert(MI.getNumOperands()>opNo && "Not enought operands for instruction"); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 174 | |
| 175 | MCOperand oper = MI.getOperand(opNo); |
| 176 | if (oper.isImm()) { |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 177 | EmitIMM(oper, CurByte, OS); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 178 | } else if (oper.isExpr()) { |
| 179 | Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind)); |
| 180 | } |
| 181 | } |
| 182 | |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 183 | |
| 184 | |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 185 | void MBlazeMCCodeEmitter:: |
| 186 | EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 187 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 188 | unsigned Opcode = MI.getOpcode(); |
| 189 | const TargetInstrDesc &Desc = TII.get(Opcode); |
| 190 | uint64_t TSFlags = Desc.TSFlags; |
| 191 | // Keep track of the current byte being emitted. |
| 192 | unsigned CurByte = 0; |
| 193 | |
| 194 | switch ((TSFlags & MBlazeII::FormMask)) { |
| 195 | default: break; |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 196 | case MBlazeII::FPseudo: |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 197 | // Pseudo instructions don't get encoded. |
| 198 | return; |
| 199 | |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 200 | case MBlazeII::FRRI: |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 201 | EmitImmediate(MI, 2, FK_Data_4, CurByte, OS, Fixups); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 202 | break; |
| 203 | |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 204 | case MBlazeII::FRIR: |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 205 | EmitImmediate(MI, 1, FK_Data_4, CurByte, OS, Fixups); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 206 | break; |
| 207 | |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 208 | case MBlazeII::FCRI: |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 209 | EmitImmediate(MI, 1, MCFixupKind(MBlaze::reloc_pcrel_2byte), CurByte, OS, |
| 210 | Fixups); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 211 | break; |
| 212 | |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 213 | case MBlazeII::FRCI: |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 214 | EmitImmediate(MI, 1, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS, |
| 215 | Fixups); |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 216 | |
| 217 | case MBlazeII::FCCI: |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame^] | 218 | EmitImmediate(MI, 0, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS, |
| 219 | Fixups); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | |
| 223 | ++MCNumEmitted; // Keep track of the # of mi's emitted |
| 224 | unsigned Value = getBinaryCodeForInstr(MI); |
| 225 | switch (Opcode) { |
| 226 | default: |
| 227 | EmitConstant(Value, 4, CurByte, OS); |
| 228 | break; |
| 229 | |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 230 | case MBlaze::BRLID: |
| 231 | case MBlaze::BRALID: |
| 232 | EmitIMM(MI,1,CurByte,OS); |
| 233 | EmitConstant(Value, 4, CurByte, OS); |
| 234 | break; |
| 235 | |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 236 | case MBlaze::BRI: |
| 237 | case MBlaze::BRAI: |
| 238 | case MBlaze::BRID: |
| 239 | case MBlaze::BRAID: |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 240 | EmitIMM(MI,0,CurByte,OS); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 241 | EmitConstant(Value, 4, CurByte, OS); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // FIXME: These #defines shouldn't be necessary. Instead, tblgen should |
| 247 | // be able to generate code emitter helpers for either variant, like it |
| 248 | // does for the AsmWriter. |
| 249 | #define MBlazeCodeEmitter MBlazeMCCodeEmitter |
| 250 | #define MachineInstr MCInst |
| 251 | #include "MBlazeGenCodeEmitter.inc" |
| 252 | #undef MBlazeCodeEmitter |
| 253 | #undef MachineInstr |