blob: 819b8627a412ccd95b782e8e26774510802b5f6a [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 Brukmancf00c4a2003-10-10 17:57:28 +0000369 // Change the return address to re-execute the restore, then the jump.
370 // However, we can't just modify %i7 here, because we return to the function
371 // that will restore the floating-point registers for us. Thus, we just return
372 // the value we want it to be, and the parent will take care of setting %i7
373 // correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000374 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000375 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000376#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
377 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
378#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000379}
380
381/// emitStubForFunction - This method is used by the JIT when it needs to emit
382/// the address of a function for a function whose code has not yet been
383/// generated. In order to do this, it generates a stub which jumps to the lazy
384/// function compiler, which will eventually get fixed to call the function
385/// directly.
386///
Misha Brukmana2196c12003-06-04 20:01:13 +0000387uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000388 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000389
Misha Brukman8f122222003-06-06 00:26:11 +0000390 DEBUG(std::cerr << "Emitting stub at addr: 0x"
391 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000392
Misha Brukman0897c602003-08-06 16:20:22 +0000393 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
394
395 // restore %g0, 0, %g0
396 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
397 .addMReg(g0, MOTy::Def);
398 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
399 delete R;
400
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000401 // save %sp, -192, %sp
402 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
403 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
404 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000405
406 int64_t CurrPC = MCE.getCurrentPCValue();
407 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
408 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000409 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
410 // Since this is a far call, the actual address of the call is shifted
411 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000412 deleteFunctionReference(CurrPC);
413 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000414 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000415 // call CallTarget ;; invoke the callback
416 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
417 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
418 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000419
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000420 // nop ;; call delay slot
421 MachineInstr *Nop = BuildMI(V9::NOP, 0);
422 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
423 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000424
425 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000426 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000427
428 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000429 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000430}
431
432
Misha Brukmana2196c12003-06-04 20:01:13 +0000433SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
434 MachineCodeEmitter &M): TM(tm), MCE(M)
435{
436 TheJITResolver = new JITResolver(*this, M);
437}
438
439SparcV9CodeEmitter::~SparcV9CodeEmitter() {
440 delete TheJITResolver;
441}
442
443void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000444 // Output the constant in big endian byte order...
Brian Gaekef4785562003-09-30 17:49:41 +0000445 ++WordsEmitted;
Misha Brukman3de36f52003-05-27 20:07:58 +0000446 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000447 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000448 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000449 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000450 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000451}
452
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000453unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000454SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000455 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000456 const TargetRegInfo &RI = TM.getRegInfo();
457 unsigned regClass, regType = RI.getRegType(fakeReg);
458 // At least map fakeReg into its class
459 fakeReg = RI.getClassRegNum(fakeReg, regClass);
460
Misha Brukman9cedd432003-07-03 18:36:47 +0000461 switch (regClass) {
462 case UltraSparcRegInfo::IntRegClassID: {
463 // Sparc manual, p31
464 static const unsigned IntRegMap[] = {
465 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
466 8, 9, 10, 11, 12, 13, 15,
467 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
468 16, 17, 18, 19, 20, 21, 22, 23,
469 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
470 24, 25, 26, 27, 28, 29, 30, 31,
471 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
472 0, 1, 2, 3, 4, 5, 6, 7,
473 // "o6"
474 14
475 };
476
477 return IntRegMap[fakeReg];
478 break;
479 }
480 case UltraSparcRegInfo::FloatRegClassID: {
481 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000482 if (regType == UltraSparcRegInfo::FPSingleRegType) {
483 // only numbered 0-31, hence can already fit into 5 bits (and 6)
484 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
485 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000486 // FIXME: This assumes that we only have 5-bit register fields!
Misha Brukman173e2502003-07-14 23:26:03 +0000487 // From Sparc Manual, page 40.
488 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
489 fakeReg |= (fakeReg >> 5) & 1;
490 fakeReg &= 0x1f;
491 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
492 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000493 return fakeReg;
494 }
495 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000496 /* xcc, icc, ccr */
497 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000498
Misha Brukmandfbfc572003-07-16 20:30:40 +0000499 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
500 && "CC register out of bounds for IntCCReg map");
501 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
502 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000503 }
504 case UltraSparcRegInfo::FloatCCRegClassID: {
505 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
506 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
507 return fakeReg;
508 }
509 default:
510 assert(0 && "Invalid unified register number in getRegType");
511 return fakeReg;
512 }
513}
514
515
Misha Brukman07d45162003-07-15 19:09:43 +0000516// WARNING: if the call used the delay slot to do meaningful work, that's not
517// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000518inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000519 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000520 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
521 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000522
Misha Brukman0897c602003-08-06 16:20:22 +0000523 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000524 //
Misha Brukman0870e972003-08-06 22:19:18 +0000525 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000526 //
Misha Brukman0870e972003-08-06 22:19:18 +0000527 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
528 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
529 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
530 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000531 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000532 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
533 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
534 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000535 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000536 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
537 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
538 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000539 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000540 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000541 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000542 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000543 };
Misha Brukman07d45162003-07-15 19:09:43 +0000544
Misha Brukman0897c602003-08-06 16:20:22 +0000545 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
546 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000547 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000548 uint64_t CurrPC = MCE.getCurrentPCValue();
549 TheJITResolver->addFunctionReference(CurrPC, F);
550 // Remember that this is a far call, to subtract appropriate offset later
551 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
552 }
Misha Brukman07d45162003-07-15 19:09:43 +0000553
Misha Brukman0897c602003-08-06 16:20:22 +0000554 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
555 delete BinaryCode[i];
556 }
Misha Brukman07d45162003-07-15 19:09:43 +0000557}
558
559
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000560int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
561 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000562 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
563 // or things that get fixed up later by the JIT.
564
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000565 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000566 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000567 abort();
568 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000569 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000570 Value *V = MO.getVRegValue();
571 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000572 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000573 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000574 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000575 } else if (const Constant *C = dyn_cast<Constant>(V)) {
576 if (ConstantMap.find(C) != ConstantMap.end()) {
577 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000578 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000579 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000580 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000581 abort();
582 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000583 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
584 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000585 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000586 // external function calls, etc.?
587 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000588 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000589 if (F->isExternal()) {
590 // Sparc backend broken: this MO should be `ExternalSymbol'
591 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
592 } else {
593 rv = (int64_t)MCE.getGlobalValueAddress(F);
594 }
595 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000596 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000597 // Function has not yet been code generated!
598 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
599 // Delayed resolution...
600 rv = TheJITResolver->getLazyResolver(F);
601 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000602 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000603 }
604 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000605 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000606 if (rv == 0) {
607 if (Constant *C = ConstantPointerRef::get(GV)) {
608 if (ConstantMap.find(C) != ConstantMap.end()) {
609 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
610 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000611 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000612 << ", " << *V << " not found in ConstantMap!\n";
613 abort();
614 }
615 }
616 }
Misha Brukman0870e972003-08-06 22:19:18 +0000617 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000618 }
619 // The real target of the call is Addr = PC + (rv * 4)
620 // So undo that: give the instruction (Addr - PC) / 4
621 if (MI.getOpcode() == V9::CALL) {
622 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000623 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000624 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000625 int64_t CallInstTarget = (rv - CurrPC) >> 2;
626 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
627 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000628 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000629 // make an indirect jump-and-link
630 emitFarCall(rv);
631 // this invalidates the instruction so that the call with an incorrect
632 // address will not be emitted
633 rv = 0;
634 } else {
635 // The call fits into 30 bits, so just return the corrected address
636 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000637 }
Misha Brukman8f122222003-06-06 00:26:11 +0000638 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000639 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000640 } else {
641 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
642 abort();
643 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000644 } else if (MO.isPhysicalRegister() ||
645 MO.getType() == MachineOperand::MO_CCRegister)
646 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000647 // This is necessary because the Sparc backend doesn't actually lay out
648 // registers in the real fashion -- it skips those that it chooses not to
649 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000650 unsigned fakeReg = MO.getAllocatedRegNum();
651 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
652 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000653 << realRegByClass << " (LLC: "
654 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000655 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000656 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000657 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000658 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000659 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000660 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000661 rv = (int64_t)
662 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000663 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000664 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000665 // Duplicate code of the above case for VirtualRegister, BasicBlock...
666 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000667 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000668 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000669 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000670 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000671 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000672 // Sparc backend doesn't generate this (yet...)
673 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
674 abort();
675 } else if (MO.isFrameIndex()) {
676 // Sparc backend doesn't generate this (yet...)
677 int FrameIndex = MO.getFrameIndex();
678 std::cerr << "ERROR: Frame index unhandled.\n";
679 abort();
680 } else if (MO.isConstantPoolIndex()) {
681 // Sparc backend doesn't generate this (yet...)
682 std::cerr << "ERROR: Constant Pool index unhandled.\n";
683 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000684 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000685 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000686 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000687 }
688
689 // Finally, deal with the various bitfield-extracting functions that
690 // are used in SPARC assembly. (Some of these make no sense in combination
691 // with some of the above; we'll trust that the instruction selector
692 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000693 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000694 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000695 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000696 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000697 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000698 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000699 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000700 return rv >> 42;
701 } else { // (unadorned) val
702 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000703 }
704}
705
706unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
707 Val >>= bit;
708 return (Val & 1);
709}
710
Misha Brukman3de36f52003-05-27 20:07:58 +0000711bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000712 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000713 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000714 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000715 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000716
Misha Brukmana2196c12003-06-04 20:01:13 +0000717 // The Sparc backend does not use MachineConstantPool;
718 // instead, it has its own constant pool implementation.
719 // We create a new MachineConstantPool here to be compatible with the emitter.
720 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000721 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
722 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
723 E = pool.end(); I != E; ++I)
724 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000725 Constant *C = (Constant*)*I;
726 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000727 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000728 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000729 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000730 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000731
Misha Brukman3de36f52003-05-27 20:07:58 +0000732 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
733 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000734 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000735
Misha Brukman9cedd432003-07-03 18:36:47 +0000736 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000737 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000738
739 // Resolve branches to BasicBlocks for the entire function
740 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
741 long Location = BBLocations[BBRefs[i].first];
742 unsigned *Ref = BBRefs[i].second.first;
743 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000744 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
745 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000746 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
747 MachineOperand &op = MI->getOperand(ii);
748 if (op.isPCRelativeDisp()) {
749 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000750 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000751 // Location is the target of the branch
752 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000753 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000754 // Save the flags.
755 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
756 if (op.opLoBits32()) { loBits32=true; }
757 if (op.opHiBits32()) { hiBits32=true; }
758 if (op.opLoBits64()) { loBits64=true; }
759 if (op.opHiBits64()) { hiBits64=true; }
760 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
761 branchTarget);
762 if (loBits32) { MI->setOperandLo32(ii); }
763 else if (hiBits32) { MI->setOperandHi32(ii); }
764 else if (loBits64) { MI->setOperandLo64(ii); }
765 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000766 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000767 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
768 *Ref = fixedInstr;
769 break;
770 }
771 }
772 }
773 BBRefs.clear();
774 BBLocations.clear();
775
Misha Brukman3de36f52003-05-27 20:07:58 +0000776 return false;
777}
778
779void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000780 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000781 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000782 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
783 unsigned binCode = getBinaryCodeForInstr(**I);
784 if (binCode == (1 << 30)) {
785 // this is an invalid call: the addr is out of bounds. that means a code
786 // sequence has already been emitted, and this is a no-op
787 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
788 } else {
789 emitWord(binCode);
790 }
791 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000792}
793
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000794void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
795 bool isPCRelative)
796{
797 if (isPCRelative) { // must be a call, this is a major hack!
798 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000799 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
800 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000801 // The real target of the call is Addr = PC + (target * 4)
802 // CurByte is the PC, Addr we just received
803 return (void*) (((long)Addr - (long)CurByte) >> 2);
804 } else {
805 if (Function *F = dyn_cast<Function>(V)) {
806 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000807 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000808 cast<Function>(V));
809 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000810 return
811 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000812
813 } else if (Constant *C = ConstantPointerRef::get(V)) {
814 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000815 return (void*)
816 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000817 } else {
818 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
819 << ", " << *V << " not found in ConstantMap!\n";
820 abort();
821 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000822 } else {
823 std::cerr << "Unhandled global: " << *V << "\n";
824 abort();
825 }
826 }
827 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000828 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000829 }
830}
831
Misha Brukman3de36f52003-05-27 20:07:58 +0000832#include "SparcV9CodeEmitter.inc"