blob: 8c318274a454e09bdb7cb34b7d5987ea013dde87 [file] [log] [blame]
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +00001//===-- SparcMCCodeEmitter.cpp - Convert Sparc 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 SparcMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mccodeemitter"
Venkatraman Govindarajub73aeca2014-01-06 01:22:54 +000015#include "SparcMCExpr.h"
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000016#include "MCTargetDesc/SparcFixupKinds.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "SparcMCTargetDesc.h"
18#include "llvm/ADT/Statistic.h"
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCRegisterInfo.h"
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000024#include "llvm/Support/raw_ostream.h"
25
26using namespace llvm;
27
28STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
29
30namespace {
31class SparcMCCodeEmitter : public MCCodeEmitter {
32 SparcMCCodeEmitter(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
33 void operator=(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34 MCContext &Ctx;
35
36public:
37 SparcMCCodeEmitter(MCContext &ctx): Ctx(ctx) {}
38
39 ~SparcMCCodeEmitter() {}
40
41 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000042 SmallVectorImpl<MCFixup> &Fixups,
43 const MCSubtargetInfo &STI) const;
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000044
45 // getBinaryCodeForInstr - TableGen'erated function for getting the
46 // binary encoding for an instruction.
47 uint64_t getBinaryCodeForInstr(const MCInst &MI,
David Woodhouse3fa98a62014-01-28 23:13:18 +000048 SmallVectorImpl<MCFixup> &Fixups,
49 const MCSubtargetInfo &STI) const;
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000050
51 /// getMachineOpValue - Return binary encoding of operand. If the machine
52 /// operand requires relocation, record the relocation and return zero.
53 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +000054 SmallVectorImpl<MCFixup> &Fixups,
55 const MCSubtargetInfo &STI) const;
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000056
57 unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000058 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const;
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000060 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000061 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const;
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000063
64};
65} // end anonymous namespace
66
67MCCodeEmitter *llvm::createSparcMCCodeEmitter(const MCInstrInfo &MCII,
68 const MCRegisterInfo &MRI,
69 const MCSubtargetInfo &STI,
70 MCContext &Ctx) {
71 return new SparcMCCodeEmitter(Ctx);
72}
73
74void SparcMCCodeEmitter::
75EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000076 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const {
David Woodhouse3fa98a62014-01-28 23:13:18 +000078 unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000079
80 // Output the constant in big endian byte order.
81 for (unsigned i = 0; i != 4; ++i) {
82 OS << (char)(Bits >> 24);
83 Bits <<= 8;
84 }
85
86 ++MCNumEmitted; // Keep track of the # of mi's emitted.
87}
88
89
90unsigned SparcMCCodeEmitter::
91getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +000092 SmallVectorImpl<MCFixup> &Fixups,
93 const MCSubtargetInfo &STI) const {
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000094
95 if (MO.isReg())
96 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
97
98 if (MO.isImm())
99 return MO.getImm();
100
101 assert(MO.isExpr());
102 const MCExpr *Expr = MO.getExpr();
Venkatraman Govindarajub73aeca2014-01-06 01:22:54 +0000103 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
104 switch(SExpr->getKind()) {
105 default: assert(0 && "Unhandled sparc expression!"); break;
106 case SparcMCExpr::VK_Sparc_LO:
107 Fixups.push_back(MCFixup::Create(0, Expr,
108 (MCFixupKind)Sparc::fixup_sparc_lo10));
109 break;
110 case SparcMCExpr::VK_Sparc_HI:
111 Fixups.push_back(MCFixup::Create(0, Expr,
112 (MCFixupKind)Sparc::fixup_sparc_hi22));
113 break;
114 case SparcMCExpr::VK_Sparc_H44:
115 Fixups.push_back(MCFixup::Create(0, Expr,
116 (MCFixupKind)Sparc::fixup_sparc_h44));
117 break;
118 case SparcMCExpr::VK_Sparc_M44:
119 Fixups.push_back(MCFixup::Create(0, Expr,
120 (MCFixupKind)Sparc::fixup_sparc_m44));
121 break;
122 case SparcMCExpr::VK_Sparc_L44:
123 Fixups.push_back(MCFixup::Create(0, Expr,
124 (MCFixupKind)Sparc::fixup_sparc_l44));
125 break;
126 case SparcMCExpr::VK_Sparc_HH:
127 Fixups.push_back(MCFixup::Create(0, Expr,
128 (MCFixupKind)Sparc::fixup_sparc_hh));
129 break;
130 case SparcMCExpr::VK_Sparc_HM:
131 Fixups.push_back(MCFixup::Create(0, Expr,
132 (MCFixupKind)Sparc::fixup_sparc_hm));
133 break;
134 }
135 return 0;
136 }
137
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +0000138 int64_t Res;
139 if (Expr->EvaluateAsAbsolute(Res))
140 return Res;
141
142 assert(0 && "Unhandled expression!");
143 return 0;
144}
145
146unsigned SparcMCCodeEmitter::
147getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000148 SmallVectorImpl<MCFixup> &Fixups,
149 const MCSubtargetInfo &STI) const {
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +0000150 const MCOperand &MO = MI.getOperand(OpNo);
151 if (MO.isReg() || MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000152 return getMachineOpValue(MI, MO, Fixups, STI);
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +0000153
154 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
155 (MCFixupKind)Sparc::fixup_sparc_call30));
156 return 0;
157}
158
159unsigned SparcMCCodeEmitter::
160getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000161 SmallVectorImpl<MCFixup> &Fixups,
162 const MCSubtargetInfo &STI) const {
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +0000163 const MCOperand &MO = MI.getOperand(OpNo);
164 if (MO.isReg() || MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000165 return getMachineOpValue(MI, MO, Fixups, STI);
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +0000166
167 Sparc::Fixups fixup = Sparc::fixup_sparc_br22;
168 if (MI.getOpcode() == SP::BPXCC)
169 fixup = Sparc::fixup_sparc_br19;
170
171 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
172 (MCFixupKind)fixup));
173 return 0;
174}
175
176#include "SparcGenMCCodeEmitter.inc"