blob: 70c41e13e411933f2c30e9dba8973b208d784111 [file] [log] [blame]
Chris Lattner556d89d2003-08-01 22:19:03 +00001//===-- SparcV9CodeEmitter.cpp --------------------------------------------===//
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00002//
Chris Lattner556d89d2003-08-01 22:19:03 +00003// FIXME: document
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00004//
5//===----------------------------------------------------------------------===//
6
Misha Brukmanf86aaa82003-06-02 04:12:39 +00007#include "llvm/Constants.h"
8#include "llvm/Function.h"
9#include "llvm/GlobalVariable.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000010#include "llvm/PassManager.h"
11#include "llvm/CodeGen/MachineCodeEmitter.h"
Misha Brukmana2196c12003-06-04 20:01:13 +000012#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000013#include "llvm/CodeGen/MachineFunctionInfo.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000016#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000017#include "llvm/Target/TargetData.h"
Chris Lattner556d89d2003-08-01 22:19:03 +000018#include "Support/Debug.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000019#include "Support/hash_set"
Misha Brukman103f0c32003-09-05 22:59:31 +000020#include "Support/Statistic.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000021#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000022#include "SparcV9CodeEmitter.h"
Misha Brukmancfd67c92003-08-29 04:22:54 +000023#include "Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000024
Misha Brukman103f0c32003-09-05 22:59:31 +000025namespace {
26 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
27 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
28 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
Brian Gaekef4785562003-09-30 17:49:41 +000029 Statistic<> WordsEmitted("words-emitted", "No. of words emitted to memory");
Misha Brukman103f0c32003-09-05 22:59:31 +000030}
31
Brian Gaekee69f7272003-08-14 06:04:59 +000032bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000033 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000034 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000035 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000036 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000037 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000038 return false;
39}
40
Misha Brukmanf86aaa82003-06-02 04:12:39 +000041namespace {
42 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000043 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000044 MachineCodeEmitter &MCE;
45
Misha Brukman0897c602003-08-06 16:20:22 +000046 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
47 /// lazily resolved.
48 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000049 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000050
Misha Brukman0897c602003-08-06 16:20:22 +000051 /// LazyResolverMap - Keep track of the lazy resolver created for a
52 /// particular function so that we can reuse them if necessary.
53 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000054 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000055
56 public:
57 enum CallType { ShortCall, FarCall };
58
59 private:
60 /// We need to keep track of whether we used a simple call or a far call
61 /// (many instructions) in sequence. This means we need to keep track of
62 /// what type of stub we generate.
63 static std::map<uint64_t, CallType> LazyCallFlavor;
64
Misha Brukmanf86aaa82003-06-02 04:12:39 +000065 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000066 JITResolver(SparcV9CodeEmitter &V9,
67 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
68 uint64_t getLazyResolver(Function *F);
69 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000070 void deleteFunctionReference(uint64_t Address);
71 void addCallFlavor(uint64_t Address, CallType Flavor) {
72 LazyCallFlavor[Address] = Flavor;
73 }
Misha Brukmana2196c12003-06-04 20:01:13 +000074
75 // Utility functions for accessing data from static callback
76 uint64_t getCurrentPCValue() {
77 return MCE.getCurrentPCValue();
78 }
79 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
80 return SparcV9.getBinaryCodeForInstr(MI);
81 }
82
Misha Brukmanf47d9c22003-06-05 20:52:06 +000083 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
84
Misha Brukmanf86aaa82003-06-02 04:12:39 +000085 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000086 uint64_t emitStubForFunction(Function *F);
Misha Brukman103f0c32003-09-05 22:59:31 +000087 static void SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
88 uint64_t &FPRS, uint64_t &CCR);
89 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
90 uint64_t &FPRS, uint64_t &CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +000091 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000092 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000093
Misha Brukmanf86aaa82003-06-02 04:12:39 +000094 };
95
96 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +000097 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000098}
99
100/// addFunctionReference - This method is called when we need to emit the
101/// address of a function that has not yet been emitted, so we don't know the
102/// address. Instead, we emit a call to the CompilationCallback method, and
103/// keep track of where we are.
104///
Misha Brukmana2196c12003-06-04 20:01:13 +0000105uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000106 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000107 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000108}
109
Misha Brukman0897c602003-08-06 16:20:22 +0000110/// deleteFunctionReference - If we are emitting a far call, we already added a
111/// reference to the function, but it is now incorrect, since the address to the
112/// JIT resolver is too far away to be a simple call instruction. This is used
113/// to remove the address from the map.
114///
115void JITResolver::deleteFunctionReference(uint64_t Address) {
116 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
117 assert(I != LazyCodeGenMap.end() && "Not in map!");
118 LazyCodeGenMap.erase(I);
119}
120
Misha Brukmana2196c12003-06-04 20:01:13 +0000121uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
122 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000123 assert(I != LazyCodeGenMap.end() && "Not in map!");
124 Function *F = I->second;
125 LazyCodeGenMap.erase(I);
126 return MCE.forceCompilationOf(F);
127}
128
Misha Brukmana2196c12003-06-04 20:01:13 +0000129uint64_t JITResolver::getLazyResolver(Function *F) {
130 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000131 if (I != LazyResolverMap.end() && I->first == F) return I->second;
132
133//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
134
Misha Brukmana2196c12003-06-04 20:01:13 +0000135 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000136 LazyResolverMap.insert(I, std::make_pair(F, Stub));
137 return Stub;
138}
139
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000140uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
141
Misha Brukmanfad49292003-08-15 00:26:50 +0000142 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000143 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
144 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000145
Misha Brukman0897c602003-08-06 16:20:22 +0000146 MachineInstr* BinaryCode[] = {
147 //
Misha Brukman0870e972003-08-06 22:19:18 +0000148 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000149 //
Misha Brukman0870e972003-08-06 22:19:18 +0000150 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
151 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
152 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
153 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
154 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
155 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
156 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
157 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000158 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000159 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
160 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
161 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
162 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
163 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
164 // nop ;; delay slot
165 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000166 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000167
Misha Brukman0897c602003-08-06 16:20:22 +0000168 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
169 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
170 delete BinaryCode[i];
171 Addr += 4;
172 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000173
174 return Addr;
175}
176
Misha Brukman103f0c32003-09-05 22:59:31 +0000177void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
178 uint64_t &FPRS, uint64_t &CCR) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000179#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000180
Misha Brukmancfd67c92003-08-29 04:22:54 +0000181#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000182 __asm__ __volatile__ (// Save condition-code registers
183 "stx %%fsr, %0;\n\t"
184 "rd %%fprs, %1;\n\t"
185 "rd %%ccr, %2;\n\t"
186 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000187#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000188
189 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000190 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
191 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
192 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
193 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
194 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
195 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
196 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
197 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
198 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
199 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
200 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
201 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
202 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
203 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
204 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
205 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
206 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000207
Misha Brukmancfd67c92003-08-29 04:22:54 +0000208 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000209 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000210 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000211 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
212 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
213 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
214 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
215 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
216 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000217 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
218 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
219 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
220 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
221 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
222 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
223 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
224 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000225#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000226}
Misha Brukmanfad49292003-08-15 00:26:50 +0000227
Misha Brukmanfad49292003-08-15 00:26:50 +0000228
Misha Brukman103f0c32003-09-05 22:59:31 +0000229void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
230 uint64_t &FPRS, uint64_t &CCR)
231{
Misha Brukmanfad49292003-08-15 00:26:50 +0000232#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000233
Misha Brukmancfd67c92003-08-29 04:22:54 +0000234#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000235 __asm__ __volatile__ (// Restore condition-code registers
236 "ldx %0, %%fsr;\n\t"
237 "wr %1, 0, %%fprs;\n\t"
238 "wr %2, 0, %%ccr;\n\t"
239 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000240#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000241
242 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000243 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
244 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
245 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
246 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
247 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
248 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
249 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
250 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
251 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
252 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
253 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
254 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
255 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
256 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
257 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
258 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
259 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000260
Misha Brukmancfd67c92003-08-29 04:22:54 +0000261 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000262 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
263 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
264 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
265 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
266 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
267 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
268 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
269 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000270 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
271 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
272 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
273 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
274 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
275 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
276 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
277 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000278#endif
279}
280
Misha Brukmancfd67c92003-08-29 04:22:54 +0000281void JITResolver::CompilationCallback() {
282 // Local space to save double registers
283 uint64_t DoubleFP[32];
Misha Brukman103f0c32003-09-05 22:59:31 +0000284 uint64_t FSR, FPRS, CCR;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000285
Misha Brukman103f0c32003-09-05 22:59:31 +0000286 SaveRegisters(DoubleFP, FSR, FPRS, CCR);
287 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000288
289 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000290 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000291 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000292 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000293 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000294#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000295 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
296 DEBUG(std::cerr << "Read i7 (return addr) = "
297 << std::hex << returnAddr << ", value: "
298 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000299#endif
300
Misha Brukman103f0c32003-09-05 22:59:31 +0000301 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
302 // trampoline function stub!!
303 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
304 int64_t OrigTarget = (Target-CameFrom1) >> 2;
305 if ((OrigCallInst & (1 << 30)) &&
306 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
307 {
308 // The original call instruction was CALL <immed>, which means we can
309 // overwrite it directly, since the offset will fit into 30 bits
310 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
311 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
312 delete C;
313 ++OverwrittenCalls;
314 } else {
315 ++UnmodifiedCalls;
316 }
317
Misha Brukman0897c602003-08-06 16:20:22 +0000318 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000319 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000320
Misha Brukman0897c602003-08-06 16:20:22 +0000321 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000322
Misha Brukman0897c602003-08-06 16:20:22 +0000323 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000324 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000325 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000326 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000327 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000328
329 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
330 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000331 // disables frame pointer elimination currently, even if it's dead. This is
332 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000333 alloca(42+Offset);
334 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000335
336 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000337 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000338 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
339 delete SV;
340 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000341 if (CodeInMem != SaveInst) {
342 std::cerr << "About to overwrite smthg not a save instr!";
343 abort();
344 }
Misha Brukman103f0c32003-09-05 22:59:31 +0000345 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << Target << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000346
Misha Brukman103f0c32003-09-05 22:59:31 +0000347 // If the target function is close enough to fit into the 19bit disp of
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000348 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman103f0c32003-09-05 22:59:31 +0000349 int64_t BranchTarget = (Target-CodeBegin) >> 2;
350 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
351 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
352 } else {
353 // ba <target>
354 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
355 *((unsigned*)(intptr_t)CodeBegin) =
356 TheJITResolver->getBinaryCodeForInstr(*I);
357 CodeBegin += 4;
358 delete I;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000359
Misha Brukman103f0c32003-09-05 22:59:31 +0000360 // nop
361 I = BuildMI(V9::NOP, 0);
362 *((unsigned*)(intptr_t)CodeBegin) =
363 TheJITResolver->getBinaryCodeForInstr(*I);
364 delete I;
365 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000366
Misha Brukman103f0c32003-09-05 22:59:31 +0000367 RestoreRegisters(DoubleFP, FSR, FPRS, CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000368
Misha Brukman103f0c32003-09-05 22:59:31 +0000369 // Change the return address to reexecute the restore, then the jump. However,
Misha Brukmanfad49292003-08-15 00:26:50 +0000370 // we can't just modify %i7 here, because we return to the function that will
371 // restore the floating-point registers for us. Thus, we just return the value
372 // we want it to be, and the parent will take care of setting %i7 correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000373 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000374 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000375#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
376 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
377#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000378}
379
380/// emitStubForFunction - This method is used by the JIT when it needs to emit
381/// the address of a function for a function whose code has not yet been
382/// generated. In order to do this, it generates a stub which jumps to the lazy
383/// function compiler, which will eventually get fixed to call the function
384/// directly.
385///
Misha Brukmana2196c12003-06-04 20:01:13 +0000386uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000387 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000388
Misha Brukman8f122222003-06-06 00:26:11 +0000389 DEBUG(std::cerr << "Emitting stub at addr: 0x"
390 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000391
Misha Brukman0897c602003-08-06 16:20:22 +0000392 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
393
394 // restore %g0, 0, %g0
395 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
396 .addMReg(g0, MOTy::Def);
397 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
398 delete R;
399
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000400 // save %sp, -192, %sp
401 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
402 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
403 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000404
405 int64_t CurrPC = MCE.getCurrentPCValue();
406 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
407 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000408 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
409 // Since this is a far call, the actual address of the call is shifted
410 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000411 deleteFunctionReference(CurrPC);
412 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000413 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000414 // call CallTarget ;; invoke the callback
415 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
416 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
417 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000418
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000419 // nop ;; call delay slot
420 MachineInstr *Nop = BuildMI(V9::NOP, 0);
421 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
422 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000423
424 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000425 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000426
427 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000428 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000429}
430
431
Misha Brukmana2196c12003-06-04 20:01:13 +0000432SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
433 MachineCodeEmitter &M): TM(tm), MCE(M)
434{
435 TheJITResolver = new JITResolver(*this, M);
436}
437
438SparcV9CodeEmitter::~SparcV9CodeEmitter() {
439 delete TheJITResolver;
440}
441
442void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000443 // Output the constant in big endian byte order...
Brian Gaekef4785562003-09-30 17:49:41 +0000444 ++WordsEmitted;
Misha Brukman3de36f52003-05-27 20:07:58 +0000445 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000446 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000447 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000448 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000449 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000450}
451
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000452unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000453SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000454 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000455 const TargetRegInfo &RI = TM.getRegInfo();
456 unsigned regClass, regType = RI.getRegType(fakeReg);
457 // At least map fakeReg into its class
458 fakeReg = RI.getClassRegNum(fakeReg, regClass);
459
Misha Brukman9cedd432003-07-03 18:36:47 +0000460 switch (regClass) {
461 case UltraSparcRegInfo::IntRegClassID: {
462 // Sparc manual, p31
463 static const unsigned IntRegMap[] = {
464 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
465 8, 9, 10, 11, 12, 13, 15,
466 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
467 16, 17, 18, 19, 20, 21, 22, 23,
468 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
469 24, 25, 26, 27, 28, 29, 30, 31,
470 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
471 0, 1, 2, 3, 4, 5, 6, 7,
472 // "o6"
473 14
474 };
475
476 return IntRegMap[fakeReg];
477 break;
478 }
479 case UltraSparcRegInfo::FloatRegClassID: {
480 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000481 if (regType == UltraSparcRegInfo::FPSingleRegType) {
482 // only numbered 0-31, hence can already fit into 5 bits (and 6)
483 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
484 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
485 // FIXME: This assumes that we only have 5-bit register fiels!
486 // From Sparc Manual, page 40.
487 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
488 fakeReg |= (fakeReg >> 5) & 1;
489 fakeReg &= 0x1f;
490 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
491 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000492 return fakeReg;
493 }
494 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000495 /* xcc, icc, ccr */
496 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000497
Misha Brukmandfbfc572003-07-16 20:30:40 +0000498 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
499 && "CC register out of bounds for IntCCReg map");
500 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
501 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000502 }
503 case UltraSparcRegInfo::FloatCCRegClassID: {
504 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
505 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
506 return fakeReg;
507 }
508 default:
509 assert(0 && "Invalid unified register number in getRegType");
510 return fakeReg;
511 }
512}
513
514
Misha Brukman07d45162003-07-15 19:09:43 +0000515// WARNING: if the call used the delay slot to do meaningful work, that's not
516// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000517inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000518 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000519 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
520 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000521
Misha Brukman0897c602003-08-06 16:20:22 +0000522 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000523 //
Misha Brukman0870e972003-08-06 22:19:18 +0000524 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000525 //
Misha Brukman0870e972003-08-06 22:19:18 +0000526 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
527 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
528 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
529 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000530 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000531 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
532 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
533 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000534 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000535 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
536 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
537 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000538 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000539 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000540 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000541 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000542 };
Misha Brukman07d45162003-07-15 19:09:43 +0000543
Misha Brukman0897c602003-08-06 16:20:22 +0000544 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
545 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000546 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000547 uint64_t CurrPC = MCE.getCurrentPCValue();
548 TheJITResolver->addFunctionReference(CurrPC, F);
549 // Remember that this is a far call, to subtract appropriate offset later
550 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
551 }
Misha Brukman07d45162003-07-15 19:09:43 +0000552
Misha Brukman0897c602003-08-06 16:20:22 +0000553 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
554 delete BinaryCode[i];
555 }
Misha Brukman07d45162003-07-15 19:09:43 +0000556}
557
558
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000559int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
560 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000561 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
562 // or things that get fixed up later by the JIT.
563
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000564 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000565 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000566 abort();
567 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000568 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000569 Value *V = MO.getVRegValue();
570 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000571 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000572 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000573 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000574 } else if (const Constant *C = dyn_cast<Constant>(V)) {
575 if (ConstantMap.find(C) != ConstantMap.end()) {
576 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000577 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000578 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000579 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000580 abort();
581 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000582 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
583 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000584 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000585 // external function calls, etc.?
586 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000587 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000588 if (F->isExternal()) {
589 // Sparc backend broken: this MO should be `ExternalSymbol'
590 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
591 } else {
592 rv = (int64_t)MCE.getGlobalValueAddress(F);
593 }
594 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000595 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000596 // Function has not yet been code generated!
597 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
598 // Delayed resolution...
599 rv = TheJITResolver->getLazyResolver(F);
600 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000601 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000602 }
603 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000604 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000605 if (rv == 0) {
606 if (Constant *C = ConstantPointerRef::get(GV)) {
607 if (ConstantMap.find(C) != ConstantMap.end()) {
608 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
609 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000610 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000611 << ", " << *V << " not found in ConstantMap!\n";
612 abort();
613 }
614 }
615 }
Misha Brukman0870e972003-08-06 22:19:18 +0000616 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000617 }
618 // The real target of the call is Addr = PC + (rv * 4)
619 // So undo that: give the instruction (Addr - PC) / 4
620 if (MI.getOpcode() == V9::CALL) {
621 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000622 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000623 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000624 int64_t CallInstTarget = (rv - CurrPC) >> 2;
625 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
626 DEBUG(std::cerr << "Making far call!\n");
627 // addresss is out of bounds for the 30-bit call,
628 // make an indirect jump-and-link
629 emitFarCall(rv);
630 // this invalidates the instruction so that the call with an incorrect
631 // address will not be emitted
632 rv = 0;
633 } else {
634 // The call fits into 30 bits, so just return the corrected address
635 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000636 }
Misha Brukman8f122222003-06-06 00:26:11 +0000637 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000638 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000639 } else {
640 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
641 abort();
642 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000643 } else if (MO.isPhysicalRegister() ||
644 MO.getType() == MachineOperand::MO_CCRegister)
645 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000646 // This is necessary because the Sparc backend doesn't actually lay out
647 // registers in the real fashion -- it skips those that it chooses not to
648 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000649 unsigned fakeReg = MO.getAllocatedRegNum();
650 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
651 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000652 << realRegByClass << " (LLC: "
653 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000654 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000655 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000656 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000657 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000658 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000659 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000660 rv = (int64_t)
661 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000662 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000663 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000664 // Duplicate code of the above case for VirtualRegister, BasicBlock...
665 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000666 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000667 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000668 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000669 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000670 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000671 // Sparc backend doesn't generate this (yet...)
672 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
673 abort();
674 } else if (MO.isFrameIndex()) {
675 // Sparc backend doesn't generate this (yet...)
676 int FrameIndex = MO.getFrameIndex();
677 std::cerr << "ERROR: Frame index unhandled.\n";
678 abort();
679 } else if (MO.isConstantPoolIndex()) {
680 // Sparc backend doesn't generate this (yet...)
681 std::cerr << "ERROR: Constant Pool index unhandled.\n";
682 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000683 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000684 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000685 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000686 }
687
688 // Finally, deal with the various bitfield-extracting functions that
689 // are used in SPARC assembly. (Some of these make no sense in combination
690 // with some of the above; we'll trust that the instruction selector
691 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000692 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000693 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000694 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000695 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000696 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000697 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000698 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000699 return rv >> 42;
700 } else { // (unadorned) val
701 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000702 }
703}
704
705unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
706 Val >>= bit;
707 return (Val & 1);
708}
709
Misha Brukman3de36f52003-05-27 20:07:58 +0000710bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000711 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000712 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000713 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000714 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000715
Misha Brukmana2196c12003-06-04 20:01:13 +0000716 // The Sparc backend does not use MachineConstantPool;
717 // instead, it has its own constant pool implementation.
718 // We create a new MachineConstantPool here to be compatible with the emitter.
719 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000720 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
721 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
722 E = pool.end(); I != E; ++I)
723 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000724 Constant *C = (Constant*)*I;
725 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000726 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000727 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000728 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000729 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000730
Misha Brukman3de36f52003-05-27 20:07:58 +0000731 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
732 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000733 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000734
Misha Brukman9cedd432003-07-03 18:36:47 +0000735 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000736 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000737
738 // Resolve branches to BasicBlocks for the entire function
739 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
740 long Location = BBLocations[BBRefs[i].first];
741 unsigned *Ref = BBRefs[i].second.first;
742 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000743 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
744 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000745 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
746 MachineOperand &op = MI->getOperand(ii);
747 if (op.isPCRelativeDisp()) {
748 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000749 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000750 // Location is the target of the branch
751 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000752 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000753 // Save the flags.
754 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
755 if (op.opLoBits32()) { loBits32=true; }
756 if (op.opHiBits32()) { hiBits32=true; }
757 if (op.opLoBits64()) { loBits64=true; }
758 if (op.opHiBits64()) { hiBits64=true; }
759 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
760 branchTarget);
761 if (loBits32) { MI->setOperandLo32(ii); }
762 else if (hiBits32) { MI->setOperandHi32(ii); }
763 else if (loBits64) { MI->setOperandLo64(ii); }
764 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000765 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000766 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
767 *Ref = fixedInstr;
768 break;
769 }
770 }
771 }
772 BBRefs.clear();
773 BBLocations.clear();
774
Misha Brukman3de36f52003-05-27 20:07:58 +0000775 return false;
776}
777
778void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000779 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000780 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000781 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
782 unsigned binCode = getBinaryCodeForInstr(**I);
783 if (binCode == (1 << 30)) {
784 // this is an invalid call: the addr is out of bounds. that means a code
785 // sequence has already been emitted, and this is a no-op
786 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
787 } else {
788 emitWord(binCode);
789 }
790 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000791}
792
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000793void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
794 bool isPCRelative)
795{
796 if (isPCRelative) { // must be a call, this is a major hack!
797 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000798 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
799 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000800 // The real target of the call is Addr = PC + (target * 4)
801 // CurByte is the PC, Addr we just received
802 return (void*) (((long)Addr - (long)CurByte) >> 2);
803 } else {
804 if (Function *F = dyn_cast<Function>(V)) {
805 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000806 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000807 cast<Function>(V));
808 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000809 return
810 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000811
812 } else if (Constant *C = ConstantPointerRef::get(V)) {
813 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000814 return (void*)
815 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000816 } else {
817 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
818 << ", " << *V << " not found in ConstantMap!\n";
819 abort();
820 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000821 } else {
822 std::cerr << "Unhandled global: " << *V << "\n";
823 abort();
824 }
825 }
826 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000827 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000828 }
829}
830
Misha Brukman3de36f52003-05-27 20:07:58 +0000831#include "SparcV9CodeEmitter.inc"