1 //===-- SparcMCCodeEmitter.cpp - Convert Sparc code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SparcMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #include "SparcMCExpr.h"
15 #include "MCTargetDesc/SparcFixupKinds.h"
16 #include "SparcMCTargetDesc.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/raw_ostream.h"
28 #define DEBUG_TYPE "mccodeemitter"
30 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
33 class SparcMCCodeEmitter : public MCCodeEmitter {
34 SparcMCCodeEmitter(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
35 void operator=(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
39 SparcMCCodeEmitter(MCContext &ctx): Ctx(ctx) {}
41 ~SparcMCCodeEmitter() {}
43 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
44 SmallVectorImpl<MCFixup> &Fixups,
45 const MCSubtargetInfo &STI) const override;
47 // getBinaryCodeForInstr - TableGen'erated function for getting the
48 // binary encoding for an instruction.
49 uint64_t getBinaryCodeForInstr(const MCInst &MI,
50 SmallVectorImpl<MCFixup> &Fixups,
51 const MCSubtargetInfo &STI) const;
53 /// getMachineOpValue - Return binary encoding of operand. If the machine
54 /// operand requires relocation, record the relocation and return zero.
55 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
56 SmallVectorImpl<MCFixup> &Fixups,
57 const MCSubtargetInfo &STI) const;
59 unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
60 SmallVectorImpl<MCFixup> &Fixups,
61 const MCSubtargetInfo &STI) const;
62 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &STI) const;
65 unsigned getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
66 SmallVectorImpl<MCFixup> &Fixups,
67 const MCSubtargetInfo &STI) const;
68 unsigned getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
69 SmallVectorImpl<MCFixup> &Fixups,
70 const MCSubtargetInfo &STI) const;
73 } // end anonymous namespace
75 MCCodeEmitter *llvm::createSparcMCCodeEmitter(const MCInstrInfo &MCII,
76 const MCRegisterInfo &MRI,
77 const MCSubtargetInfo &STI,
79 return new SparcMCCodeEmitter(Ctx);
82 void SparcMCCodeEmitter::
83 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
84 SmallVectorImpl<MCFixup> &Fixups,
85 const MCSubtargetInfo &STI) const {
86 unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
88 // Output the constant in big endian byte order.
89 for (unsigned i = 0; i != 4; ++i) {
90 OS << (char)(Bits >> 24);
94 switch (MI.getOpcode()) {
96 case SP::TLS_CALL: tlsOpNo = 1; break;
100 case SP::TLS_LDXrr: tlsOpNo = 3; break;
103 const MCOperand &MO = MI.getOperand(tlsOpNo);
104 uint64_t op = getMachineOpValue(MI, MO, Fixups, STI);
105 assert(op == 0 && "Unexpected operand value!");
106 (void)op; // suppress warning.
109 ++MCNumEmitted; // Keep track of the # of mi's emitted.
113 unsigned SparcMCCodeEmitter::
114 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
115 SmallVectorImpl<MCFixup> &Fixups,
116 const MCSubtargetInfo &STI) const {
119 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
125 const MCExpr *Expr = MO.getExpr();
126 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
127 MCFixupKind Kind = (MCFixupKind)SExpr->getFixupKind();
128 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
133 if (Expr->EvaluateAsAbsolute(Res))
136 llvm_unreachable("Unhandled expression!");
140 unsigned SparcMCCodeEmitter::
141 getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
142 SmallVectorImpl<MCFixup> &Fixups,
143 const MCSubtargetInfo &STI) const {
144 const MCOperand &MO = MI.getOperand(OpNo);
145 if (MO.isReg() || MO.isImm())
146 return getMachineOpValue(MI, MO, Fixups, STI);
148 if (MI.getOpcode() == SP::TLS_CALL) {
149 // No fixups for __tls_get_addr. Will emit for fixups for tls_symbol in
150 // EncodeInstruction.
152 // Verify that the callee is actually __tls_get_addr.
153 const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(MO.getExpr());
154 assert(SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef &&
155 "Unexpected expression in TLS_CALL");
156 const MCSymbolRefExpr *SymExpr = cast<MCSymbolRefExpr>(SExpr->getSubExpr());
157 assert(SymExpr->getSymbol().getName() == "__tls_get_addr" &&
158 "Unexpected function for TLS_CALL");
163 MCFixupKind fixupKind = (MCFixupKind)Sparc::fixup_sparc_call30;
165 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(MO.getExpr())) {
166 if (SExpr->getKind() == SparcMCExpr::VK_Sparc_WPLT30)
167 fixupKind = (MCFixupKind)Sparc::fixup_sparc_wplt30;
170 Fixups.push_back(MCFixup::Create(0, MO.getExpr(), fixupKind));
175 unsigned SparcMCCodeEmitter::
176 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
177 SmallVectorImpl<MCFixup> &Fixups,
178 const MCSubtargetInfo &STI) const {
179 const MCOperand &MO = MI.getOperand(OpNo);
180 if (MO.isReg() || MO.isImm())
181 return getMachineOpValue(MI, MO, Fixups, STI);
183 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
184 (MCFixupKind)Sparc::fixup_sparc_br22));
188 unsigned SparcMCCodeEmitter::
189 getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
190 SmallVectorImpl<MCFixup> &Fixups,
191 const MCSubtargetInfo &STI) const {
192 const MCOperand &MO = MI.getOperand(OpNo);
193 if (MO.isReg() || MO.isImm())
194 return getMachineOpValue(MI, MO, Fixups, STI);
196 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
197 (MCFixupKind)Sparc::fixup_sparc_br19));
200 unsigned SparcMCCodeEmitter::
201 getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
202 SmallVectorImpl<MCFixup> &Fixups,
203 const MCSubtargetInfo &STI) const {
204 const MCOperand &MO = MI.getOperand(OpNo);
205 if (MO.isReg() || MO.isImm())
206 return getMachineOpValue(MI, MO, Fixups, STI);
208 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
209 (MCFixupKind)Sparc::fixup_sparc_br16_2));
210 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
211 (MCFixupKind)Sparc::fixup_sparc_br16_14));
218 #include "SparcGenMCCodeEmitter.inc"