blob: 15bb1bc2d30716771a24813ff9c4fd72e89afadd [file] [log] [blame]
Chris Lattner556d89d2003-08-01 22:19:03 +00001//===-- SparcV9CodeEmitter.cpp --------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00009//
Brian Gaeke42960882003-10-13 19:51:20 +000010// SPARC-specific backend for emitting machine code to memory.
11//
12// This module also contains the code for lazily resolving the targets
13// of call instructions, including the callback used to redirect calls
14// to functions for which the code has not yet been generated into the
15// JIT compiler.
16//
17// This file #includes SparcV9CodeEmitter.inc, which contains the code
18// for getBinaryCodeForInstr(), a method that converts a MachineInstr
19// into the corresponding binary machine code word.
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000020//
21//===----------------------------------------------------------------------===//
22
Misha Brukmanf86aaa82003-06-02 04:12:39 +000023#include "llvm/Constants.h"
24#include "llvm/Function.h"
25#include "llvm/GlobalVariable.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000026#include "llvm/PassManager.h"
27#include "llvm/CodeGen/MachineCodeEmitter.h"
Misha Brukmana2196c12003-06-04 20:01:13 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000029#include "llvm/CodeGen/MachineFunctionInfo.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000032#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000033#include "llvm/Target/TargetData.h"
Chris Lattner556d89d2003-08-01 22:19:03 +000034#include "Support/Debug.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000035#include "Support/hash_set"
Misha Brukman103f0c32003-09-05 22:59:31 +000036#include "Support/Statistic.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000037#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000038#include "SparcV9CodeEmitter.h"
Misha Brukmancfd67c92003-08-29 04:22:54 +000039#include "Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000040
Misha Brukman103f0c32003-09-05 22:59:31 +000041namespace {
42 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
43 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
44 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
45}
46
Brian Gaekee69f7272003-08-14 06:04:59 +000047bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000048 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000049 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000050 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000051 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000052 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000053 return false;
54}
55
Misha Brukmanf86aaa82003-06-02 04:12:39 +000056namespace {
57 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000058 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000059 MachineCodeEmitter &MCE;
60
Misha Brukman0897c602003-08-06 16:20:22 +000061 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
62 /// lazily resolved.
63 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000064 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000065
Misha Brukman0897c602003-08-06 16:20:22 +000066 /// LazyResolverMap - Keep track of the lazy resolver created for a
67 /// particular function so that we can reuse them if necessary.
68 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000069 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000070
71 public:
72 enum CallType { ShortCall, FarCall };
73
74 private:
75 /// We need to keep track of whether we used a simple call or a far call
76 /// (many instructions) in sequence. This means we need to keep track of
77 /// what type of stub we generate.
78 static std::map<uint64_t, CallType> LazyCallFlavor;
79
Misha Brukmanf86aaa82003-06-02 04:12:39 +000080 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000081 JITResolver(SparcV9CodeEmitter &V9,
82 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
83 uint64_t getLazyResolver(Function *F);
84 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000085 void deleteFunctionReference(uint64_t Address);
86 void addCallFlavor(uint64_t Address, CallType Flavor) {
87 LazyCallFlavor[Address] = Flavor;
88 }
Misha Brukmana2196c12003-06-04 20:01:13 +000089
90 // Utility functions for accessing data from static callback
91 uint64_t getCurrentPCValue() {
92 return MCE.getCurrentPCValue();
93 }
94 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
95 return SparcV9.getBinaryCodeForInstr(MI);
96 }
97
Brian Gaeke4ca7e532003-10-17 18:27:37 +000098 inline void insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
99 void insertJumpAtAddr(int64_t Value, uint64_t &Addr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000100
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000101 private:
Misha Brukmana2196c12003-06-04 20:01:13 +0000102 uint64_t emitStubForFunction(Function *F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000103 static void SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
104 uint64_t &FPRS, uint64_t &CCR);
105 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
106 uint64_t &FPRS, uint64_t &CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000107 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +0000108 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000109
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000110 };
111
112 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +0000113 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000114}
115
116/// addFunctionReference - This method is called when we need to emit the
117/// address of a function that has not yet been emitted, so we don't know the
118/// address. Instead, we emit a call to the CompilationCallback method, and
119/// keep track of where we are.
120///
Misha Brukmana2196c12003-06-04 20:01:13 +0000121uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000122 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000123 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000124}
125
Misha Brukman0897c602003-08-06 16:20:22 +0000126/// deleteFunctionReference - If we are emitting a far call, we already added a
127/// reference to the function, but it is now incorrect, since the address to the
128/// JIT resolver is too far away to be a simple call instruction. This is used
129/// to remove the address from the map.
130///
131void JITResolver::deleteFunctionReference(uint64_t Address) {
132 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
133 assert(I != LazyCodeGenMap.end() && "Not in map!");
134 LazyCodeGenMap.erase(I);
135}
136
Misha Brukmana2196c12003-06-04 20:01:13 +0000137uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
138 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000139 assert(I != LazyCodeGenMap.end() && "Not in map!");
140 Function *F = I->second;
141 LazyCodeGenMap.erase(I);
142 return MCE.forceCompilationOf(F);
143}
144
Misha Brukmana2196c12003-06-04 20:01:13 +0000145uint64_t JITResolver::getLazyResolver(Function *F) {
146 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000147 if (I != LazyResolverMap.end() && I->first == F) return I->second;
148
Misha Brukmana2196c12003-06-04 20:01:13 +0000149 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000150 LazyResolverMap.insert(I, std::make_pair(F, Stub));
151 return Stub;
152}
153
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000154void JITResolver::insertJumpAtAddr(int64_t JumpTarget, uint64_t &Addr) {
155 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << JumpTarget << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000156
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000157 // If the target function is close enough to fit into the 19bit disp of
158 // BA, we should use this version, as it's much cheaper to generate.
159 int64_t BranchTarget = (JumpTarget-Addr) >> 2;
160 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
161 TheJITResolver->insertFarJumpAtAddr(JumpTarget, Addr);
162 } else {
163 // ba <target>
164 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
165 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
166 Addr += 4;
167 delete I;
168
169 // nop
170 I = BuildMI(V9::NOP, 0);
171 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
172 delete I;
173 }
174}
175
176void JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000177 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000178 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
179 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000180
Misha Brukman0897c602003-08-06 16:20:22 +0000181 MachineInstr* BinaryCode[] = {
182 //
Misha Brukman0870e972003-08-06 22:19:18 +0000183 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000184 //
Misha Brukman0870e972003-08-06 22:19:18 +0000185 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
186 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
187 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
188 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
189 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
190 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
191 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
192 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000193 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000194 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
195 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
196 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
197 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
198 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
199 // nop ;; delay slot
200 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000201 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000202
Misha Brukman0897c602003-08-06 16:20:22 +0000203 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
204 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
205 delete BinaryCode[i];
206 Addr += 4;
207 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000208}
209
Misha Brukman103f0c32003-09-05 22:59:31 +0000210void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
211 uint64_t &FPRS, uint64_t &CCR) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000212#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000213
Misha Brukmancfd67c92003-08-29 04:22:54 +0000214#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000215 __asm__ __volatile__ (// Save condition-code registers
216 "stx %%fsr, %0;\n\t"
217 "rd %%fprs, %1;\n\t"
218 "rd %%ccr, %2;\n\t"
219 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000220#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000221
222 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000223 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
224 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
225 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
226 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
227 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
228 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
229 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
230 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
231 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
232 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
233 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
234 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
235 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
236 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
237 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
238 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
239 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000240
Misha Brukmancfd67c92003-08-29 04:22:54 +0000241 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000242 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000243 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000244 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
245 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
246 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
247 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
248 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
249 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000250 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
251 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
252 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
253 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
254 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
255 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
256 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
257 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000258#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000259}
Misha Brukmanfad49292003-08-15 00:26:50 +0000260
Misha Brukmanfad49292003-08-15 00:26:50 +0000261
Misha Brukman103f0c32003-09-05 22:59:31 +0000262void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
263 uint64_t &FPRS, uint64_t &CCR)
264{
Misha Brukmanfad49292003-08-15 00:26:50 +0000265#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000266
Misha Brukmancfd67c92003-08-29 04:22:54 +0000267#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000268 __asm__ __volatile__ (// Restore condition-code registers
269 "ldx %0, %%fsr;\n\t"
270 "wr %1, 0, %%fprs;\n\t"
271 "wr %2, 0, %%ccr;\n\t"
272 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000273#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000274
275 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000276 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
277 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
278 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
279 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
280 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
281 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
282 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
283 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
284 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
285 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
286 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
287 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
288 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
289 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
290 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
291 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
292 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000293
Misha Brukmancfd67c92003-08-29 04:22:54 +0000294 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000295 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
296 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
297 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
298 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
299 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
300 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
301 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
302 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000303 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
304 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
305 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
306 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
307 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
308 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
309 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
310 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000311#endif
312}
313
Misha Brukmancfd67c92003-08-29 04:22:54 +0000314void JITResolver::CompilationCallback() {
315 // Local space to save double registers
316 uint64_t DoubleFP[32];
Misha Brukman103f0c32003-09-05 22:59:31 +0000317 uint64_t FSR, FPRS, CCR;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000318
Misha Brukman103f0c32003-09-05 22:59:31 +0000319 SaveRegisters(DoubleFP, FSR, FPRS, CCR);
320 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000321
322 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000323 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000324 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000325 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000326 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000327#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000328 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
329 DEBUG(std::cerr << "Read i7 (return addr) = "
330 << std::hex << returnAddr << ", value: "
331 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000332#endif
333
Misha Brukman103f0c32003-09-05 22:59:31 +0000334 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
335 // trampoline function stub!!
336 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
337 int64_t OrigTarget = (Target-CameFrom1) >> 2;
338 if ((OrigCallInst & (1 << 30)) &&
339 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
340 {
341 // The original call instruction was CALL <immed>, which means we can
342 // overwrite it directly, since the offset will fit into 30 bits
343 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
344 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
345 delete C;
346 ++OverwrittenCalls;
347 } else {
348 ++UnmodifiedCalls;
349 }
350
Misha Brukman0897c602003-08-06 16:20:22 +0000351 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000352 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000353
Misha Brukman0897c602003-08-06 16:20:22 +0000354 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000355
Misha Brukman0897c602003-08-06 16:20:22 +0000356 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000357 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000358 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000359 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000360 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000361
362 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
363 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000364 // disables frame pointer elimination currently, even if it's dead. This is
365 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000366 alloca(42+Offset);
367 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000368
369 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000370 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000371 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
372 delete SV;
373 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000374 if (CodeInMem != SaveInst) {
375 std::cerr << "About to overwrite smthg not a save instr!";
376 abort();
377 }
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000378 // Overwrite it
379 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000380
Misha Brukman103f0c32003-09-05 22:59:31 +0000381 RestoreRegisters(DoubleFP, FSR, FPRS, CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000382
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000383 // Change the return address to re-execute the restore, then the jump.
384 // However, we can't just modify %i7 here, because we return to the function
385 // that will restore the floating-point registers for us. Thus, we just return
386 // the value we want it to be, and the parent will take care of setting %i7
387 // correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000388 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000389 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000390#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
391 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
392#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000393}
394
395/// emitStubForFunction - This method is used by the JIT when it needs to emit
396/// the address of a function for a function whose code has not yet been
397/// generated. In order to do this, it generates a stub which jumps to the lazy
398/// function compiler, which will eventually get fixed to call the function
399/// directly.
400///
Misha Brukmana2196c12003-06-04 20:01:13 +0000401uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000402 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000403
Misha Brukman8f122222003-06-06 00:26:11 +0000404 DEBUG(std::cerr << "Emitting stub at addr: 0x"
405 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000406
Misha Brukman0897c602003-08-06 16:20:22 +0000407 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
408
409 // restore %g0, 0, %g0
410 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
411 .addMReg(g0, MOTy::Def);
412 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
413 delete R;
414
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000415 // save %sp, -192, %sp
416 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
417 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
418 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000419
420 int64_t CurrPC = MCE.getCurrentPCValue();
421 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
422 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000423 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
424 // Since this is a far call, the actual address of the call is shifted
425 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000426 deleteFunctionReference(CurrPC);
427 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000428 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000429 // call CallTarget ;; invoke the callback
430 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
431 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
432 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000433
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000434 // nop ;; call delay slot
435 MachineInstr *Nop = BuildMI(V9::NOP, 0);
436 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
437 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000438
439 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000440 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000441
442 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000443 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000444}
445
446
Misha Brukmana2196c12003-06-04 20:01:13 +0000447SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
448 MachineCodeEmitter &M): TM(tm), MCE(M)
449{
450 TheJITResolver = new JITResolver(*this, M);
451}
452
453SparcV9CodeEmitter::~SparcV9CodeEmitter() {
454 delete TheJITResolver;
455}
456
457void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000458 // Output the constant in big endian byte order...
459 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000460 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000461 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000462 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000463 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000464}
465
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000466unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000467SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000468 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000469 const TargetRegInfo &RI = TM.getRegInfo();
470 unsigned regClass, regType = RI.getRegType(fakeReg);
471 // At least map fakeReg into its class
472 fakeReg = RI.getClassRegNum(fakeReg, regClass);
473
Misha Brukman9cedd432003-07-03 18:36:47 +0000474 switch (regClass) {
475 case UltraSparcRegInfo::IntRegClassID: {
476 // Sparc manual, p31
477 static const unsigned IntRegMap[] = {
478 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
479 8, 9, 10, 11, 12, 13, 15,
480 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
481 16, 17, 18, 19, 20, 21, 22, 23,
482 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
483 24, 25, 26, 27, 28, 29, 30, 31,
484 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
485 0, 1, 2, 3, 4, 5, 6, 7,
486 // "o6"
487 14
488 };
489
490 return IntRegMap[fakeReg];
491 break;
492 }
493 case UltraSparcRegInfo::FloatRegClassID: {
494 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000495 if (regType == UltraSparcRegInfo::FPSingleRegType) {
496 // only numbered 0-31, hence can already fit into 5 bits (and 6)
497 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
498 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000499 // FIXME: This assumes that we only have 5-bit register fields!
Misha Brukman173e2502003-07-14 23:26:03 +0000500 // From Sparc Manual, page 40.
501 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
502 fakeReg |= (fakeReg >> 5) & 1;
503 fakeReg &= 0x1f;
504 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
505 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000506 return fakeReg;
507 }
508 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000509 /* xcc, icc, ccr */
510 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000511
Misha Brukmandfbfc572003-07-16 20:30:40 +0000512 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
513 && "CC register out of bounds for IntCCReg map");
514 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
515 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000516 }
517 case UltraSparcRegInfo::FloatCCRegClassID: {
518 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
519 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
520 return fakeReg;
521 }
522 default:
523 assert(0 && "Invalid unified register number in getRegType");
524 return fakeReg;
525 }
526}
527
528
Misha Brukman07d45162003-07-15 19:09:43 +0000529// WARNING: if the call used the delay slot to do meaningful work, that's not
530// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000531inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000532 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000533 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
534 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000535
Misha Brukman0897c602003-08-06 16:20:22 +0000536 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000537 //
Misha Brukman0870e972003-08-06 22:19:18 +0000538 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000539 //
Misha Brukman0870e972003-08-06 22:19:18 +0000540 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
541 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
542 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
543 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000544 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000545 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
546 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
547 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000548 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000549 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
550 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
551 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000552 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000553 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000554 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000555 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000556 };
Misha Brukman07d45162003-07-15 19:09:43 +0000557
Misha Brukman0897c602003-08-06 16:20:22 +0000558 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
559 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000560 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000561 uint64_t CurrPC = MCE.getCurrentPCValue();
562 TheJITResolver->addFunctionReference(CurrPC, F);
563 // Remember that this is a far call, to subtract appropriate offset later
564 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
565 }
Misha Brukman07d45162003-07-15 19:09:43 +0000566
Misha Brukman0897c602003-08-06 16:20:22 +0000567 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
568 delete BinaryCode[i];
569 }
Misha Brukman07d45162003-07-15 19:09:43 +0000570}
571
Brian Gaeke682ce722003-10-20 15:15:17 +0000572void UltraSparc::replaceMachineCodeForFunction (void *Old, void *New) {
Brian Gaeke0522b082003-10-20 17:59:09 +0000573 assert (TheJITResolver &&
574 "Can only call replaceMachineCodeForFunction from within JIT");
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000575 uint64_t Target = (uint64_t)(intptr_t)New;
576 uint64_t CodeBegin = (uint64_t)(intptr_t)Old;
577 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000578}
Misha Brukman07d45162003-07-15 19:09:43 +0000579
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000580int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
581 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000582 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
583 // or things that get fixed up later by the JIT.
584
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000585 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000586 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000587 abort();
588 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000589 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000590 Value *V = MO.getVRegValue();
591 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000592 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000593 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000594 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000595 } else if (const Constant *C = dyn_cast<Constant>(V)) {
596 if (ConstantMap.find(C) != ConstantMap.end()) {
597 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000598 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000599 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000600 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000601 abort();
602 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000603 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
604 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000605 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000606 // external function calls, etc.?
607 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000608 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000609 if (F->isExternal()) {
610 // Sparc backend broken: this MO should be `ExternalSymbol'
611 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
612 } else {
613 rv = (int64_t)MCE.getGlobalValueAddress(F);
614 }
615 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000616 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000617 // Function has not yet been code generated!
618 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
619 // Delayed resolution...
620 rv = TheJITResolver->getLazyResolver(F);
621 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000622 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000623 }
624 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000625 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000626 if (rv == 0) {
627 if (Constant *C = ConstantPointerRef::get(GV)) {
628 if (ConstantMap.find(C) != ConstantMap.end()) {
629 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
630 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000631 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000632 << ", " << *V << " not found in ConstantMap!\n";
633 abort();
634 }
635 }
636 }
Misha Brukman0870e972003-08-06 22:19:18 +0000637 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000638 }
639 // The real target of the call is Addr = PC + (rv * 4)
640 // So undo that: give the instruction (Addr - PC) / 4
641 if (MI.getOpcode() == V9::CALL) {
642 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000643 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000644 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000645 int64_t CallInstTarget = (rv - CurrPC) >> 2;
646 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
647 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000648 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000649 // make an indirect jump-and-link
650 emitFarCall(rv);
651 // this invalidates the instruction so that the call with an incorrect
652 // address will not be emitted
653 rv = 0;
654 } else {
655 // The call fits into 30 bits, so just return the corrected address
656 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000657 }
Misha Brukman8f122222003-06-06 00:26:11 +0000658 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000659 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000660 } else {
661 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
662 abort();
663 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000664 } else if (MO.isPhysicalRegister() ||
665 MO.getType() == MachineOperand::MO_CCRegister)
666 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000667 // This is necessary because the Sparc backend doesn't actually lay out
668 // registers in the real fashion -- it skips those that it chooses not to
669 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000670 unsigned fakeReg = MO.getAllocatedRegNum();
671 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
672 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000673 << realRegByClass << " (LLC: "
674 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000675 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000676 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000677 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000678 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000679 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000680 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000681 rv = (int64_t)
682 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000683 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000684 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000685 // Duplicate code of the above case for VirtualRegister, BasicBlock...
686 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000687 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000688 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000689 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000690 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000691 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000692 // Sparc backend doesn't generate this (yet...)
693 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
694 abort();
695 } else if (MO.isFrameIndex()) {
696 // Sparc backend doesn't generate this (yet...)
697 int FrameIndex = MO.getFrameIndex();
698 std::cerr << "ERROR: Frame index unhandled.\n";
699 abort();
700 } else if (MO.isConstantPoolIndex()) {
701 // Sparc backend doesn't generate this (yet...)
702 std::cerr << "ERROR: Constant Pool index unhandled.\n";
703 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000704 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000705 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000706 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000707 }
708
709 // Finally, deal with the various bitfield-extracting functions that
710 // are used in SPARC assembly. (Some of these make no sense in combination
711 // with some of the above; we'll trust that the instruction selector
712 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000713 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000714 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000715 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000716 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000717 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000718 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000719 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000720 return rv >> 42;
721 } else { // (unadorned) val
722 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000723 }
724}
725
726unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
727 Val >>= bit;
728 return (Val & 1);
729}
730
Misha Brukman3de36f52003-05-27 20:07:58 +0000731bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000732 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000733 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000734 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000735 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000736
Misha Brukmana2196c12003-06-04 20:01:13 +0000737 // The Sparc backend does not use MachineConstantPool;
738 // instead, it has its own constant pool implementation.
739 // We create a new MachineConstantPool here to be compatible with the emitter.
740 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000741 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
742 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
743 E = pool.end(); I != E; ++I)
744 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000745 Constant *C = (Constant*)*I;
746 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000747 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000748 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000749 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000750 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000751
Misha Brukman3de36f52003-05-27 20:07:58 +0000752 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
753 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000754 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000755
Misha Brukman9cedd432003-07-03 18:36:47 +0000756 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000757 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000758
759 // Resolve branches to BasicBlocks for the entire function
760 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
761 long Location = BBLocations[BBRefs[i].first];
762 unsigned *Ref = BBRefs[i].second.first;
763 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000764 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
765 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000766 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
767 MachineOperand &op = MI->getOperand(ii);
768 if (op.isPCRelativeDisp()) {
769 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000770 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000771 // Location is the target of the branch
772 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000773 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000774 // Save the flags.
775 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
776 if (op.opLoBits32()) { loBits32=true; }
777 if (op.opHiBits32()) { hiBits32=true; }
778 if (op.opLoBits64()) { loBits64=true; }
779 if (op.opHiBits64()) { hiBits64=true; }
780 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
781 branchTarget);
782 if (loBits32) { MI->setOperandLo32(ii); }
783 else if (hiBits32) { MI->setOperandHi32(ii); }
784 else if (loBits64) { MI->setOperandLo64(ii); }
785 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000786 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000787 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
788 *Ref = fixedInstr;
789 break;
790 }
791 }
792 }
793 BBRefs.clear();
794 BBLocations.clear();
795
Misha Brukman3de36f52003-05-27 20:07:58 +0000796 return false;
797}
798
799void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000800 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000801 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000802 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
803 unsigned binCode = getBinaryCodeForInstr(**I);
804 if (binCode == (1 << 30)) {
805 // this is an invalid call: the addr is out of bounds. that means a code
806 // sequence has already been emitted, and this is a no-op
807 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
808 } else {
809 emitWord(binCode);
810 }
811 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000812}
813
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000814void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
815 bool isPCRelative)
816{
817 if (isPCRelative) { // must be a call, this is a major hack!
818 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000819 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
820 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000821 // The real target of the call is Addr = PC + (target * 4)
822 // CurByte is the PC, Addr we just received
823 return (void*) (((long)Addr - (long)CurByte) >> 2);
824 } else {
825 if (Function *F = dyn_cast<Function>(V)) {
826 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000827 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000828 cast<Function>(V));
829 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000830 return
831 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000832
833 } else if (Constant *C = ConstantPointerRef::get(V)) {
834 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000835 return (void*)
836 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000837 } else {
838 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
839 << ", " << *V << " not found in ConstantMap!\n";
840 abort();
841 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000842 } else {
843 std::cerr << "Unhandled global: " << *V << "\n";
844 abort();
845 }
846 }
847 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000848 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000849 }
850}
851
Misha Brukman3de36f52003-05-27 20:07:58 +0000852#include "SparcV9CodeEmitter.inc"