[SparcV9] Add support for parsing branch instructions with prediction.
[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/MC/MCSymbol.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 using namespace llvm;
28
29 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
30
31 namespace {
32 class SparcMCCodeEmitter : public MCCodeEmitter {
33   SparcMCCodeEmitter(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34   void operator=(const SparcMCCodeEmitter &) LLVM_DELETED_FUNCTION;
35   MCContext &Ctx;
36
37 public:
38   SparcMCCodeEmitter(MCContext &ctx): Ctx(ctx) {}
39
40   ~SparcMCCodeEmitter() {}
41
42   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
43                          SmallVectorImpl<MCFixup> &Fixups,
44                          const MCSubtargetInfo &STI) const;
45
46   // getBinaryCodeForInstr - TableGen'erated function for getting the
47   // binary encoding for an instruction.
48   uint64_t getBinaryCodeForInstr(const MCInst &MI,
49                                  SmallVectorImpl<MCFixup> &Fixups,
50                                  const MCSubtargetInfo &STI) const;
51
52   /// getMachineOpValue - Return binary encoding of operand. If the machine
53   /// operand requires relocation, record the relocation and return zero.
54   unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
55                              SmallVectorImpl<MCFixup> &Fixups,
56                              const MCSubtargetInfo &STI) const;
57
58   unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
59                              SmallVectorImpl<MCFixup> &Fixups,
60                              const MCSubtargetInfo &STI) const;
61   unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
62                              SmallVectorImpl<MCFixup> &Fixups,
63                              const MCSubtargetInfo &STI) const;
64   unsigned getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
65                                       SmallVectorImpl<MCFixup> &Fixups,
66                                       const MCSubtargetInfo &STI) const;
67 };
68 } // end anonymous namespace
69
70 MCCodeEmitter *llvm::createSparcMCCodeEmitter(const MCInstrInfo &MCII,
71                                               const MCRegisterInfo &MRI,
72                                               const MCSubtargetInfo &STI,
73                                               MCContext &Ctx) {
74   return new SparcMCCodeEmitter(Ctx);
75 }
76
77 void SparcMCCodeEmitter::
78 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
79                   SmallVectorImpl<MCFixup> &Fixups,
80                   const MCSubtargetInfo &STI) const {
81   unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
82
83   // Output the constant in big endian byte order.
84   for (unsigned i = 0; i != 4; ++i) {
85     OS << (char)(Bits >> 24);
86     Bits <<= 8;
87   }
88   unsigned tlsOpNo = 0;
89   switch (MI.getOpcode()) {
90   default: break;
91   case SP::TLS_CALL:   tlsOpNo = 1; break;
92   case SP::TLS_ADDrr:
93   case SP::TLS_ADDXrr:
94   case SP::TLS_LDrr:
95   case SP::TLS_LDXrr:  tlsOpNo = 3; break;
96   }
97   if (tlsOpNo != 0) {
98     const MCOperand &MO = MI.getOperand(tlsOpNo);
99     uint64_t op = getMachineOpValue(MI, MO, Fixups, STI);
100     assert(op == 0 && "Unexpected operand value!");
101     (void)op; // suppress warning.
102   }
103
104   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
105 }
106
107
108 unsigned SparcMCCodeEmitter::
109 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
110                   SmallVectorImpl<MCFixup> &Fixups,
111                   const MCSubtargetInfo &STI) const {
112
113   if (MO.isReg())
114     return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
115
116   if (MO.isImm())
117     return MO.getImm();
118
119   assert(MO.isExpr());
120   const MCExpr *Expr = MO.getExpr();
121   if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
122     MCFixupKind Kind = (MCFixupKind)SExpr->getFixupKind();
123     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
124     return 0;
125   }
126
127   int64_t Res;
128   if (Expr->EvaluateAsAbsolute(Res))
129     return Res;
130
131   assert(0 && "Unhandled expression!");
132   return 0;
133 }
134
135 unsigned SparcMCCodeEmitter::
136 getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
137                      SmallVectorImpl<MCFixup> &Fixups,
138                      const MCSubtargetInfo &STI) const {
139   const MCOperand &MO = MI.getOperand(OpNo);
140   if (MO.isReg() || MO.isImm())
141     return getMachineOpValue(MI, MO, Fixups, STI);
142
143   if (MI.getOpcode() == SP::TLS_CALL) {
144     // No fixups for __tls_get_addr. Will emit for fixups for tls_symbol in
145     // EncodeInstruction.
146 #ifndef NDEBUG
147     // Verify that the callee is actually __tls_get_addr.
148     const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(MO.getExpr());
149     assert(SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef &&
150            "Unexpected expression in TLS_CALL");
151     const MCSymbolRefExpr *SymExpr = cast<MCSymbolRefExpr>(SExpr->getSubExpr());
152     assert(SymExpr->getSymbol().getName() == "__tls_get_addr" &&
153            "Unexpected function for TLS_CALL");
154 #endif
155     return 0;
156   }
157
158   MCFixupKind fixupKind = (MCFixupKind)Sparc::fixup_sparc_call30;
159
160   if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(MO.getExpr())) {
161     if (SExpr->getKind() == SparcMCExpr::VK_Sparc_WPLT30)
162       fixupKind = (MCFixupKind)Sparc::fixup_sparc_wplt30;
163   }
164
165   Fixups.push_back(MCFixup::Create(0, MO.getExpr(), fixupKind));
166
167   return 0;
168 }
169
170 unsigned SparcMCCodeEmitter::
171 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
172                   SmallVectorImpl<MCFixup> &Fixups,
173                   const MCSubtargetInfo &STI) const {
174   const MCOperand &MO = MI.getOperand(OpNo);
175   if (MO.isReg() || MO.isImm())
176     return getMachineOpValue(MI, MO, Fixups, STI);
177
178   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
179                                    (MCFixupKind)Sparc::fixup_sparc_br22));
180   return 0;
181 }
182
183 unsigned SparcMCCodeEmitter::
184 getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
185                            SmallVectorImpl<MCFixup> &Fixups,
186                            const MCSubtargetInfo &STI) const {
187   const MCOperand &MO = MI.getOperand(OpNo);
188   if (MO.isReg() || MO.isImm())
189     return getMachineOpValue(MI, MO, Fixups, STI);
190
191   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
192                                    (MCFixupKind)Sparc::fixup_sparc_br19));
193   return 0;
194 }
195
196
197 #include "SparcGenMCCodeEmitter.inc"