blob: 4118b2b0df254ede84ce22e126cf218d344d0b00 [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
Brian Gaeke4ca7e532003-10-17 18:27:37 +000091 inline void insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
92 void insertJumpAtAddr(int64_t Value, uint64_t &Addr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000093
Misha Brukmanf86aaa82003-06-02 04:12:39 +000094 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000095 uint64_t emitStubForFunction(Function *F);
Misha Brukman103f0c32003-09-05 22:59:31 +000096 static void SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
97 uint64_t &FPRS, uint64_t &CCR);
98 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
99 uint64_t &FPRS, uint64_t &CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000100 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +0000101 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000102
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000103 };
104
105 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +0000106 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000107}
108
109/// addFunctionReference - This method is called when we need to emit the
110/// address of a function that has not yet been emitted, so we don't know the
111/// address. Instead, we emit a call to the CompilationCallback method, and
112/// keep track of where we are.
113///
Misha Brukmana2196c12003-06-04 20:01:13 +0000114uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000115 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000116 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000117}
118
Misha Brukman0897c602003-08-06 16:20:22 +0000119/// deleteFunctionReference - If we are emitting a far call, we already added a
120/// reference to the function, but it is now incorrect, since the address to the
121/// JIT resolver is too far away to be a simple call instruction. This is used
122/// to remove the address from the map.
123///
124void JITResolver::deleteFunctionReference(uint64_t Address) {
125 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
126 assert(I != LazyCodeGenMap.end() && "Not in map!");
127 LazyCodeGenMap.erase(I);
128}
129
Misha Brukmana2196c12003-06-04 20:01:13 +0000130uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
131 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000132 assert(I != LazyCodeGenMap.end() && "Not in map!");
133 Function *F = I->second;
134 LazyCodeGenMap.erase(I);
135 return MCE.forceCompilationOf(F);
136}
137
Misha Brukmana2196c12003-06-04 20:01:13 +0000138uint64_t JITResolver::getLazyResolver(Function *F) {
139 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000140 if (I != LazyResolverMap.end() && I->first == F) return I->second;
141
Misha Brukmana2196c12003-06-04 20:01:13 +0000142 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000143 LazyResolverMap.insert(I, std::make_pair(F, Stub));
144 return Stub;
145}
146
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000147void JITResolver::insertJumpAtAddr(int64_t JumpTarget, uint64_t &Addr) {
148 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << JumpTarget << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000149
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000150 // If the target function is close enough to fit into the 19bit disp of
151 // BA, we should use this version, as it's much cheaper to generate.
152 int64_t BranchTarget = (JumpTarget-Addr) >> 2;
153 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
154 TheJITResolver->insertFarJumpAtAddr(JumpTarget, Addr);
155 } else {
156 // ba <target>
157 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
158 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
159 Addr += 4;
160 delete I;
161
162 // nop
163 I = BuildMI(V9::NOP, 0);
164 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
165 delete I;
166 }
167}
168
169void JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000170 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000171 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
172 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000173
Misha Brukman0897c602003-08-06 16:20:22 +0000174 MachineInstr* BinaryCode[] = {
175 //
Misha Brukman0870e972003-08-06 22:19:18 +0000176 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000177 //
Misha Brukman0870e972003-08-06 22:19:18 +0000178 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
179 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
180 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
181 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
182 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
183 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
184 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
185 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000186 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000187 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
188 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
189 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
190 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
191 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
192 // nop ;; delay slot
193 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000194 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000195
Misha Brukman0897c602003-08-06 16:20:22 +0000196 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
197 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
198 delete BinaryCode[i];
199 Addr += 4;
200 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000201}
202
Misha Brukman103f0c32003-09-05 22:59:31 +0000203void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
204 uint64_t &FPRS, uint64_t &CCR) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000205#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000206
Misha Brukmancfd67c92003-08-29 04:22:54 +0000207#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000208 __asm__ __volatile__ (// Save condition-code registers
209 "stx %%fsr, %0;\n\t"
210 "rd %%fprs, %1;\n\t"
211 "rd %%ccr, %2;\n\t"
212 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000213#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000214
215 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000216 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
217 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
218 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
219 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
220 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
221 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
222 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
223 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
224 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
225 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
226 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
227 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
228 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
229 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
230 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
231 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
232 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000233
Misha Brukmancfd67c92003-08-29 04:22:54 +0000234 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000235 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000236 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000237 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
238 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
239 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
240 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
241 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
242 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000243 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
244 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
245 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
246 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
247 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
248 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
249 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
250 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000251#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000252}
Misha Brukmanfad49292003-08-15 00:26:50 +0000253
Misha Brukmanfad49292003-08-15 00:26:50 +0000254
Misha Brukman103f0c32003-09-05 22:59:31 +0000255void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
256 uint64_t &FPRS, uint64_t &CCR)
257{
Misha Brukmanfad49292003-08-15 00:26:50 +0000258#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000259
Misha Brukmancfd67c92003-08-29 04:22:54 +0000260#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000261 __asm__ __volatile__ (// Restore condition-code registers
262 "ldx %0, %%fsr;\n\t"
263 "wr %1, 0, %%fprs;\n\t"
264 "wr %2, 0, %%ccr;\n\t"
265 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000266#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000267
268 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000269 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
270 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
271 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
272 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
273 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
274 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
275 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
276 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
277 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
278 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
279 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
280 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
281 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
282 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
283 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
284 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
285 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000286
Misha Brukmancfd67c92003-08-29 04:22:54 +0000287 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000288 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
289 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
290 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
291 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
292 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
293 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
294 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
295 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000296 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
297 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
298 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
299 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
300 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
301 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
302 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
303 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000304#endif
305}
306
Misha Brukmancfd67c92003-08-29 04:22:54 +0000307void JITResolver::CompilationCallback() {
308 // Local space to save double registers
309 uint64_t DoubleFP[32];
Misha Brukman103f0c32003-09-05 22:59:31 +0000310 uint64_t FSR, FPRS, CCR;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000311
Misha Brukman103f0c32003-09-05 22:59:31 +0000312 SaveRegisters(DoubleFP, FSR, FPRS, CCR);
313 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000314
315 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000316 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000317 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000318 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000319 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000320#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000321 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
322 DEBUG(std::cerr << "Read i7 (return addr) = "
323 << std::hex << returnAddr << ", value: "
324 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000325#endif
326
Misha Brukman103f0c32003-09-05 22:59:31 +0000327 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
328 // trampoline function stub!!
329 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
330 int64_t OrigTarget = (Target-CameFrom1) >> 2;
331 if ((OrigCallInst & (1 << 30)) &&
332 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
333 {
334 // The original call instruction was CALL <immed>, which means we can
335 // overwrite it directly, since the offset will fit into 30 bits
336 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
337 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
338 delete C;
339 ++OverwrittenCalls;
340 } else {
341 ++UnmodifiedCalls;
342 }
343
Misha Brukman0897c602003-08-06 16:20:22 +0000344 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000345 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000346
Misha Brukman0897c602003-08-06 16:20:22 +0000347 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000348
Misha Brukman0897c602003-08-06 16:20:22 +0000349 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000350 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000351 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000352 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000353 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000354
355 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
356 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000357 // disables frame pointer elimination currently, even if it's dead. This is
358 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000359 alloca(42+Offset);
360 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000361
362 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000363 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000364 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
365 delete SV;
366 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000367 if (CodeInMem != SaveInst) {
368 std::cerr << "About to overwrite smthg not a save instr!";
369 abort();
370 }
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000371 // Overwrite it
372 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000373
Misha Brukman103f0c32003-09-05 22:59:31 +0000374 RestoreRegisters(DoubleFP, FSR, FPRS, CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000375
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000376 // Change the return address to re-execute the restore, then the jump.
377 // However, we can't just modify %i7 here, because we return to the function
378 // that will restore the floating-point registers for us. Thus, we just return
379 // the value we want it to be, and the parent will take care of setting %i7
380 // correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000381 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000382 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000383#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
384 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
385#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000386}
387
388/// emitStubForFunction - This method is used by the JIT when it needs to emit
389/// the address of a function for a function whose code has not yet been
390/// generated. In order to do this, it generates a stub which jumps to the lazy
391/// function compiler, which will eventually get fixed to call the function
392/// directly.
393///
Misha Brukmana2196c12003-06-04 20:01:13 +0000394uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000395 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000396
Misha Brukman8f122222003-06-06 00:26:11 +0000397 DEBUG(std::cerr << "Emitting stub at addr: 0x"
398 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000399
Misha Brukman0897c602003-08-06 16:20:22 +0000400 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
401
402 // restore %g0, 0, %g0
403 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
404 .addMReg(g0, MOTy::Def);
405 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
406 delete R;
407
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000408 // save %sp, -192, %sp
409 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
410 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
411 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000412
413 int64_t CurrPC = MCE.getCurrentPCValue();
414 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
415 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000416 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
417 // Since this is a far call, the actual address of the call is shifted
418 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000419 deleteFunctionReference(CurrPC);
420 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000421 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000422 // call CallTarget ;; invoke the callback
423 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
424 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
425 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000426
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000427 // nop ;; call delay slot
428 MachineInstr *Nop = BuildMI(V9::NOP, 0);
429 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
430 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000431
432 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000433 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000434
435 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000436 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000437}
438
439
Misha Brukmana2196c12003-06-04 20:01:13 +0000440SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
441 MachineCodeEmitter &M): TM(tm), MCE(M)
442{
443 TheJITResolver = new JITResolver(*this, M);
444}
445
446SparcV9CodeEmitter::~SparcV9CodeEmitter() {
447 delete TheJITResolver;
448}
449
450void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000451 // Output the constant in big endian byte order...
452 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000453 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000454 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000455 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000456 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000457}
458
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000459unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000460SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000461 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000462 const TargetRegInfo &RI = TM.getRegInfo();
463 unsigned regClass, regType = RI.getRegType(fakeReg);
464 // At least map fakeReg into its class
465 fakeReg = RI.getClassRegNum(fakeReg, regClass);
466
Misha Brukman9cedd432003-07-03 18:36:47 +0000467 switch (regClass) {
468 case UltraSparcRegInfo::IntRegClassID: {
469 // Sparc manual, p31
470 static const unsigned IntRegMap[] = {
471 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
472 8, 9, 10, 11, 12, 13, 15,
473 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
474 16, 17, 18, 19, 20, 21, 22, 23,
475 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
476 24, 25, 26, 27, 28, 29, 30, 31,
477 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
478 0, 1, 2, 3, 4, 5, 6, 7,
479 // "o6"
480 14
481 };
482
483 return IntRegMap[fakeReg];
484 break;
485 }
486 case UltraSparcRegInfo::FloatRegClassID: {
487 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000488 if (regType == UltraSparcRegInfo::FPSingleRegType) {
489 // only numbered 0-31, hence can already fit into 5 bits (and 6)
490 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
491 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000492 // FIXME: This assumes that we only have 5-bit register fields!
Misha Brukman173e2502003-07-14 23:26:03 +0000493 // From Sparc Manual, page 40.
494 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
495 fakeReg |= (fakeReg >> 5) & 1;
496 fakeReg &= 0x1f;
497 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
498 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000499 return fakeReg;
500 }
501 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000502 /* xcc, icc, ccr */
503 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000504
Misha Brukmandfbfc572003-07-16 20:30:40 +0000505 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
506 && "CC register out of bounds for IntCCReg map");
507 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
508 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000509 }
510 case UltraSparcRegInfo::FloatCCRegClassID: {
511 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
512 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
513 return fakeReg;
514 }
515 default:
516 assert(0 && "Invalid unified register number in getRegType");
517 return fakeReg;
518 }
519}
520
521
Misha Brukman07d45162003-07-15 19:09:43 +0000522// WARNING: if the call used the delay slot to do meaningful work, that's not
523// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000524inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000525 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000526 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
527 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000528
Misha Brukman0897c602003-08-06 16:20:22 +0000529 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000530 //
Misha Brukman0870e972003-08-06 22:19:18 +0000531 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000532 //
Misha Brukman0870e972003-08-06 22:19:18 +0000533 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
534 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
535 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
536 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000537 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000538 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
539 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
540 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000541 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000542 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
543 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
544 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000545 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000546 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000547 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000548 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000549 };
Misha Brukman07d45162003-07-15 19:09:43 +0000550
Misha Brukman0897c602003-08-06 16:20:22 +0000551 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
552 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000553 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000554 uint64_t CurrPC = MCE.getCurrentPCValue();
555 TheJITResolver->addFunctionReference(CurrPC, F);
556 // Remember that this is a far call, to subtract appropriate offset later
557 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
558 }
Misha Brukman07d45162003-07-15 19:09:43 +0000559
Misha Brukman0897c602003-08-06 16:20:22 +0000560 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
561 delete BinaryCode[i];
562 }
Misha Brukman07d45162003-07-15 19:09:43 +0000563}
564
Brian Gaeke682ce722003-10-20 15:15:17 +0000565void UltraSparc::replaceMachineCodeForFunction (void *Old, void *New) {
Brian Gaeke0522b082003-10-20 17:59:09 +0000566 assert (TheJITResolver &&
567 "Can only call replaceMachineCodeForFunction from within JIT");
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000568 uint64_t Target = (uint64_t)(intptr_t)New;
569 uint64_t CodeBegin = (uint64_t)(intptr_t)Old;
570 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000571}
Misha Brukman07d45162003-07-15 19:09:43 +0000572
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000573int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
574 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000575 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
576 // or things that get fixed up later by the JIT.
577
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000578 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000579 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000580 abort();
581 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000582 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000583 Value *V = MO.getVRegValue();
584 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000585 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000586 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000587 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000588 } else if (const Constant *C = dyn_cast<Constant>(V)) {
589 if (ConstantMap.find(C) != ConstantMap.end()) {
590 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000591 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000592 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000593 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000594 abort();
595 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000596 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
597 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000598 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000599 // external function calls, etc.?
600 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000601 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000602 if (F->isExternal()) {
603 // Sparc backend broken: this MO should be `ExternalSymbol'
604 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
605 } else {
606 rv = (int64_t)MCE.getGlobalValueAddress(F);
607 }
608 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000609 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000610 // Function has not yet been code generated!
611 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
612 // Delayed resolution...
613 rv = TheJITResolver->getLazyResolver(F);
614 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000615 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000616 }
617 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000618 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000619 if (rv == 0) {
620 if (Constant *C = ConstantPointerRef::get(GV)) {
621 if (ConstantMap.find(C) != ConstantMap.end()) {
622 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
623 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000624 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000625 << ", " << *V << " not found in ConstantMap!\n";
626 abort();
627 }
628 }
629 }
Misha Brukman0870e972003-08-06 22:19:18 +0000630 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000631 }
632 // The real target of the call is Addr = PC + (rv * 4)
633 // So undo that: give the instruction (Addr - PC) / 4
634 if (MI.getOpcode() == V9::CALL) {
635 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000636 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000637 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000638 int64_t CallInstTarget = (rv - CurrPC) >> 2;
639 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
640 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000641 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000642 // make an indirect jump-and-link
643 emitFarCall(rv);
644 // this invalidates the instruction so that the call with an incorrect
645 // address will not be emitted
646 rv = 0;
647 } else {
648 // The call fits into 30 bits, so just return the corrected address
649 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000650 }
Misha Brukman8f122222003-06-06 00:26:11 +0000651 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000652 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000653 } else {
654 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
655 abort();
656 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000657 } else if (MO.isPhysicalRegister() ||
658 MO.getType() == MachineOperand::MO_CCRegister)
659 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000660 // This is necessary because the Sparc backend doesn't actually lay out
661 // registers in the real fashion -- it skips those that it chooses not to
662 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000663 unsigned fakeReg = MO.getAllocatedRegNum();
664 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
665 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000666 << realRegByClass << " (LLC: "
667 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000668 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000669 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000670 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000671 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000672 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000673 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000674 rv = (int64_t)
675 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000676 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000677 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000678 // Duplicate code of the above case for VirtualRegister, BasicBlock...
679 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000680 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000681 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000682 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000683 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000684 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000685 // Sparc backend doesn't generate this (yet...)
686 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
687 abort();
688 } else if (MO.isFrameIndex()) {
689 // Sparc backend doesn't generate this (yet...)
690 int FrameIndex = MO.getFrameIndex();
691 std::cerr << "ERROR: Frame index unhandled.\n";
692 abort();
693 } else if (MO.isConstantPoolIndex()) {
694 // Sparc backend doesn't generate this (yet...)
695 std::cerr << "ERROR: Constant Pool index unhandled.\n";
696 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000697 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000698 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000699 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000700 }
701
702 // Finally, deal with the various bitfield-extracting functions that
703 // are used in SPARC assembly. (Some of these make no sense in combination
704 // with some of the above; we'll trust that the instruction selector
705 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000706 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000707 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000708 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000709 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000710 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000711 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000712 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000713 return rv >> 42;
714 } else { // (unadorned) val
715 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000716 }
717}
718
719unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
720 Val >>= bit;
721 return (Val & 1);
722}
723
Misha Brukman3de36f52003-05-27 20:07:58 +0000724bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000725 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000726 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000727 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000728 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000729
Misha Brukmana2196c12003-06-04 20:01:13 +0000730 // The Sparc backend does not use MachineConstantPool;
731 // instead, it has its own constant pool implementation.
732 // We create a new MachineConstantPool here to be compatible with the emitter.
733 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000734 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
735 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
736 E = pool.end(); I != E; ++I)
737 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000738 Constant *C = (Constant*)*I;
739 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000740 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000741 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000742 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000743 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000744
Misha Brukman3de36f52003-05-27 20:07:58 +0000745 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
746 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000747 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000748
Misha Brukman9cedd432003-07-03 18:36:47 +0000749 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000750 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000751
752 // Resolve branches to BasicBlocks for the entire function
753 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
754 long Location = BBLocations[BBRefs[i].first];
755 unsigned *Ref = BBRefs[i].second.first;
756 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000757 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
758 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000759 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
760 MachineOperand &op = MI->getOperand(ii);
761 if (op.isPCRelativeDisp()) {
762 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000763 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000764 // Location is the target of the branch
765 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000766 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000767 // Save the flags.
768 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
769 if (op.opLoBits32()) { loBits32=true; }
770 if (op.opHiBits32()) { hiBits32=true; }
771 if (op.opLoBits64()) { loBits64=true; }
772 if (op.opHiBits64()) { hiBits64=true; }
773 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
774 branchTarget);
775 if (loBits32) { MI->setOperandLo32(ii); }
776 else if (hiBits32) { MI->setOperandHi32(ii); }
777 else if (loBits64) { MI->setOperandLo64(ii); }
778 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000779 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000780 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
781 *Ref = fixedInstr;
782 break;
783 }
784 }
785 }
786 BBRefs.clear();
787 BBLocations.clear();
788
Misha Brukman3de36f52003-05-27 20:07:58 +0000789 return false;
790}
791
792void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000793 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000794 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000795 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
796 unsigned binCode = getBinaryCodeForInstr(**I);
797 if (binCode == (1 << 30)) {
798 // this is an invalid call: the addr is out of bounds. that means a code
799 // sequence has already been emitted, and this is a no-op
800 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
801 } else {
802 emitWord(binCode);
803 }
804 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000805}
806
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000807void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
808 bool isPCRelative)
809{
810 if (isPCRelative) { // must be a call, this is a major hack!
811 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000812 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
813 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000814 // The real target of the call is Addr = PC + (target * 4)
815 // CurByte is the PC, Addr we just received
816 return (void*) (((long)Addr - (long)CurByte) >> 2);
817 } else {
818 if (Function *F = dyn_cast<Function>(V)) {
819 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000820 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000821 cast<Function>(V));
822 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000823 return
824 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000825
826 } else if (Constant *C = ConstantPointerRef::get(V)) {
827 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000828 return (void*)
829 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000830 } else {
831 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
832 << ", " << *V << " not found in ConstantMap!\n";
833 abort();
834 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000835 } else {
836 std::cerr << "Unhandled global: " << *V << "\n";
837 abort();
838 }
839 }
840 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000841 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000842 }
843}
844
Misha Brukman3de36f52003-05-27 20:07:58 +0000845#include "SparcV9CodeEmitter.inc"