Explictly pass MCSubtargetInfo to MCCodeEmitter::EncodeInstruction()
[oota-llvm.git] / lib / Target / Sparc / MCTargetDesc / SparcMCCodeEmitter.cpp
1 //===-- 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"
15 #include "SparcMCExpr.h"
16 #include "MCTargetDesc/SparcFixupKinds.h"
17 #include "SparcMCTargetDesc.h"
18 #include "llvm/ADT/Statistic.h"
19 #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"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
29
30 namespace {
31 class SparcMCCodeEmitter : public MCCodeEmitter {
32   SparcMCCodeEmitter(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
33   void operator=(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34   MCContext &Ctx;
35
36 public:
37   SparcMCCodeEmitter(MCContext &ctx): Ctx(ctx) {}
38
39   ~SparcMCCodeEmitter() {}
40
41   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
42                          SmallVectorImpl<MCFixup> &Fixups,
43                          const MCSubtargetInfo &STI) const;
44
45   // getBinaryCodeForInstr - TableGen'erated function for getting the
46   // binary encoding for an instruction.
47   uint64_t getBinaryCodeForInstr(const MCInst &MI,
48                                  SmallVectorImpl<MCFixup> &Fixups) const;
49
50   /// getMachineOpValue - Return binary encoding of operand. If the machine
51   /// operand requires relocation, record the relocation and return zero.
52   unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
53                              SmallVectorImpl<MCFixup> &Fixups) const;
54
55   unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
56                              SmallVectorImpl<MCFixup> &Fixups) const;
57   unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
58                              SmallVectorImpl<MCFixup> &Fixups) const;
59
60 };
61 } // end anonymous namespace
62
63 MCCodeEmitter *llvm::createSparcMCCodeEmitter(const MCInstrInfo &MCII,
64                                               const MCRegisterInfo &MRI,
65                                               const MCSubtargetInfo &STI,
66                                               MCContext &Ctx) {
67   return new SparcMCCodeEmitter(Ctx);
68 }
69
70 void SparcMCCodeEmitter::
71 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
72                   SmallVectorImpl<MCFixup> &Fixups,
73                   const MCSubtargetInfo &STI) const {
74   unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
75
76   // Output the constant in big endian byte order.
77   for (unsigned i = 0; i != 4; ++i) {
78     OS << (char)(Bits >> 24);
79     Bits <<= 8;
80   }
81
82   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
83 }
84
85
86 unsigned SparcMCCodeEmitter::
87 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
88                   SmallVectorImpl<MCFixup> &Fixups) const {
89
90   if (MO.isReg())
91     return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
92
93   if (MO.isImm())
94     return MO.getImm();
95
96   assert(MO.isExpr());
97   const MCExpr *Expr = MO.getExpr();
98   if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
99     switch(SExpr->getKind()) {
100     default: assert(0 && "Unhandled sparc expression!"); break;
101     case SparcMCExpr::VK_Sparc_LO:
102       Fixups.push_back(MCFixup::Create(0, Expr,
103                                        (MCFixupKind)Sparc::fixup_sparc_lo10));
104       break;
105     case SparcMCExpr::VK_Sparc_HI:
106       Fixups.push_back(MCFixup::Create(0, Expr,
107                                        (MCFixupKind)Sparc::fixup_sparc_hi22));
108       break;
109     case SparcMCExpr::VK_Sparc_H44:
110       Fixups.push_back(MCFixup::Create(0, Expr,
111                                        (MCFixupKind)Sparc::fixup_sparc_h44));
112       break;
113     case SparcMCExpr::VK_Sparc_M44:
114       Fixups.push_back(MCFixup::Create(0, Expr,
115                                        (MCFixupKind)Sparc::fixup_sparc_m44));
116       break;
117     case SparcMCExpr::VK_Sparc_L44:
118       Fixups.push_back(MCFixup::Create(0, Expr,
119                                        (MCFixupKind)Sparc::fixup_sparc_l44));
120       break;
121     case SparcMCExpr::VK_Sparc_HH:
122       Fixups.push_back(MCFixup::Create(0, Expr,
123                                        (MCFixupKind)Sparc::fixup_sparc_hh));
124       break;
125     case SparcMCExpr::VK_Sparc_HM:
126       Fixups.push_back(MCFixup::Create(0, Expr,
127                                        (MCFixupKind)Sparc::fixup_sparc_hm));
128       break;
129     }
130     return 0;
131   }
132
133   int64_t Res;
134   if (Expr->EvaluateAsAbsolute(Res))
135     return Res;
136
137   assert(0 && "Unhandled expression!");
138   return 0;
139 }
140
141 unsigned SparcMCCodeEmitter::
142 getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
143                      SmallVectorImpl<MCFixup> &Fixups) const {
144   const MCOperand &MO = MI.getOperand(OpNo);
145   if (MO.isReg() || MO.isImm())
146     return getMachineOpValue(MI, MO, Fixups);
147
148   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
149                                    (MCFixupKind)Sparc::fixup_sparc_call30));
150   return 0;
151 }
152
153 unsigned SparcMCCodeEmitter::
154 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
155                   SmallVectorImpl<MCFixup> &Fixups) const {
156   const MCOperand &MO = MI.getOperand(OpNo);
157   if (MO.isReg() || MO.isImm())
158     return getMachineOpValue(MI, MO, Fixups);
159
160   Sparc::Fixups fixup = Sparc::fixup_sparc_br22;
161   if (MI.getOpcode() == SP::BPXCC)
162     fixup = Sparc::fixup_sparc_br19;
163
164   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
165                                    (MCFixupKind)fixup));
166   return 0;
167 }
168
169 #include "SparcGenMCCodeEmitter.inc"