blob: 63bb7c37ad886c7643b0c442f3582f4e735e8118 [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 Brukman3de36f52003-05-27 20:07:58 +000020#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000021#include "SparcV9CodeEmitter.h"
Misha Brukmancfd67c92003-08-29 04:22:54 +000022#include "Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000023
Brian Gaekee69f7272003-08-14 06:04:59 +000024bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000025 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000026 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000027 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000028 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000029 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000030 return false;
31}
32
Misha Brukmanf86aaa82003-06-02 04:12:39 +000033namespace {
34 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000035 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000036 MachineCodeEmitter &MCE;
37
Misha Brukman0897c602003-08-06 16:20:22 +000038 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
39 /// lazily resolved.
40 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000041 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000042
Misha Brukman0897c602003-08-06 16:20:22 +000043 /// LazyResolverMap - Keep track of the lazy resolver created for a
44 /// particular function so that we can reuse them if necessary.
45 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000046 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000047
48 public:
49 enum CallType { ShortCall, FarCall };
50
51 private:
52 /// We need to keep track of whether we used a simple call or a far call
53 /// (many instructions) in sequence. This means we need to keep track of
54 /// what type of stub we generate.
55 static std::map<uint64_t, CallType> LazyCallFlavor;
56
Misha Brukmanf86aaa82003-06-02 04:12:39 +000057 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000058 JITResolver(SparcV9CodeEmitter &V9,
59 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
60 uint64_t getLazyResolver(Function *F);
61 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000062 void deleteFunctionReference(uint64_t Address);
63 void addCallFlavor(uint64_t Address, CallType Flavor) {
64 LazyCallFlavor[Address] = Flavor;
65 }
Misha Brukmana2196c12003-06-04 20:01:13 +000066
67 // Utility functions for accessing data from static callback
68 uint64_t getCurrentPCValue() {
69 return MCE.getCurrentPCValue();
70 }
71 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
72 return SparcV9.getBinaryCodeForInstr(MI);
73 }
74
Misha Brukmanf47d9c22003-06-05 20:52:06 +000075 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
76
Misha Brukmanf86aaa82003-06-02 04:12:39 +000077 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000078 uint64_t emitStubForFunction(Function *F);
Misha Brukmancfd67c92003-08-29 04:22:54 +000079 static void SaveRegisters(uint64_t DoubleFP[]);
80 static void RestoreRegisters(uint64_t DoubleFP[]);
81 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000082 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000083
Misha Brukmanf86aaa82003-06-02 04:12:39 +000084 };
85
86 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +000087 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000088}
89
90/// addFunctionReference - This method is called when we need to emit the
91/// address of a function that has not yet been emitted, so we don't know the
92/// address. Instead, we emit a call to the CompilationCallback method, and
93/// keep track of where we are.
94///
Misha Brukmana2196c12003-06-04 20:01:13 +000095uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +000096 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +000097 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000098}
99
Misha Brukman0897c602003-08-06 16:20:22 +0000100/// deleteFunctionReference - If we are emitting a far call, we already added a
101/// reference to the function, but it is now incorrect, since the address to the
102/// JIT resolver is too far away to be a simple call instruction. This is used
103/// to remove the address from the map.
104///
105void JITResolver::deleteFunctionReference(uint64_t Address) {
106 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
107 assert(I != LazyCodeGenMap.end() && "Not in map!");
108 LazyCodeGenMap.erase(I);
109}
110
Misha Brukmana2196c12003-06-04 20:01:13 +0000111uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
112 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000113 assert(I != LazyCodeGenMap.end() && "Not in map!");
114 Function *F = I->second;
115 LazyCodeGenMap.erase(I);
116 return MCE.forceCompilationOf(F);
117}
118
Misha Brukmana2196c12003-06-04 20:01:13 +0000119uint64_t JITResolver::getLazyResolver(Function *F) {
120 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000121 if (I != LazyResolverMap.end() && I->first == F) return I->second;
122
123//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
124
Misha Brukmana2196c12003-06-04 20:01:13 +0000125 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000126 LazyResolverMap.insert(I, std::make_pair(F, Stub));
127 return Stub;
128}
129
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000130uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
131
Misha Brukmanfad49292003-08-15 00:26:50 +0000132 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000133 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
134 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000135
Misha Brukman0897c602003-08-06 16:20:22 +0000136 MachineInstr* BinaryCode[] = {
137 //
Misha Brukman0870e972003-08-06 22:19:18 +0000138 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000139 //
Misha Brukman0870e972003-08-06 22:19:18 +0000140 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
141 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
142 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
143 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
144 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
145 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
146 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
147 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000148 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000149 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
150 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
151 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
152 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
153 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
154 // nop ;; delay slot
155 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000156 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000157
Misha Brukman0897c602003-08-06 16:20:22 +0000158 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
159 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
160 delete BinaryCode[i];
161 Addr += 4;
162 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000163
164 return Addr;
165}
166
Misha Brukmancfd67c92003-08-29 04:22:54 +0000167void JITResolver::SaveRegisters(uint64_t DoubleFP[]) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000168#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000169
Misha Brukmancfd67c92003-08-29 04:22:54 +0000170#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000171 __asm__ __volatile__ (// Save condition-code registers
172 "stx %%fsr, %0;\n\t"
173 "rd %%fprs, %1;\n\t"
174 "rd %%ccr, %2;\n\t"
175 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000176#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000177
178 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000179 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
180 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
181 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
182 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
183 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
184 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
185 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
186 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
187 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
188 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
189 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
190 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
191 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
192 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
193 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
194 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
195 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000196
Misha Brukmancfd67c92003-08-29 04:22:54 +0000197 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000198 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000199 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000200 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
201 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
202 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
203 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
204 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
205 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000206 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
207 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
208 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
209 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
210 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
211 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
212 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
213 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000214#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000215}
Misha Brukmanfad49292003-08-15 00:26:50 +0000216
Misha Brukmanfad49292003-08-15 00:26:50 +0000217
Misha Brukmancfd67c92003-08-29 04:22:54 +0000218void JITResolver::RestoreRegisters(uint64_t DoubleFP[]) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000219#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000220
Misha Brukmancfd67c92003-08-29 04:22:54 +0000221#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000222 __asm__ __volatile__ (// Restore condition-code registers
223 "ldx %0, %%fsr;\n\t"
224 "wr %1, 0, %%fprs;\n\t"
225 "wr %2, 0, %%ccr;\n\t"
226 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000227#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000228
229 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000230 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
231 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
232 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
233 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
234 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
235 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
236 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
237 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
238 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
239 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
240 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
241 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
242 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
243 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
244 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
245 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
246 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000247
Misha Brukmancfd67c92003-08-29 04:22:54 +0000248 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000249 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
250 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
251 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
252 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
253 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
254 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
255 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
256 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000257 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
258 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
259 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
260 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
261 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
262 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
263 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
264 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000265#endif
266}
267
Misha Brukmancfd67c92003-08-29 04:22:54 +0000268void JITResolver::CompilationCallback() {
269 // Local space to save double registers
270 uint64_t DoubleFP[32];
271 //uint64_t CCR, FSR, FPRS;
272
273 SaveRegisters(DoubleFP);
274
275 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000276 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000277 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000278 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000279#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000280 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
281 DEBUG(std::cerr << "Read i7 (return addr) = "
282 << std::hex << returnAddr << ", value: "
283 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000284#endif
285
Misha Brukman0897c602003-08-06 16:20:22 +0000286 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000287 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000288
Misha Brukman0897c602003-08-06 16:20:22 +0000289 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000290
Misha Brukman0897c602003-08-06 16:20:22 +0000291 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000292 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000293 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000294 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000295 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000296
297 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
298 // pointer elimination has been performed. Having a variable sized alloca
299 // disables frame pointer elimination currently, even if it's dead. This is a
300 // gross hack.
301 alloca(42+Offset);
302 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000303
304 // Make sure that what we're about to overwrite is indeed "save"
305 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
306 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
307 delete SV;
308 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000309 if (CodeInMem != SaveInst) {
310 std::cerr << "About to overwrite smthg not a save instr!";
311 abort();
312 }
Misha Brukman0897c602003-08-06 16:20:22 +0000313 DEBUG(std::cerr << "Emitting a far jump to 0x" << std::hex << Target << "\n");
314 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000315
316 // FIXME: if the target function is close enough to fit into the 19bit disp of
317 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman0897c602003-08-06 16:20:22 +0000318#if 0
319 uint64_t InstAddr = CodeBegin;
320 // ba <target>
321 MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(Target);
322 *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*MI);
323 InstAddr += 4;
Misha Brukmana2196c12003-06-04 20:01:13 +0000324 delete MI;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000325
Misha Brukman0897c602003-08-06 16:20:22 +0000326 // nop
327 MI = BuildMI(V9::NOP, 0);
328 *((unsigned*)(intptr_t))=TheJITResolver->getBinaryCodeForInstr(*Nop);
329 delete MI;
330#endif
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000331
Misha Brukmancfd67c92003-08-29 04:22:54 +0000332 RestoreRegisters(DoubleFP);
333
Misha Brukmanfad49292003-08-15 00:26:50 +0000334 // Change the return address to reexecute the restore, then the jump However,
335 // we can't just modify %i7 here, because we return to the function that will
336 // restore the floating-point registers for us. Thus, we just return the value
337 // we want it to be, and the parent will take care of setting %i7 correctly.
338 DEBUG(std::cerr << "Callback returning the addr of restore inst: "
Misha Brukman0897c602003-08-06 16:20:22 +0000339 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000340#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
341 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
342#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000343}
344
345/// emitStubForFunction - This method is used by the JIT when it needs to emit
346/// the address of a function for a function whose code has not yet been
347/// generated. In order to do this, it generates a stub which jumps to the lazy
348/// function compiler, which will eventually get fixed to call the function
349/// directly.
350///
Misha Brukmana2196c12003-06-04 20:01:13 +0000351uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000352 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000353
Misha Brukman8f122222003-06-06 00:26:11 +0000354 DEBUG(std::cerr << "Emitting stub at addr: 0x"
355 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000356
Misha Brukman0897c602003-08-06 16:20:22 +0000357 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
358
359 // restore %g0, 0, %g0
360 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
361 .addMReg(g0, MOTy::Def);
362 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
363 delete R;
364
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000365 // save %sp, -192, %sp
366 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
367 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
368 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000369
370 int64_t CurrPC = MCE.getCurrentPCValue();
371 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
372 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman0897c602003-08-06 16:20:22 +0000373 //if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
Misha Brukman0870e972003-08-06 22:19:18 +0000374 // Since this is a far call, the actual address of the call is shifted
375 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000376 deleteFunctionReference(CurrPC);
377 SparcV9.emitFarCall(Addr, F);
378#if 0
Misha Brukman0870e972003-08-06 22:19:18 +0000379 else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000380 // call CallTarget ;; invoke the callback
381 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
382 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
383 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000384
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000385 // nop ;; call delay slot
386 MachineInstr *Nop = BuildMI(V9::NOP, 0);
387 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
388 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000389
390 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000391 }
Misha Brukman0897c602003-08-06 16:20:22 +0000392#endif
Misha Brukmana2196c12003-06-04 20:01:13 +0000393
394 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000395 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000396}
397
398
Misha Brukmana2196c12003-06-04 20:01:13 +0000399SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
400 MachineCodeEmitter &M): TM(tm), MCE(M)
401{
402 TheJITResolver = new JITResolver(*this, M);
403}
404
405SparcV9CodeEmitter::~SparcV9CodeEmitter() {
406 delete TheJITResolver;
407}
408
409void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000410 // Output the constant in big endian byte order...
411 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000412 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000413 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000414 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000415 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000416}
417
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000418unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000419SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000420 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000421 const TargetRegInfo &RI = TM.getRegInfo();
422 unsigned regClass, regType = RI.getRegType(fakeReg);
423 // At least map fakeReg into its class
424 fakeReg = RI.getClassRegNum(fakeReg, regClass);
425
Misha Brukman9cedd432003-07-03 18:36:47 +0000426 switch (regClass) {
427 case UltraSparcRegInfo::IntRegClassID: {
428 // Sparc manual, p31
429 static const unsigned IntRegMap[] = {
430 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
431 8, 9, 10, 11, 12, 13, 15,
432 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
433 16, 17, 18, 19, 20, 21, 22, 23,
434 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
435 24, 25, 26, 27, 28, 29, 30, 31,
436 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
437 0, 1, 2, 3, 4, 5, 6, 7,
438 // "o6"
439 14
440 };
441
442 return IntRegMap[fakeReg];
443 break;
444 }
445 case UltraSparcRegInfo::FloatRegClassID: {
446 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000447 if (regType == UltraSparcRegInfo::FPSingleRegType) {
448 // only numbered 0-31, hence can already fit into 5 bits (and 6)
449 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
450 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
451 // FIXME: This assumes that we only have 5-bit register fiels!
452 // From Sparc Manual, page 40.
453 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
454 fakeReg |= (fakeReg >> 5) & 1;
455 fakeReg &= 0x1f;
456 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
457 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000458 return fakeReg;
459 }
460 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000461 /* xcc, icc, ccr */
462 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000463
Misha Brukmandfbfc572003-07-16 20:30:40 +0000464 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
465 && "CC register out of bounds for IntCCReg map");
466 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
467 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000468 }
469 case UltraSparcRegInfo::FloatCCRegClassID: {
470 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
471 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
472 return fakeReg;
473 }
474 default:
475 assert(0 && "Invalid unified register number in getRegType");
476 return fakeReg;
477 }
478}
479
480
Misha Brukman07d45162003-07-15 19:09:43 +0000481// WARNING: if the call used the delay slot to do meaningful work, that's not
482// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000483inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000484 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000485 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
486 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000487
Misha Brukman0897c602003-08-06 16:20:22 +0000488 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000489 //
Misha Brukman0870e972003-08-06 22:19:18 +0000490 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000491 //
Misha Brukman0870e972003-08-06 22:19:18 +0000492 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
493 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
494 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
495 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000496 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000497 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
498 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
499 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000500 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000501 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
502 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
503 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000504 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000505 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000506 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000507 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000508 };
Misha Brukman07d45162003-07-15 19:09:43 +0000509
Misha Brukman0897c602003-08-06 16:20:22 +0000510 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
511 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000512 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000513 uint64_t CurrPC = MCE.getCurrentPCValue();
514 TheJITResolver->addFunctionReference(CurrPC, F);
515 // Remember that this is a far call, to subtract appropriate offset later
516 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
517 }
Misha Brukman07d45162003-07-15 19:09:43 +0000518
Misha Brukman0897c602003-08-06 16:20:22 +0000519 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
520 delete BinaryCode[i];
521 }
Misha Brukman07d45162003-07-15 19:09:43 +0000522}
523
524
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000525int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
526 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000527 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
528 // or things that get fixed up later by the JIT.
529
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000530 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000531 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000532 abort();
533 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000534 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000535 Value *V = MO.getVRegValue();
536 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000537 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000538 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000539 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000540 } else if (const Constant *C = dyn_cast<Constant>(V)) {
541 if (ConstantMap.find(C) != ConstantMap.end()) {
542 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000543 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000544 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000545 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000546 abort();
547 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000548 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
549 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000550 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000551 // external function calls, etc.?
552 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000553 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000554 if (F->isExternal()) {
555 // Sparc backend broken: this MO should be `ExternalSymbol'
556 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
557 } else {
558 rv = (int64_t)MCE.getGlobalValueAddress(F);
559 }
560 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000561 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000562 // Function has not yet been code generated!
563 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
564 // Delayed resolution...
565 rv = TheJITResolver->getLazyResolver(F);
566 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000567 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000568 }
569 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000570 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000571 if (rv == 0) {
572 if (Constant *C = ConstantPointerRef::get(GV)) {
573 if (ConstantMap.find(C) != ConstantMap.end()) {
574 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
575 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000576 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000577 << ", " << *V << " not found in ConstantMap!\n";
578 abort();
579 }
580 }
581 }
Misha Brukman0870e972003-08-06 22:19:18 +0000582 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000583 }
584 // The real target of the call is Addr = PC + (rv * 4)
585 // So undo that: give the instruction (Addr - PC) / 4
586 if (MI.getOpcode() == V9::CALL) {
587 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000588 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000589 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000590 int64_t CallInstTarget = (rv - CurrPC) >> 2;
591 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
592 DEBUG(std::cerr << "Making far call!\n");
593 // addresss is out of bounds for the 30-bit call,
594 // make an indirect jump-and-link
595 emitFarCall(rv);
596 // this invalidates the instruction so that the call with an incorrect
597 // address will not be emitted
598 rv = 0;
599 } else {
600 // The call fits into 30 bits, so just return the corrected address
601 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000602 }
Misha Brukman8f122222003-06-06 00:26:11 +0000603 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000604 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000605 } else {
606 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
607 abort();
608 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000609 } else if (MO.isPhysicalRegister() ||
610 MO.getType() == MachineOperand::MO_CCRegister)
611 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000612 // This is necessary because the Sparc backend doesn't actually lay out
613 // registers in the real fashion -- it skips those that it chooses not to
614 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000615 unsigned fakeReg = MO.getAllocatedRegNum();
616 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
617 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000618 << realRegByClass << " (LLC: "
619 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000620 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000621 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000622 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000623 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000624 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000625 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000626 rv = (int64_t)
627 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000628 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000629 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000630 // Duplicate code of the above case for VirtualRegister, BasicBlock...
631 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000632 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000633 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000634 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000635 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000636 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000637 // Sparc backend doesn't generate this (yet...)
638 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
639 abort();
640 } else if (MO.isFrameIndex()) {
641 // Sparc backend doesn't generate this (yet...)
642 int FrameIndex = MO.getFrameIndex();
643 std::cerr << "ERROR: Frame index unhandled.\n";
644 abort();
645 } else if (MO.isConstantPoolIndex()) {
646 // Sparc backend doesn't generate this (yet...)
647 std::cerr << "ERROR: Constant Pool index unhandled.\n";
648 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000649 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000650 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000651 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000652 }
653
654 // Finally, deal with the various bitfield-extracting functions that
655 // are used in SPARC assembly. (Some of these make no sense in combination
656 // with some of the above; we'll trust that the instruction selector
657 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000658 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000659 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000660 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000661 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000662 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000663 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000664 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000665 return rv >> 42;
666 } else { // (unadorned) val
667 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000668 }
669}
670
671unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
672 Val >>= bit;
673 return (Val & 1);
674}
675
Misha Brukman3de36f52003-05-27 20:07:58 +0000676bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000677 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000678 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000679 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000680 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000681
Misha Brukmana2196c12003-06-04 20:01:13 +0000682 // The Sparc backend does not use MachineConstantPool;
683 // instead, it has its own constant pool implementation.
684 // We create a new MachineConstantPool here to be compatible with the emitter.
685 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000686 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
687 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
688 E = pool.end(); I != E; ++I)
689 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000690 Constant *C = (Constant*)*I;
691 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000692 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000693 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000694 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000695 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000696
Misha Brukman3de36f52003-05-27 20:07:58 +0000697 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
698 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000699 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000700
Misha Brukman9cedd432003-07-03 18:36:47 +0000701 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000702 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000703
704 // Resolve branches to BasicBlocks for the entire function
705 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
706 long Location = BBLocations[BBRefs[i].first];
707 unsigned *Ref = BBRefs[i].second.first;
708 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000709 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
710 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000711 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
712 MachineOperand &op = MI->getOperand(ii);
713 if (op.isPCRelativeDisp()) {
714 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000715 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000716 // Location is the target of the branch
717 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000718 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000719 // Save the flags.
720 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
721 if (op.opLoBits32()) { loBits32=true; }
722 if (op.opHiBits32()) { hiBits32=true; }
723 if (op.opLoBits64()) { loBits64=true; }
724 if (op.opHiBits64()) { hiBits64=true; }
725 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
726 branchTarget);
727 if (loBits32) { MI->setOperandLo32(ii); }
728 else if (hiBits32) { MI->setOperandHi32(ii); }
729 else if (loBits64) { MI->setOperandLo64(ii); }
730 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000731 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000732 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
733 *Ref = fixedInstr;
734 break;
735 }
736 }
737 }
738 BBRefs.clear();
739 BBLocations.clear();
740
Misha Brukman3de36f52003-05-27 20:07:58 +0000741 return false;
742}
743
744void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000745 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000746 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000747 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
748 unsigned binCode = getBinaryCodeForInstr(**I);
749 if (binCode == (1 << 30)) {
750 // this is an invalid call: the addr is out of bounds. that means a code
751 // sequence has already been emitted, and this is a no-op
752 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
753 } else {
754 emitWord(binCode);
755 }
756 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000757}
758
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000759void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
760 bool isPCRelative)
761{
762 if (isPCRelative) { // must be a call, this is a major hack!
763 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000764 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
765 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000766 // The real target of the call is Addr = PC + (target * 4)
767 // CurByte is the PC, Addr we just received
768 return (void*) (((long)Addr - (long)CurByte) >> 2);
769 } else {
770 if (Function *F = dyn_cast<Function>(V)) {
771 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000772 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000773 cast<Function>(V));
774 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000775 return
776 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000777
778 } else if (Constant *C = ConstantPointerRef::get(V)) {
779 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000780 return (void*)
781 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000782 } else {
783 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
784 << ", " << *V << " not found in ConstantMap!\n";
785 abort();
786 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000787 } else {
788 std::cerr << "Unhandled global: " << *V << "\n";
789 abort();
790 }
791 }
792 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000793 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000794 }
795}
796
Misha Brukman3de36f52003-05-27 20:07:58 +0000797#include "SparcV9CodeEmitter.inc"