blob: e741d75cc58ed372decee5f938a39cc7bff09799 [file] [log] [blame]
Chris Lattner556d89d2003-08-01 22:19:03 +00001//===-- SparcV9CodeEmitter.cpp --------------------------------------------===//
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00002//
Brian Gaeke42960882003-10-13 19:51:20 +00003// SPARC-specific backend for emitting machine code to memory.
4//
5// This module also contains the code for lazily resolving the targets
6// of call instructions, including the callback used to redirect calls
7// to functions for which the code has not yet been generated into the
8// JIT compiler.
9//
10// This file #includes SparcV9CodeEmitter.inc, which contains the code
11// for getBinaryCodeForInstr(), a method that converts a MachineInstr
12// into the corresponding binary machine code word.
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000013//
14//===----------------------------------------------------------------------===//
15
Misha Brukmanf86aaa82003-06-02 04:12:39 +000016#include "llvm/Constants.h"
17#include "llvm/Function.h"
18#include "llvm/GlobalVariable.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000019#include "llvm/PassManager.h"
20#include "llvm/CodeGen/MachineCodeEmitter.h"
Misha Brukmana2196c12003-06-04 20:01:13 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000022#include "llvm/CodeGen/MachineFunctionInfo.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000025#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000026#include "llvm/Target/TargetData.h"
Chris Lattner556d89d2003-08-01 22:19:03 +000027#include "Support/Debug.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000028#include "Support/hash_set"
Misha Brukman103f0c32003-09-05 22:59:31 +000029#include "Support/Statistic.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000030#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000031#include "SparcV9CodeEmitter.h"
Misha Brukmancfd67c92003-08-29 04:22:54 +000032#include "Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000033
Misha Brukman103f0c32003-09-05 22:59:31 +000034namespace {
35 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
36 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
37 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
38}
39
Brian Gaekee69f7272003-08-14 06:04:59 +000040bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000041 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000042 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000043 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000044 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000045 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000046 return false;
47}
48
Misha Brukmanf86aaa82003-06-02 04:12:39 +000049namespace {
50 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000051 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000052 MachineCodeEmitter &MCE;
53
Misha Brukman0897c602003-08-06 16:20:22 +000054 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
55 /// lazily resolved.
56 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000057 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000058
Misha Brukman0897c602003-08-06 16:20:22 +000059 /// LazyResolverMap - Keep track of the lazy resolver created for a
60 /// particular function so that we can reuse them if necessary.
61 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000062 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000063
64 public:
65 enum CallType { ShortCall, FarCall };
66
67 private:
68 /// We need to keep track of whether we used a simple call or a far call
69 /// (many instructions) in sequence. This means we need to keep track of
70 /// what type of stub we generate.
71 static std::map<uint64_t, CallType> LazyCallFlavor;
72
Misha Brukmanf86aaa82003-06-02 04:12:39 +000073 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000074 JITResolver(SparcV9CodeEmitter &V9,
75 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
76 uint64_t getLazyResolver(Function *F);
77 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000078 void deleteFunctionReference(uint64_t Address);
79 void addCallFlavor(uint64_t Address, CallType Flavor) {
80 LazyCallFlavor[Address] = Flavor;
81 }
Misha Brukmana2196c12003-06-04 20:01:13 +000082
83 // Utility functions for accessing data from static callback
84 uint64_t getCurrentPCValue() {
85 return MCE.getCurrentPCValue();
86 }
87 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
88 return SparcV9.getBinaryCodeForInstr(MI);
89 }
90
Misha Brukmanf47d9c22003-06-05 20:52:06 +000091 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
92
Misha Brukmanf86aaa82003-06-02 04:12:39 +000093 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000094 uint64_t emitStubForFunction(Function *F);
Misha Brukman103f0c32003-09-05 22:59:31 +000095 static void SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
96 uint64_t &FPRS, uint64_t &CCR);
97 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
98 uint64_t &FPRS, uint64_t &CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +000099 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +0000100 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000101
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000102 };
103
104 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +0000105 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000106}
107
108/// addFunctionReference - This method is called when we need to emit the
109/// address of a function that has not yet been emitted, so we don't know the
110/// address. Instead, we emit a call to the CompilationCallback method, and
111/// keep track of where we are.
112///
Misha Brukmana2196c12003-06-04 20:01:13 +0000113uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000114 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000115 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000116}
117
Misha Brukman0897c602003-08-06 16:20:22 +0000118/// deleteFunctionReference - If we are emitting a far call, we already added a
119/// reference to the function, but it is now incorrect, since the address to the
120/// JIT resolver is too far away to be a simple call instruction. This is used
121/// to remove the address from the map.
122///
123void JITResolver::deleteFunctionReference(uint64_t Address) {
124 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
125 assert(I != LazyCodeGenMap.end() && "Not in map!");
126 LazyCodeGenMap.erase(I);
127}
128
Misha Brukmana2196c12003-06-04 20:01:13 +0000129uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
130 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000131 assert(I != LazyCodeGenMap.end() && "Not in map!");
132 Function *F = I->second;
133 LazyCodeGenMap.erase(I);
134 return MCE.forceCompilationOf(F);
135}
136
Misha Brukmana2196c12003-06-04 20:01:13 +0000137uint64_t JITResolver::getLazyResolver(Function *F) {
138 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000139 if (I != LazyResolverMap.end() && I->first == F) return I->second;
140
141//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
142
Misha Brukmana2196c12003-06-04 20:01:13 +0000143 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000144 LazyResolverMap.insert(I, std::make_pair(F, Stub));
145 return Stub;
146}
147
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000148uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
149
Misha Brukmanfad49292003-08-15 00:26:50 +0000150 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000151 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
152 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000153
Misha Brukman0897c602003-08-06 16:20:22 +0000154 MachineInstr* BinaryCode[] = {
155 //
Misha Brukman0870e972003-08-06 22:19:18 +0000156 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000157 //
Misha Brukman0870e972003-08-06 22:19:18 +0000158 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
159 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
160 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
161 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
162 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
163 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
164 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
165 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000166 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000167 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
168 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
169 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
170 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
171 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
172 // nop ;; delay slot
173 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000174 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000175
Misha Brukman0897c602003-08-06 16:20:22 +0000176 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
177 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
178 delete BinaryCode[i];
179 Addr += 4;
180 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000181
182 return Addr;
183}
184
Misha Brukman103f0c32003-09-05 22:59:31 +0000185void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
186 uint64_t &FPRS, uint64_t &CCR) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000187#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000188
Misha Brukmancfd67c92003-08-29 04:22:54 +0000189#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000190 __asm__ __volatile__ (// Save condition-code registers
191 "stx %%fsr, %0;\n\t"
192 "rd %%fprs, %1;\n\t"
193 "rd %%ccr, %2;\n\t"
194 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000195#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000196
197 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000198 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
199 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
200 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
201 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
202 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
203 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
204 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
205 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
206 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
207 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
208 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
209 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
210 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
211 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
212 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
213 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
214 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000215
Misha Brukmancfd67c92003-08-29 04:22:54 +0000216 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000217 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000218 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000219 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
220 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
221 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
222 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
223 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
224 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000225 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
226 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
227 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
228 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
229 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
230 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
231 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
232 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000233#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000234}
Misha Brukmanfad49292003-08-15 00:26:50 +0000235
Misha Brukmanfad49292003-08-15 00:26:50 +0000236
Misha Brukman103f0c32003-09-05 22:59:31 +0000237void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
238 uint64_t &FPRS, uint64_t &CCR)
239{
Misha Brukmanfad49292003-08-15 00:26:50 +0000240#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000241
Misha Brukmancfd67c92003-08-29 04:22:54 +0000242#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000243 __asm__ __volatile__ (// Restore condition-code registers
244 "ldx %0, %%fsr;\n\t"
245 "wr %1, 0, %%fprs;\n\t"
246 "wr %2, 0, %%ccr;\n\t"
247 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000248#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000249
250 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000251 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
252 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
253 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
254 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
255 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
256 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
257 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
258 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
259 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
260 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
261 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
262 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
263 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
264 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
265 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
266 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
267 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000268
Misha Brukmancfd67c92003-08-29 04:22:54 +0000269 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000270 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
271 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
272 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
273 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
274 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
275 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
276 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
277 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000278 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
279 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
280 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
281 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
282 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
283 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
284 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
285 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000286#endif
287}
288
Misha Brukmancfd67c92003-08-29 04:22:54 +0000289void JITResolver::CompilationCallback() {
290 // Local space to save double registers
291 uint64_t DoubleFP[32];
Misha Brukman103f0c32003-09-05 22:59:31 +0000292 uint64_t FSR, FPRS, CCR;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000293
Misha Brukman103f0c32003-09-05 22:59:31 +0000294 SaveRegisters(DoubleFP, FSR, FPRS, CCR);
295 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000296
297 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000298 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000299 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000300 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000301 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000302#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000303 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
304 DEBUG(std::cerr << "Read i7 (return addr) = "
305 << std::hex << returnAddr << ", value: "
306 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000307#endif
308
Misha Brukman103f0c32003-09-05 22:59:31 +0000309 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
310 // trampoline function stub!!
311 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
312 int64_t OrigTarget = (Target-CameFrom1) >> 2;
313 if ((OrigCallInst & (1 << 30)) &&
314 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
315 {
316 // The original call instruction was CALL <immed>, which means we can
317 // overwrite it directly, since the offset will fit into 30 bits
318 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
319 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
320 delete C;
321 ++OverwrittenCalls;
322 } else {
323 ++UnmodifiedCalls;
324 }
325
Misha Brukman0897c602003-08-06 16:20:22 +0000326 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000327 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000328
Misha Brukman0897c602003-08-06 16:20:22 +0000329 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000330
Misha Brukman0897c602003-08-06 16:20:22 +0000331 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000332 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000333 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000334 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000335 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000336
337 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
338 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000339 // disables frame pointer elimination currently, even if it's dead. This is
340 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000341 alloca(42+Offset);
342 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000343
344 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000345 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000346 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
347 delete SV;
348 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000349 if (CodeInMem != SaveInst) {
350 std::cerr << "About to overwrite smthg not a save instr!";
351 abort();
352 }
Misha Brukman103f0c32003-09-05 22:59:31 +0000353 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << Target << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000354
Misha Brukman103f0c32003-09-05 22:59:31 +0000355 // If the target function is close enough to fit into the 19bit disp of
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000356 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman103f0c32003-09-05 22:59:31 +0000357 int64_t BranchTarget = (Target-CodeBegin) >> 2;
358 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
359 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
360 } else {
361 // ba <target>
362 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
363 *((unsigned*)(intptr_t)CodeBegin) =
364 TheJITResolver->getBinaryCodeForInstr(*I);
365 CodeBegin += 4;
366 delete I;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000367
Misha Brukman103f0c32003-09-05 22:59:31 +0000368 // nop
369 I = BuildMI(V9::NOP, 0);
370 *((unsigned*)(intptr_t)CodeBegin) =
371 TheJITResolver->getBinaryCodeForInstr(*I);
372 delete I;
373 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000374
Misha Brukman103f0c32003-09-05 22:59:31 +0000375 RestoreRegisters(DoubleFP, FSR, FPRS, CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000376
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000377 // Change the return address to re-execute the restore, then the jump.
378 // However, we can't just modify %i7 here, because we return to the function
379 // that will restore the floating-point registers for us. Thus, we just return
380 // the value we want it to be, and the parent will take care of setting %i7
381 // correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000382 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000383 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000384#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
385 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
386#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000387}
388
389/// emitStubForFunction - This method is used by the JIT when it needs to emit
390/// the address of a function for a function whose code has not yet been
391/// generated. In order to do this, it generates a stub which jumps to the lazy
392/// function compiler, which will eventually get fixed to call the function
393/// directly.
394///
Misha Brukmana2196c12003-06-04 20:01:13 +0000395uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000396 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000397
Misha Brukman8f122222003-06-06 00:26:11 +0000398 DEBUG(std::cerr << "Emitting stub at addr: 0x"
399 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000400
Misha Brukman0897c602003-08-06 16:20:22 +0000401 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
402
403 // restore %g0, 0, %g0
404 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
405 .addMReg(g0, MOTy::Def);
406 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
407 delete R;
408
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000409 // save %sp, -192, %sp
410 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
411 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
412 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000413
414 int64_t CurrPC = MCE.getCurrentPCValue();
415 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
416 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000417 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
418 // Since this is a far call, the actual address of the call is shifted
419 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000420 deleteFunctionReference(CurrPC);
421 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000422 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000423 // call CallTarget ;; invoke the callback
424 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
425 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
426 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000427
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000428 // nop ;; call delay slot
429 MachineInstr *Nop = BuildMI(V9::NOP, 0);
430 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
431 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000432
433 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000434 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000435
436 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000437 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000438}
439
440
Misha Brukmana2196c12003-06-04 20:01:13 +0000441SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
442 MachineCodeEmitter &M): TM(tm), MCE(M)
443{
444 TheJITResolver = new JITResolver(*this, M);
445}
446
447SparcV9CodeEmitter::~SparcV9CodeEmitter() {
448 delete TheJITResolver;
449}
450
451void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000452 // Output the constant in big endian byte order...
453 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000454 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000455 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000456 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000457 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000458}
459
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000460unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000461SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000462 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000463 const TargetRegInfo &RI = TM.getRegInfo();
464 unsigned regClass, regType = RI.getRegType(fakeReg);
465 // At least map fakeReg into its class
466 fakeReg = RI.getClassRegNum(fakeReg, regClass);
467
Misha Brukman9cedd432003-07-03 18:36:47 +0000468 switch (regClass) {
469 case UltraSparcRegInfo::IntRegClassID: {
470 // Sparc manual, p31
471 static const unsigned IntRegMap[] = {
472 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
473 8, 9, 10, 11, 12, 13, 15,
474 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
475 16, 17, 18, 19, 20, 21, 22, 23,
476 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
477 24, 25, 26, 27, 28, 29, 30, 31,
478 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
479 0, 1, 2, 3, 4, 5, 6, 7,
480 // "o6"
481 14
482 };
483
484 return IntRegMap[fakeReg];
485 break;
486 }
487 case UltraSparcRegInfo::FloatRegClassID: {
488 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000489 if (regType == UltraSparcRegInfo::FPSingleRegType) {
490 // only numbered 0-31, hence can already fit into 5 bits (and 6)
491 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
492 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000493 // FIXME: This assumes that we only have 5-bit register fields!
Misha Brukman173e2502003-07-14 23:26:03 +0000494 // From Sparc Manual, page 40.
495 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
496 fakeReg |= (fakeReg >> 5) & 1;
497 fakeReg &= 0x1f;
498 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
499 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000500 return fakeReg;
501 }
502 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000503 /* xcc, icc, ccr */
504 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000505
Misha Brukmandfbfc572003-07-16 20:30:40 +0000506 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
507 && "CC register out of bounds for IntCCReg map");
508 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
509 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000510 }
511 case UltraSparcRegInfo::FloatCCRegClassID: {
512 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
513 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
514 return fakeReg;
515 }
516 default:
517 assert(0 && "Invalid unified register number in getRegType");
518 return fakeReg;
519 }
520}
521
522
Misha Brukman07d45162003-07-15 19:09:43 +0000523// WARNING: if the call used the delay slot to do meaningful work, that's not
524// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000525inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000526 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000527 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
528 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000529
Misha Brukman0897c602003-08-06 16:20:22 +0000530 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000531 //
Misha Brukman0870e972003-08-06 22:19:18 +0000532 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000533 //
Misha Brukman0870e972003-08-06 22:19:18 +0000534 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
535 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
536 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
537 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000538 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000539 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
540 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
541 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000542 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000543 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
544 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
545 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000546 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000547 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000548 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000549 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000550 };
Misha Brukman07d45162003-07-15 19:09:43 +0000551
Misha Brukman0897c602003-08-06 16:20:22 +0000552 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
553 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000554 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000555 uint64_t CurrPC = MCE.getCurrentPCValue();
556 TheJITResolver->addFunctionReference(CurrPC, F);
557 // Remember that this is a far call, to subtract appropriate offset later
558 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
559 }
Misha Brukman07d45162003-07-15 19:09:43 +0000560
Misha Brukman0897c602003-08-06 16:20:22 +0000561 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
562 delete BinaryCode[i];
563 }
Misha Brukman07d45162003-07-15 19:09:43 +0000564}
565
566
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000567int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
568 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000569 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
570 // or things that get fixed up later by the JIT.
571
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000572 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000573 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000574 abort();
575 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000576 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000577 Value *V = MO.getVRegValue();
578 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000579 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000580 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000581 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000582 } else if (const Constant *C = dyn_cast<Constant>(V)) {
583 if (ConstantMap.find(C) != ConstantMap.end()) {
584 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000585 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000586 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000587 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000588 abort();
589 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000590 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
591 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000592 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000593 // external function calls, etc.?
594 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000595 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000596 if (F->isExternal()) {
597 // Sparc backend broken: this MO should be `ExternalSymbol'
598 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
599 } else {
600 rv = (int64_t)MCE.getGlobalValueAddress(F);
601 }
602 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000603 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000604 // Function has not yet been code generated!
605 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
606 // Delayed resolution...
607 rv = TheJITResolver->getLazyResolver(F);
608 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000609 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000610 }
611 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000612 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000613 if (rv == 0) {
614 if (Constant *C = ConstantPointerRef::get(GV)) {
615 if (ConstantMap.find(C) != ConstantMap.end()) {
616 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
617 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000618 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000619 << ", " << *V << " not found in ConstantMap!\n";
620 abort();
621 }
622 }
623 }
Misha Brukman0870e972003-08-06 22:19:18 +0000624 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000625 }
626 // The real target of the call is Addr = PC + (rv * 4)
627 // So undo that: give the instruction (Addr - PC) / 4
628 if (MI.getOpcode() == V9::CALL) {
629 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000630 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000631 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000632 int64_t CallInstTarget = (rv - CurrPC) >> 2;
633 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
634 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000635 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000636 // make an indirect jump-and-link
637 emitFarCall(rv);
638 // this invalidates the instruction so that the call with an incorrect
639 // address will not be emitted
640 rv = 0;
641 } else {
642 // The call fits into 30 bits, so just return the corrected address
643 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000644 }
Misha Brukman8f122222003-06-06 00:26:11 +0000645 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000646 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000647 } else {
648 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
649 abort();
650 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000651 } else if (MO.isPhysicalRegister() ||
652 MO.getType() == MachineOperand::MO_CCRegister)
653 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000654 // This is necessary because the Sparc backend doesn't actually lay out
655 // registers in the real fashion -- it skips those that it chooses not to
656 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000657 unsigned fakeReg = MO.getAllocatedRegNum();
658 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
659 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000660 << realRegByClass << " (LLC: "
661 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000662 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000663 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000664 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000665 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000666 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000667 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000668 rv = (int64_t)
669 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000670 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000671 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000672 // Duplicate code of the above case for VirtualRegister, BasicBlock...
673 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000674 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000675 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000676 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000677 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000678 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000679 // Sparc backend doesn't generate this (yet...)
680 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
681 abort();
682 } else if (MO.isFrameIndex()) {
683 // Sparc backend doesn't generate this (yet...)
684 int FrameIndex = MO.getFrameIndex();
685 std::cerr << "ERROR: Frame index unhandled.\n";
686 abort();
687 } else if (MO.isConstantPoolIndex()) {
688 // Sparc backend doesn't generate this (yet...)
689 std::cerr << "ERROR: Constant Pool index unhandled.\n";
690 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000691 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000692 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000693 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000694 }
695
696 // Finally, deal with the various bitfield-extracting functions that
697 // are used in SPARC assembly. (Some of these make no sense in combination
698 // with some of the above; we'll trust that the instruction selector
699 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000700 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000701 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000702 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000703 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000704 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000705 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000706 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000707 return rv >> 42;
708 } else { // (unadorned) val
709 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000710 }
711}
712
713unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
714 Val >>= bit;
715 return (Val & 1);
716}
717
Misha Brukman3de36f52003-05-27 20:07:58 +0000718bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000719 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000720 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000721 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000722 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000723
Misha Brukmana2196c12003-06-04 20:01:13 +0000724 // The Sparc backend does not use MachineConstantPool;
725 // instead, it has its own constant pool implementation.
726 // We create a new MachineConstantPool here to be compatible with the emitter.
727 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000728 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
729 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
730 E = pool.end(); I != E; ++I)
731 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000732 Constant *C = (Constant*)*I;
733 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000734 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000735 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000736 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000737 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000738
Misha Brukman3de36f52003-05-27 20:07:58 +0000739 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
740 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000741 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000742
Misha Brukman9cedd432003-07-03 18:36:47 +0000743 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000744 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000745
746 // Resolve branches to BasicBlocks for the entire function
747 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
748 long Location = BBLocations[BBRefs[i].first];
749 unsigned *Ref = BBRefs[i].second.first;
750 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000751 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
752 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000753 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
754 MachineOperand &op = MI->getOperand(ii);
755 if (op.isPCRelativeDisp()) {
756 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000757 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000758 // Location is the target of the branch
759 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000760 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000761 // Save the flags.
762 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
763 if (op.opLoBits32()) { loBits32=true; }
764 if (op.opHiBits32()) { hiBits32=true; }
765 if (op.opLoBits64()) { loBits64=true; }
766 if (op.opHiBits64()) { hiBits64=true; }
767 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
768 branchTarget);
769 if (loBits32) { MI->setOperandLo32(ii); }
770 else if (hiBits32) { MI->setOperandHi32(ii); }
771 else if (loBits64) { MI->setOperandLo64(ii); }
772 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000773 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000774 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
775 *Ref = fixedInstr;
776 break;
777 }
778 }
779 }
780 BBRefs.clear();
781 BBLocations.clear();
782
Misha Brukman3de36f52003-05-27 20:07:58 +0000783 return false;
784}
785
786void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000787 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000788 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000789 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
790 unsigned binCode = getBinaryCodeForInstr(**I);
791 if (binCode == (1 << 30)) {
792 // this is an invalid call: the addr is out of bounds. that means a code
793 // sequence has already been emitted, and this is a no-op
794 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
795 } else {
796 emitWord(binCode);
797 }
798 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000799}
800
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000801void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
802 bool isPCRelative)
803{
804 if (isPCRelative) { // must be a call, this is a major hack!
805 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000806 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
807 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000808 // The real target of the call is Addr = PC + (target * 4)
809 // CurByte is the PC, Addr we just received
810 return (void*) (((long)Addr - (long)CurByte) >> 2);
811 } else {
812 if (Function *F = dyn_cast<Function>(V)) {
813 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000814 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000815 cast<Function>(V));
816 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000817 return
818 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000819
820 } else if (Constant *C = ConstantPointerRef::get(V)) {
821 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000822 return (void*)
823 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000824 } else {
825 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
826 << ", " << *V << " not found in ConstantMap!\n";
827 abort();
828 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000829 } else {
830 std::cerr << "Unhandled global: " << *V << "\n";
831 abort();
832 }
833 }
834 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000835 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000836 }
837}
838
Misha Brukman3de36f52003-05-27 20:07:58 +0000839#include "SparcV9CodeEmitter.inc"