blob: 51254e21c9d99e3a54d3f2b2839ccc3ab1cab595 [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 Brukman3de36f52003-05-27 20:07:58 +000022
Brian Gaekee69f7272003-08-14 06:04:59 +000023bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000024 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000025 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000026 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000027 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000028 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000029 return false;
30}
31
Misha Brukmanf86aaa82003-06-02 04:12:39 +000032namespace {
33 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000034 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000035 MachineCodeEmitter &MCE;
36
Misha Brukman0897c602003-08-06 16:20:22 +000037 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
38 /// lazily resolved.
39 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000040 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000041
Misha Brukman0897c602003-08-06 16:20:22 +000042 /// LazyResolverMap - Keep track of the lazy resolver created for a
43 /// particular function so that we can reuse them if necessary.
44 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000045 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000046
47 public:
48 enum CallType { ShortCall, FarCall };
49
50 private:
51 /// We need to keep track of whether we used a simple call or a far call
52 /// (many instructions) in sequence. This means we need to keep track of
53 /// what type of stub we generate.
54 static std::map<uint64_t, CallType> LazyCallFlavor;
55
Misha Brukmanf86aaa82003-06-02 04:12:39 +000056 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000057 JITResolver(SparcV9CodeEmitter &V9,
58 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
59 uint64_t getLazyResolver(Function *F);
60 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000061 void deleteFunctionReference(uint64_t Address);
62 void addCallFlavor(uint64_t Address, CallType Flavor) {
63 LazyCallFlavor[Address] = Flavor;
64 }
Misha Brukmana2196c12003-06-04 20:01:13 +000065
66 // Utility functions for accessing data from static callback
67 uint64_t getCurrentPCValue() {
68 return MCE.getCurrentPCValue();
69 }
70 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
71 return SparcV9.getBinaryCodeForInstr(MI);
72 }
73
Misha Brukmanf47d9c22003-06-05 20:52:06 +000074 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
75
Misha Brukmanf86aaa82003-06-02 04:12:39 +000076 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000077 uint64_t emitStubForFunction(Function *F);
Misha Brukmanfad49292003-08-15 00:26:50 +000078 static void SaveRestoreRegisters();
79 static uint64_t CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000080 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000081
Misha Brukmanf86aaa82003-06-02 04:12:39 +000082 };
83
84 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +000085 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000086}
87
88/// addFunctionReference - This method is called when we need to emit the
89/// address of a function that has not yet been emitted, so we don't know the
90/// address. Instead, we emit a call to the CompilationCallback method, and
91/// keep track of where we are.
92///
Misha Brukmana2196c12003-06-04 20:01:13 +000093uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +000094 LazyCodeGenMap[Address] = F;
Misha Brukmanfad49292003-08-15 00:26:50 +000095 return (intptr_t)&JITResolver::SaveRestoreRegisters;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000096}
97
Misha Brukman0897c602003-08-06 16:20:22 +000098/// deleteFunctionReference - If we are emitting a far call, we already added a
99/// reference to the function, but it is now incorrect, since the address to the
100/// JIT resolver is too far away to be a simple call instruction. This is used
101/// to remove the address from the map.
102///
103void JITResolver::deleteFunctionReference(uint64_t Address) {
104 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
105 assert(I != LazyCodeGenMap.end() && "Not in map!");
106 LazyCodeGenMap.erase(I);
107}
108
Misha Brukmana2196c12003-06-04 20:01:13 +0000109uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
110 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000111 assert(I != LazyCodeGenMap.end() && "Not in map!");
112 Function *F = I->second;
113 LazyCodeGenMap.erase(I);
114 return MCE.forceCompilationOf(F);
115}
116
Misha Brukmana2196c12003-06-04 20:01:13 +0000117uint64_t JITResolver::getLazyResolver(Function *F) {
118 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000119 if (I != LazyResolverMap.end() && I->first == F) return I->second;
120
121//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
122
Misha Brukmana2196c12003-06-04 20:01:13 +0000123 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000124 LazyResolverMap.insert(I, std::make_pair(F, Stub));
125 return Stub;
126}
127
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000128uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
129
Misha Brukmanfad49292003-08-15 00:26:50 +0000130 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000131 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
132 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000133
Misha Brukman0897c602003-08-06 16:20:22 +0000134 MachineInstr* BinaryCode[] = {
135 //
Misha Brukman0870e972003-08-06 22:19:18 +0000136 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000137 //
Misha Brukman0870e972003-08-06 22:19:18 +0000138 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
139 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
140 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
141 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
142 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
143 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
144 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
145 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000146 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000147 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
148 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
149 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
150 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
151 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
152 // nop ;; delay slot
153 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000154 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000155
Misha Brukman0897c602003-08-06 16:20:22 +0000156 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
157 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
158 delete BinaryCode[i];
159 Addr += 4;
160 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000161
162 return Addr;
163}
164
Misha Brukmanfad49292003-08-15 00:26:50 +0000165void JITResolver::SaveRestoreRegisters() {
166 uint32_t SingleFP[32];
167 uint64_t DoubleFP[16];
168 // FIXME: uint128_t QuadFloatRegs[..];
169 uint64_t CCR, FSR, FPRS, g1, g5;
170
171#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
172 __asm__ __volatile__ (// Save g1 and g5
173 "stx %%g1, %0;\n\t" "stx %%g5, %1;\n\t"
174 : "=m"(g1), "=m"(g5));
175
176 __asm__ __volatile__ (// Save condition-code registers
177 "stx %%fsr, %0;\n\t"
178 "rd %%fprs, %1;\n\t"
179 "rd %%ccr, %2;\n\t"
180 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
181
182 // GCC says: `asm' only allows up to thirty parameters!
183 __asm__ __volatile__ (// Save Single FP registers, part 1
184 "st %%f0, %0;\n\t" "st %%f1, %1;\n\t"
185 "st %%f2, %2;\n\t" "st %%f3, %3;\n\t"
186 "st %%f4, %4;\n\t" "st %%f5, %5;\n\t"
187 "st %%f6, %6;\n\t" "st %%f7, %7;\n\t"
188 "st %%f8, %8;\n\t" "st %%f9, %9;\n\t"
189 "st %%f10, %10;\n\t" "st %%f11, %11;\n\t"
190 "st %%f12, %12;\n\t" "st %%f13, %13;\n\t"
191 "st %%f14, %14;\n\t" "st %%f15, %15;\n\t"
192 : "=m"(SingleFP[ 0]), "=m"(SingleFP[ 1]),
193 "=m"(SingleFP[ 2]), "=m"(SingleFP[ 3]),
194 "=m"(SingleFP[ 4]), "=m"(SingleFP[ 5]),
195 "=m"(SingleFP[ 6]), "=m"(SingleFP[ 7]),
196 "=m"(SingleFP[ 8]), "=m"(SingleFP[ 9]),
197 "=m"(SingleFP[10]), "=m"(SingleFP[11]),
198 "=m"(SingleFP[12]), "=m"(SingleFP[13]),
199 "=m"(SingleFP[14]), "=m"(SingleFP[15]));
200
201 __asm__ __volatile__ (// Save Single FP registers, part 2
202 "st %%f16, %0;\n\t" "st %%f17, %1;\n\t"
203 "st %%f18, %2;\n\t" "st %%f19, %3;\n\t"
204 "st %%f20, %4;\n\t" "st %%f21, %5;\n\t"
205 "st %%f22, %6;\n\t" "st %%f23, %7;\n\t"
206 "st %%f24, %8;\n\t" "st %%f25, %9;\n\t"
207 "st %%f26, %10;\n\t" "st %%f27, %11;\n\t"
208 "st %%f28, %12;\n\t" "st %%f29, %13;\n\t"
209 "st %%f30, %14;\n\t" "st %%f31, %15;\n\t"
210 : "=m"(SingleFP[16]), "=m"(SingleFP[17]),
211 "=m"(SingleFP[18]), "=m"(SingleFP[19]),
212 "=m"(SingleFP[20]), "=m"(SingleFP[21]),
213 "=m"(SingleFP[22]), "=m"(SingleFP[23]),
214 "=m"(SingleFP[24]), "=m"(SingleFP[25]),
215 "=m"(SingleFP[26]), "=m"(SingleFP[27]),
216 "=m"(SingleFP[28]), "=m"(SingleFP[29]),
217 "=m"(SingleFP[30]), "=m"(SingleFP[31]));
218
219 __asm__ __volatile__ (// Save Double FP registers
220 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000221 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000222 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
223 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
224 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
225 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
226 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
227 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
228 : "=m"(DoubleFP[32/2-16]), "=m"(DoubleFP[34/2-16]),
229 "=m"(DoubleFP[36/2-16]), "=m"(DoubleFP[38/2-16]),
230 "=m"(DoubleFP[40/2-16]), "=m"(DoubleFP[42/2-16]),
231 "=m"(DoubleFP[44/2-16]), "=m"(DoubleFP[46/2-16]),
232 "=m"(DoubleFP[48/2-16]), "=m"(DoubleFP[50/2-16]),
233 "=m"(DoubleFP[52/2-16]), "=m"(DoubleFP[54/2-16]),
234 "=m"(DoubleFP[56/2-16]), "=m"(DoubleFP[58/2-16]),
235 "=m"(DoubleFP[60/2-16]), "=m"(DoubleFP[62/2-16]));
236#endif
237
238 // Resolve the function call
239 register uint64_t restoreAddr = CompilationCallback();
240
241#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
242 // Set the return address to re-execute the `restore' instruction
243 __asm__ __volatile__ ("or %%o0, %%g0, %%i7;\n\t"
244
245 // Restore g1 and g5
246 "ldx %0, %%g1;\n\t" "ldx %1, %%g5;\n\t"
247 :: "m"(g1), "m"(g5));
248
249 __asm__ __volatile__ (// Restore condition-code registers
250 "ldx %0, %%fsr;\n\t"
251 "wr %1, 0, %%fprs;\n\t"
252 "wr %2, 0, %%ccr;\n\t"
253 :: "m"(FSR), "r"(FPRS), "r"(CCR));
254
255 // GCC says: `asm' only allows up to thirty parameters!
256 __asm__ __volatile__ (// Restore Single FP registers, part 1
257 "ld %0, %%f0;\n\t" "ld %1, %%f1;\n\t"
258 "ld %2, %%f2;\n\t" "ld %3, %%f3;\n\t"
259 "ld %4, %%f4;\n\t" "ld %5, %%f5;\n\t"
260 "ld %6, %%f6;\n\t" "ld %7, %%f7;\n\t"
261 "ld %8, %%f8;\n\t" "ld %9, %%f9;\n\t"
262 "ld %10, %%f10;\n\t" "ld %11, %%f11;\n\t"
263 "ld %12, %%f12;\n\t" "ld %13, %%f13;\n\t"
264 "ld %14, %%f14;\n\t" "ld %15, %%f15;\n\t"
265 :: "m"(SingleFP[0]), "m"(SingleFP[1]),
266 "m"(SingleFP[2]), "m"(SingleFP[3]),
267 "m"(SingleFP[4]), "m"(SingleFP[5]),
268 "m"(SingleFP[6]), "m"(SingleFP[7]),
269 "m"(SingleFP[8]), "m"(SingleFP[9]),
270 "m"(SingleFP[10]), "m"(SingleFP[11]),
271 "m"(SingleFP[12]), "m"(SingleFP[13]),
272 "m"(SingleFP[14]), "m"(SingleFP[15]));
273
274 __asm__ __volatile__ (// Restore Single FP registers, part 2
275 "ld %0, %%f16;\n\t" "ld %1, %%f17;\n\t"
276 "ld %2, %%f18;\n\t" "ld %3, %%f19;\n\t"
277 "ld %4, %%f20;\n\t" "ld %5, %%f21;\n\t"
278 "ld %6, %%f22;\n\t" "ld %7, %%f23;\n\t"
279 "ld %8, %%f24;\n\t" "ld %9, %%f25;\n\t"
280 "ld %10, %%f26;\n\t" "ld %11, %%f27;\n\t"
281 "ld %12, %%f28;\n\t" "ld %13, %%f29;\n\t"
282 "ld %14, %%f30;\n\t" "ld %15, %%f31;\n\t"
283 :: "m"(SingleFP[16]), "m"(SingleFP[17]),
284 "m"(SingleFP[18]), "m"(SingleFP[19]),
285 "m"(SingleFP[20]), "m"(SingleFP[21]),
286 "m"(SingleFP[22]), "m"(SingleFP[23]),
287 "m"(SingleFP[24]), "m"(SingleFP[25]),
288 "m"(SingleFP[26]), "m"(SingleFP[27]),
289 "m"(SingleFP[28]), "m"(SingleFP[29]),
290 "m"(SingleFP[30]), "m"(SingleFP[31]));
291
292 __asm__ __volatile__ (// Restore Double FP registers
293 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
294 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
295 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
296 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
297 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
298 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
299 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
300 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
301 :: "m"(DoubleFP[32/2-16]), "m"(DoubleFP[34/2-16]),
302 "m"(DoubleFP[36/2-16]), "m"(DoubleFP[38/2-16]),
303 "m"(DoubleFP[40/2-16]), "m"(DoubleFP[42/2-16]),
304 "m"(DoubleFP[44/2-16]), "m"(DoubleFP[46/2-16]),
305 "m"(DoubleFP[48/2-16]), "m"(DoubleFP[50/2-16]),
306 "m"(DoubleFP[52/2-16]), "m"(DoubleFP[54/2-16]),
307 "m"(DoubleFP[56/2-16]), "m"(DoubleFP[58/2-16]),
308 "m"(DoubleFP[60/2-16]), "m"(DoubleFP[62/2-16]));
309#endif
310}
311
312uint64_t JITResolver::CompilationCallback() {
313 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000314 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000315 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000316 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000317#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000318 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
319 DEBUG(std::cerr << "Read i7 (return addr) = "
320 << std::hex << returnAddr << ", value: "
321 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000322#endif
323
Misha Brukman0897c602003-08-06 16:20:22 +0000324 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000325 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000326
Misha Brukman0897c602003-08-06 16:20:22 +0000327 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000328
Misha Brukman0897c602003-08-06 16:20:22 +0000329 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000330 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000331 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000332 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000333 uint64_t CodeBegin = CameFrom - Offset;
334
335 // Make sure that what we're about to overwrite is indeed "save"
336 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
337 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
338 delete SV;
339 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
340 assert(CodeInMem == SaveInst && "About to overwrite smthg not a save instr!");
341 DEBUG(std::cerr << "Emitting a far jump to 0x" << std::hex << Target << "\n");
342 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000343
344 // FIXME: if the target function is close enough to fit into the 19bit disp of
345 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman0897c602003-08-06 16:20:22 +0000346#if 0
347 uint64_t InstAddr = CodeBegin;
348 // ba <target>
349 MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(Target);
350 *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*MI);
351 InstAddr += 4;
Misha Brukmana2196c12003-06-04 20:01:13 +0000352 delete MI;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000353
Misha Brukman0897c602003-08-06 16:20:22 +0000354 // nop
355 MI = BuildMI(V9::NOP, 0);
356 *((unsigned*)(intptr_t))=TheJITResolver->getBinaryCodeForInstr(*Nop);
357 delete MI;
358#endif
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000359
Misha Brukmanfad49292003-08-15 00:26:50 +0000360 // Change the return address to reexecute the restore, then the jump However,
361 // we can't just modify %i7 here, because we return to the function that will
362 // restore the floating-point registers for us. Thus, we just return the value
363 // we want it to be, and the parent will take care of setting %i7 correctly.
364 DEBUG(std::cerr << "Callback returning the addr of restore inst: "
Misha Brukman0897c602003-08-06 16:20:22 +0000365 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000366 return CameFrom - Offset - 12; // 8 because of call+delay, 4 more to restore
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000367}
368
369/// emitStubForFunction - This method is used by the JIT when it needs to emit
370/// the address of a function for a function whose code has not yet been
371/// generated. In order to do this, it generates a stub which jumps to the lazy
372/// function compiler, which will eventually get fixed to call the function
373/// directly.
374///
Misha Brukmana2196c12003-06-04 20:01:13 +0000375uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmand2dfc962003-08-15 18:03:06 +0000376 // FIXME: 40 is not enough... but should be
377 MCE.startFunctionStub(*F, 64);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000378
Misha Brukman8f122222003-06-06 00:26:11 +0000379 DEBUG(std::cerr << "Emitting stub at addr: 0x"
380 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000381
Misha Brukman0897c602003-08-06 16:20:22 +0000382 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
383
384 // restore %g0, 0, %g0
385 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
386 .addMReg(g0, MOTy::Def);
387 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
388 delete R;
389
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000390 // save %sp, -192, %sp
391 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
392 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
393 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000394
395 int64_t CurrPC = MCE.getCurrentPCValue();
396 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
397 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman0897c602003-08-06 16:20:22 +0000398 //if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
Misha Brukman0870e972003-08-06 22:19:18 +0000399 // Since this is a far call, the actual address of the call is shifted
400 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000401 deleteFunctionReference(CurrPC);
402 SparcV9.emitFarCall(Addr, F);
403#if 0
Misha Brukman0870e972003-08-06 22:19:18 +0000404 else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000405 // call CallTarget ;; invoke the callback
406 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
407 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
408 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000409
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000410 // nop ;; call delay slot
411 MachineInstr *Nop = BuildMI(V9::NOP, 0);
412 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
413 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000414
415 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000416 }
Misha Brukman0897c602003-08-06 16:20:22 +0000417#endif
Misha Brukmana2196c12003-06-04 20:01:13 +0000418
419 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000420 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000421}
422
423
Misha Brukmana2196c12003-06-04 20:01:13 +0000424SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
425 MachineCodeEmitter &M): TM(tm), MCE(M)
426{
427 TheJITResolver = new JITResolver(*this, M);
428}
429
430SparcV9CodeEmitter::~SparcV9CodeEmitter() {
431 delete TheJITResolver;
432}
433
434void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000435 // Output the constant in big endian byte order...
436 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000437 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000438 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000439 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000440 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000441}
442
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000443unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000444SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000445 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000446 const TargetRegInfo &RI = TM.getRegInfo();
447 unsigned regClass, regType = RI.getRegType(fakeReg);
448 // At least map fakeReg into its class
449 fakeReg = RI.getClassRegNum(fakeReg, regClass);
450
Misha Brukman9cedd432003-07-03 18:36:47 +0000451 switch (regClass) {
452 case UltraSparcRegInfo::IntRegClassID: {
453 // Sparc manual, p31
454 static const unsigned IntRegMap[] = {
455 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
456 8, 9, 10, 11, 12, 13, 15,
457 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
458 16, 17, 18, 19, 20, 21, 22, 23,
459 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
460 24, 25, 26, 27, 28, 29, 30, 31,
461 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
462 0, 1, 2, 3, 4, 5, 6, 7,
463 // "o6"
464 14
465 };
466
467 return IntRegMap[fakeReg];
468 break;
469 }
470 case UltraSparcRegInfo::FloatRegClassID: {
471 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000472 if (regType == UltraSparcRegInfo::FPSingleRegType) {
473 // only numbered 0-31, hence can already fit into 5 bits (and 6)
474 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
475 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
476 // FIXME: This assumes that we only have 5-bit register fiels!
477 // From Sparc Manual, page 40.
478 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
479 fakeReg |= (fakeReg >> 5) & 1;
480 fakeReg &= 0x1f;
481 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
482 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000483 return fakeReg;
484 }
485 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000486 /* xcc, icc, ccr */
487 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000488
Misha Brukmandfbfc572003-07-16 20:30:40 +0000489 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
490 && "CC register out of bounds for IntCCReg map");
491 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
492 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000493 }
494 case UltraSparcRegInfo::FloatCCRegClassID: {
495 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
496 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
497 return fakeReg;
498 }
499 default:
500 assert(0 && "Invalid unified register number in getRegType");
501 return fakeReg;
502 }
503}
504
505
Misha Brukman07d45162003-07-15 19:09:43 +0000506// WARNING: if the call used the delay slot to do meaningful work, that's not
507// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000508inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000509 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000510 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
511 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000512
Misha Brukman0897c602003-08-06 16:20:22 +0000513 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000514 //
Misha Brukman0870e972003-08-06 22:19:18 +0000515 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000516 //
Misha Brukman0870e972003-08-06 22:19:18 +0000517 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
518 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
519 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
520 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000521 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000522 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
523 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
524 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000525 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000526 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
527 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
528 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000529 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000530 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000531 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000532 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000533 };
Misha Brukman07d45162003-07-15 19:09:43 +0000534
Misha Brukman0897c602003-08-06 16:20:22 +0000535 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
536 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000537 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000538 uint64_t CurrPC = MCE.getCurrentPCValue();
539 TheJITResolver->addFunctionReference(CurrPC, F);
540 // Remember that this is a far call, to subtract appropriate offset later
541 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
542 }
Misha Brukman07d45162003-07-15 19:09:43 +0000543
Misha Brukman0897c602003-08-06 16:20:22 +0000544 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
545 delete BinaryCode[i];
546 }
Misha Brukman07d45162003-07-15 19:09:43 +0000547}
548
549
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000550int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
551 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000552 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
553 // or things that get fixed up later by the JIT.
554
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000555 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000556 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000557 abort();
558 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000559 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000560 Value *V = MO.getVRegValue();
561 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000562 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000563 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000564 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000565 } else if (const Constant *C = dyn_cast<Constant>(V)) {
566 if (ConstantMap.find(C) != ConstantMap.end()) {
567 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000568 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000569 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000570 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000571 abort();
572 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000573 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
574 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000575 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000576 // external function calls, etc.?
577 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000578 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000579 if (F->isExternal()) {
580 // Sparc backend broken: this MO should be `ExternalSymbol'
581 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
582 } else {
583 rv = (int64_t)MCE.getGlobalValueAddress(F);
584 }
585 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000586 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000587 // Function has not yet been code generated!
588 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
589 // Delayed resolution...
590 rv = TheJITResolver->getLazyResolver(F);
591 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000592 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000593 }
594 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000595 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000596 if (rv == 0) {
597 if (Constant *C = ConstantPointerRef::get(GV)) {
598 if (ConstantMap.find(C) != ConstantMap.end()) {
599 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
600 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000601 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000602 << ", " << *V << " not found in ConstantMap!\n";
603 abort();
604 }
605 }
606 }
Misha Brukman0870e972003-08-06 22:19:18 +0000607 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000608 }
609 // The real target of the call is Addr = PC + (rv * 4)
610 // So undo that: give the instruction (Addr - PC) / 4
611 if (MI.getOpcode() == V9::CALL) {
612 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000613 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000614 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000615 int64_t CallInstTarget = (rv - CurrPC) >> 2;
616 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
617 DEBUG(std::cerr << "Making far call!\n");
618 // addresss is out of bounds for the 30-bit call,
619 // make an indirect jump-and-link
620 emitFarCall(rv);
621 // this invalidates the instruction so that the call with an incorrect
622 // address will not be emitted
623 rv = 0;
624 } else {
625 // The call fits into 30 bits, so just return the corrected address
626 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000627 }
Misha Brukman8f122222003-06-06 00:26:11 +0000628 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000629 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000630 } else {
631 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
632 abort();
633 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000634 } else if (MO.isPhysicalRegister() ||
635 MO.getType() == MachineOperand::MO_CCRegister)
636 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000637 // This is necessary because the Sparc backend doesn't actually lay out
638 // registers in the real fashion -- it skips those that it chooses not to
639 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000640 unsigned fakeReg = MO.getAllocatedRegNum();
641 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
642 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000643 << realRegByClass << " (LLC: "
644 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000645 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000646 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000647 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000648 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000649 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000650 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000651 rv = (int64_t)
652 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000653 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000654 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000655 // Duplicate code of the above case for VirtualRegister, BasicBlock...
656 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000657 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000658 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000659 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000660 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000661 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000662 // Sparc backend doesn't generate this (yet...)
663 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
664 abort();
665 } else if (MO.isFrameIndex()) {
666 // Sparc backend doesn't generate this (yet...)
667 int FrameIndex = MO.getFrameIndex();
668 std::cerr << "ERROR: Frame index unhandled.\n";
669 abort();
670 } else if (MO.isConstantPoolIndex()) {
671 // Sparc backend doesn't generate this (yet...)
672 std::cerr << "ERROR: Constant Pool index unhandled.\n";
673 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000674 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000675 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000676 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000677 }
678
679 // Finally, deal with the various bitfield-extracting functions that
680 // are used in SPARC assembly. (Some of these make no sense in combination
681 // with some of the above; we'll trust that the instruction selector
682 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000683 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000684 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000685 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000686 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000687 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000688 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000689 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000690 return rv >> 42;
691 } else { // (unadorned) val
692 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000693 }
694}
695
696unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
697 Val >>= bit;
698 return (Val & 1);
699}
700
Misha Brukman3de36f52003-05-27 20:07:58 +0000701bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000702 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000703 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000704 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000705 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000706
Misha Brukmana2196c12003-06-04 20:01:13 +0000707 // The Sparc backend does not use MachineConstantPool;
708 // instead, it has its own constant pool implementation.
709 // We create a new MachineConstantPool here to be compatible with the emitter.
710 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000711 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
712 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
713 E = pool.end(); I != E; ++I)
714 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000715 Constant *C = (Constant*)*I;
716 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000717 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000718 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000719 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000720 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000721
Misha Brukman3de36f52003-05-27 20:07:58 +0000722 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
723 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000724 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000725
Misha Brukman9cedd432003-07-03 18:36:47 +0000726 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000727 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000728
729 // Resolve branches to BasicBlocks for the entire function
730 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
731 long Location = BBLocations[BBRefs[i].first];
732 unsigned *Ref = BBRefs[i].second.first;
733 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000734 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
735 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000736 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
737 MachineOperand &op = MI->getOperand(ii);
738 if (op.isPCRelativeDisp()) {
739 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000740 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000741 // Location is the target of the branch
742 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000743 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000744 // Save the flags.
745 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
746 if (op.opLoBits32()) { loBits32=true; }
747 if (op.opHiBits32()) { hiBits32=true; }
748 if (op.opLoBits64()) { loBits64=true; }
749 if (op.opHiBits64()) { hiBits64=true; }
750 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
751 branchTarget);
752 if (loBits32) { MI->setOperandLo32(ii); }
753 else if (hiBits32) { MI->setOperandHi32(ii); }
754 else if (loBits64) { MI->setOperandLo64(ii); }
755 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000756 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000757 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
758 *Ref = fixedInstr;
759 break;
760 }
761 }
762 }
763 BBRefs.clear();
764 BBLocations.clear();
765
Misha Brukman3de36f52003-05-27 20:07:58 +0000766 return false;
767}
768
769void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000770 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000771 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000772 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
773 unsigned binCode = getBinaryCodeForInstr(**I);
774 if (binCode == (1 << 30)) {
775 // this is an invalid call: the addr is out of bounds. that means a code
776 // sequence has already been emitted, and this is a no-op
777 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
778 } else {
779 emitWord(binCode);
780 }
781 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000782}
783
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000784void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
785 bool isPCRelative)
786{
787 if (isPCRelative) { // must be a call, this is a major hack!
788 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000789 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
790 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000791 // The real target of the call is Addr = PC + (target * 4)
792 // CurByte is the PC, Addr we just received
793 return (void*) (((long)Addr - (long)CurByte) >> 2);
794 } else {
795 if (Function *F = dyn_cast<Function>(V)) {
796 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000797 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000798 cast<Function>(V));
799 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000800 return
801 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000802
803 } else if (Constant *C = ConstantPointerRef::get(V)) {
804 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000805 return (void*)
806 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000807 } else {
808 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
809 << ", " << *V << " not found in ConstantMap!\n";
810 abort();
811 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000812 } else {
813 std::cerr << "Unhandled global: " << *V << "\n";
814 abort();
815 }
816 }
817 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000818 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000819 }
820}
821
Misha Brukman3de36f52003-05-27 20:07:58 +0000822#include "SparcV9CodeEmitter.inc"