blob: f56e7f3b63f3d14f8ef9e7a8b9f836b6495b421d [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
Brian Gaeked0fde302003-11-11 22:41:34 +000041namespace llvm {
42
Misha Brukman103f0c32003-09-05 22:59:31 +000043namespace {
44 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
45 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
46 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
47}
48
Brian Gaekee69f7272003-08-14 06:04:59 +000049bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000050 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000051 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000052 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000053 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000054 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000055 return false;
56}
57
Misha Brukmanf86aaa82003-06-02 04:12:39 +000058namespace {
59 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000060 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000061 MachineCodeEmitter &MCE;
62
Misha Brukman0897c602003-08-06 16:20:22 +000063 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
64 /// lazily resolved.
65 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000066 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000067
Misha Brukman0897c602003-08-06 16:20:22 +000068 /// LazyResolverMap - Keep track of the lazy resolver created for a
69 /// particular function so that we can reuse them if necessary.
70 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000071 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000072
73 public:
74 enum CallType { ShortCall, FarCall };
75
76 private:
77 /// We need to keep track of whether we used a simple call or a far call
78 /// (many instructions) in sequence. This means we need to keep track of
79 /// what type of stub we generate.
80 static std::map<uint64_t, CallType> LazyCallFlavor;
81
Misha Brukmanf86aaa82003-06-02 04:12:39 +000082 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000083 JITResolver(SparcV9CodeEmitter &V9,
84 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
85 uint64_t getLazyResolver(Function *F);
86 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000087 void deleteFunctionReference(uint64_t Address);
88 void addCallFlavor(uint64_t Address, CallType Flavor) {
89 LazyCallFlavor[Address] = Flavor;
90 }
Misha Brukmana2196c12003-06-04 20:01:13 +000091
92 // Utility functions for accessing data from static callback
93 uint64_t getCurrentPCValue() {
94 return MCE.getCurrentPCValue();
95 }
96 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
97 return SparcV9.getBinaryCodeForInstr(MI);
98 }
99
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000100 inline void insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
101 void insertJumpAtAddr(int64_t Value, uint64_t &Addr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000102
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000103 private:
Misha Brukmana2196c12003-06-04 20:01:13 +0000104 uint64_t emitStubForFunction(Function *F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000105 static void SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
106 uint64_t &FPRS, uint64_t &CCR);
107 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
108 uint64_t &FPRS, uint64_t &CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000109 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +0000110 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000111
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000112 };
113
114 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +0000115 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000116}
117
118/// addFunctionReference - This method is called when we need to emit the
119/// address of a function that has not yet been emitted, so we don't know the
120/// address. Instead, we emit a call to the CompilationCallback method, and
121/// keep track of where we are.
122///
Misha Brukmana2196c12003-06-04 20:01:13 +0000123uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000124 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000125 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000126}
127
Misha Brukman0897c602003-08-06 16:20:22 +0000128/// deleteFunctionReference - If we are emitting a far call, we already added a
129/// reference to the function, but it is now incorrect, since the address to the
130/// JIT resolver is too far away to be a simple call instruction. This is used
131/// to remove the address from the map.
132///
133void JITResolver::deleteFunctionReference(uint64_t Address) {
134 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
135 assert(I != LazyCodeGenMap.end() && "Not in map!");
136 LazyCodeGenMap.erase(I);
137}
138
Misha Brukmana2196c12003-06-04 20:01:13 +0000139uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
140 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000141 assert(I != LazyCodeGenMap.end() && "Not in map!");
142 Function *F = I->second;
143 LazyCodeGenMap.erase(I);
144 return MCE.forceCompilationOf(F);
145}
146
Misha Brukmana2196c12003-06-04 20:01:13 +0000147uint64_t JITResolver::getLazyResolver(Function *F) {
148 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000149 if (I != LazyResolverMap.end() && I->first == F) return I->second;
150
Misha Brukmana2196c12003-06-04 20:01:13 +0000151 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000152 LazyResolverMap.insert(I, std::make_pair(F, Stub));
153 return Stub;
154}
155
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000156void JITResolver::insertJumpAtAddr(int64_t JumpTarget, uint64_t &Addr) {
157 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << JumpTarget << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000158
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000159 // If the target function is close enough to fit into the 19bit disp of
160 // BA, we should use this version, as it's much cheaper to generate.
161 int64_t BranchTarget = (JumpTarget-Addr) >> 2;
162 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
163 TheJITResolver->insertFarJumpAtAddr(JumpTarget, Addr);
164 } else {
165 // ba <target>
166 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
167 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
168 Addr += 4;
169 delete I;
170
171 // nop
172 I = BuildMI(V9::NOP, 0);
173 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
174 delete I;
175 }
176}
177
178void JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000179 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000180 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
181 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000182
Misha Brukman0897c602003-08-06 16:20:22 +0000183 MachineInstr* BinaryCode[] = {
184 //
Misha Brukman0870e972003-08-06 22:19:18 +0000185 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000186 //
Misha Brukman0870e972003-08-06 22:19:18 +0000187 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
188 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
189 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
190 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
191 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
192 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
193 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
194 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000195 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000196 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
197 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
198 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
199 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
200 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
201 // nop ;; delay slot
202 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000203 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000204
Misha Brukman0897c602003-08-06 16:20:22 +0000205 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
206 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
207 delete BinaryCode[i];
208 Addr += 4;
209 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000210}
211
Misha Brukman103f0c32003-09-05 22:59:31 +0000212void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
213 uint64_t &FPRS, uint64_t &CCR) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000214#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000215
Misha Brukmancfd67c92003-08-29 04:22:54 +0000216#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000217 __asm__ __volatile__ (// Save condition-code registers
218 "stx %%fsr, %0;\n\t"
219 "rd %%fprs, %1;\n\t"
220 "rd %%ccr, %2;\n\t"
221 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000222#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000223
224 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000225 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
226 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
227 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
228 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
229 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
230 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
231 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
232 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
233 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
234 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
235 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
236 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
237 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
238 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
239 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
240 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
241 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000242
Misha Brukmancfd67c92003-08-29 04:22:54 +0000243 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000244 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000245 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000246 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
247 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
248 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
249 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
250 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
251 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000252 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
253 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
254 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
255 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
256 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
257 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
258 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
259 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000260#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000261}
Misha Brukmanfad49292003-08-15 00:26:50 +0000262
Misha Brukmanfad49292003-08-15 00:26:50 +0000263
Misha Brukman103f0c32003-09-05 22:59:31 +0000264void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
265 uint64_t &FPRS, uint64_t &CCR)
266{
Misha Brukmanfad49292003-08-15 00:26:50 +0000267#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000268
Misha Brukmancfd67c92003-08-29 04:22:54 +0000269#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000270 __asm__ __volatile__ (// Restore condition-code registers
271 "ldx %0, %%fsr;\n\t"
272 "wr %1, 0, %%fprs;\n\t"
273 "wr %2, 0, %%ccr;\n\t"
274 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000275#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000276
277 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000278 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
279 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
280 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
281 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
282 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
283 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
284 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
285 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
286 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
287 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
288 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
289 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
290 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
291 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
292 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
293 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
294 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000295
Misha Brukmancfd67c92003-08-29 04:22:54 +0000296 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000297 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
298 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
299 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
300 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
301 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
302 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
303 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
304 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000305 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
306 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
307 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
308 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
309 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
310 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
311 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
312 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000313#endif
314}
315
Misha Brukmancfd67c92003-08-29 04:22:54 +0000316void JITResolver::CompilationCallback() {
317 // Local space to save double registers
318 uint64_t DoubleFP[32];
Misha Brukman103f0c32003-09-05 22:59:31 +0000319 uint64_t FSR, FPRS, CCR;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000320
Misha Brukman103f0c32003-09-05 22:59:31 +0000321 SaveRegisters(DoubleFP, FSR, FPRS, CCR);
322 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000323
324 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000325 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000326 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000327 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000328 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000329#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000330 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
331 DEBUG(std::cerr << "Read i7 (return addr) = "
332 << std::hex << returnAddr << ", value: "
333 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000334#endif
335
Misha Brukman103f0c32003-09-05 22:59:31 +0000336 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
337 // trampoline function stub!!
338 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
339 int64_t OrigTarget = (Target-CameFrom1) >> 2;
340 if ((OrigCallInst & (1 << 30)) &&
341 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
342 {
343 // The original call instruction was CALL <immed>, which means we can
344 // overwrite it directly, since the offset will fit into 30 bits
345 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
346 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
347 delete C;
348 ++OverwrittenCalls;
349 } else {
350 ++UnmodifiedCalls;
351 }
352
Misha Brukman0897c602003-08-06 16:20:22 +0000353 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000354 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000355
Misha Brukman0897c602003-08-06 16:20:22 +0000356 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000357
Misha Brukman0897c602003-08-06 16:20:22 +0000358 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000359 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000360 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000361 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000362 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000363
364 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
365 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000366 // disables frame pointer elimination currently, even if it's dead. This is
367 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000368 alloca(42+Offset);
369 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000370
371 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000372 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000373 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
374 delete SV;
375 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000376 if (CodeInMem != SaveInst) {
377 std::cerr << "About to overwrite smthg not a save instr!";
378 abort();
379 }
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000380 // Overwrite it
381 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000382
Misha Brukman103f0c32003-09-05 22:59:31 +0000383 RestoreRegisters(DoubleFP, FSR, FPRS, CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000384
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000385 // Change the return address to re-execute the restore, then the jump.
386 // However, we can't just modify %i7 here, because we return to the function
387 // that will restore the floating-point registers for us. Thus, we just return
388 // the value we want it to be, and the parent will take care of setting %i7
389 // correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000390 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000391 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000392#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
393 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
394#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000395}
396
397/// emitStubForFunction - This method is used by the JIT when it needs to emit
398/// the address of a function for a function whose code has not yet been
399/// generated. In order to do this, it generates a stub which jumps to the lazy
400/// function compiler, which will eventually get fixed to call the function
401/// directly.
402///
Misha Brukmana2196c12003-06-04 20:01:13 +0000403uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000404 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000405
Misha Brukman8f122222003-06-06 00:26:11 +0000406 DEBUG(std::cerr << "Emitting stub at addr: 0x"
407 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000408
Misha Brukman0897c602003-08-06 16:20:22 +0000409 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
410
411 // restore %g0, 0, %g0
412 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
413 .addMReg(g0, MOTy::Def);
414 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
415 delete R;
416
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000417 // save %sp, -192, %sp
418 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
419 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
420 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000421
422 int64_t CurrPC = MCE.getCurrentPCValue();
423 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
424 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000425 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
426 // Since this is a far call, the actual address of the call is shifted
427 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000428 deleteFunctionReference(CurrPC);
429 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000430 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000431 // call CallTarget ;; invoke the callback
432 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
433 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
434 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000435
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000436 // nop ;; call delay slot
437 MachineInstr *Nop = BuildMI(V9::NOP, 0);
438 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
439 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000440
441 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000442 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000443
444 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000445 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000446}
447
Misha Brukmana2196c12003-06-04 20:01:13 +0000448SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
449 MachineCodeEmitter &M): TM(tm), MCE(M)
450{
451 TheJITResolver = new JITResolver(*this, M);
452}
453
454SparcV9CodeEmitter::~SparcV9CodeEmitter() {
455 delete TheJITResolver;
456}
457
458void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000459 // Output the constant in big endian byte order...
460 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000461 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000462 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000463 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000464 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000465}
466
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000467unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000468SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000469 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000470 const TargetRegInfo &RI = TM.getRegInfo();
471 unsigned regClass, regType = RI.getRegType(fakeReg);
472 // At least map fakeReg into its class
473 fakeReg = RI.getClassRegNum(fakeReg, regClass);
474
Misha Brukman9cedd432003-07-03 18:36:47 +0000475 switch (regClass) {
476 case UltraSparcRegInfo::IntRegClassID: {
477 // Sparc manual, p31
478 static const unsigned IntRegMap[] = {
479 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
480 8, 9, 10, 11, 12, 13, 15,
481 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
482 16, 17, 18, 19, 20, 21, 22, 23,
483 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
484 24, 25, 26, 27, 28, 29, 30, 31,
485 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
486 0, 1, 2, 3, 4, 5, 6, 7,
487 // "o6"
488 14
489 };
490
491 return IntRegMap[fakeReg];
492 break;
493 }
494 case UltraSparcRegInfo::FloatRegClassID: {
495 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000496 if (regType == UltraSparcRegInfo::FPSingleRegType) {
497 // only numbered 0-31, hence can already fit into 5 bits (and 6)
498 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
499 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000500 // FIXME: This assumes that we only have 5-bit register fields!
Misha Brukman173e2502003-07-14 23:26:03 +0000501 // From Sparc Manual, page 40.
502 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
503 fakeReg |= (fakeReg >> 5) & 1;
504 fakeReg &= 0x1f;
505 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
506 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000507 return fakeReg;
508 }
509 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000510 /* xcc, icc, ccr */
511 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000512
Misha Brukmandfbfc572003-07-16 20:30:40 +0000513 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
514 && "CC register out of bounds for IntCCReg map");
515 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
516 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000517 }
518 case UltraSparcRegInfo::FloatCCRegClassID: {
519 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
520 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
521 return fakeReg;
522 }
523 default:
524 assert(0 && "Invalid unified register number in getRegType");
525 return fakeReg;
526 }
527}
528
529
Misha Brukman07d45162003-07-15 19:09:43 +0000530// WARNING: if the call used the delay slot to do meaningful work, that's not
531// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000532inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000533 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000534 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
535 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000536
Misha Brukman0897c602003-08-06 16:20:22 +0000537 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000538 //
Misha Brukman0870e972003-08-06 22:19:18 +0000539 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000540 //
Misha Brukman0870e972003-08-06 22:19:18 +0000541 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
542 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
543 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
544 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000545 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000546 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
547 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
548 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000549 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000550 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
551 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
552 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000553 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000554 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000555 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000556 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000557 };
Misha Brukman07d45162003-07-15 19:09:43 +0000558
Misha Brukman0897c602003-08-06 16:20:22 +0000559 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
560 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000561 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000562 uint64_t CurrPC = MCE.getCurrentPCValue();
563 TheJITResolver->addFunctionReference(CurrPC, F);
564 // Remember that this is a far call, to subtract appropriate offset later
565 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
566 }
Misha Brukman07d45162003-07-15 19:09:43 +0000567
Misha Brukman0897c602003-08-06 16:20:22 +0000568 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
569 delete BinaryCode[i];
570 }
Misha Brukman07d45162003-07-15 19:09:43 +0000571}
572
Brian Gaeke682ce722003-10-20 15:15:17 +0000573void UltraSparc::replaceMachineCodeForFunction (void *Old, void *New) {
Brian Gaeke0522b082003-10-20 17:59:09 +0000574 assert (TheJITResolver &&
575 "Can only call replaceMachineCodeForFunction from within JIT");
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000576 uint64_t Target = (uint64_t)(intptr_t)New;
577 uint64_t CodeBegin = (uint64_t)(intptr_t)Old;
578 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000579}
Misha Brukman07d45162003-07-15 19:09:43 +0000580
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000581int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
582 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000583 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
584 // or things that get fixed up later by the JIT.
585
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000586 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000587 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000588 abort();
589 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000590 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000591 Value *V = MO.getVRegValue();
592 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000593 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000594 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000595 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000596 } else if (const Constant *C = dyn_cast<Constant>(V)) {
Misha Brukman52709452003-11-07 21:07:30 +0000597 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
598 rv = CI->getRawValue() - MCE.getCurrentPCValue();
599 } else {
600 std::cerr << "Cannot have non-integral const in instruction: "
601 << *C;
602 abort();
603 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000604 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
605 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000606 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000607 // external function calls, etc.?
608 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000609 DEBUG(std::cerr << "Function: ");
Brian Gaeke1654bdb2003-11-09 07:08:34 +0000610 // NOTE: This results in stubs being generated even for
611 // external, native functions, which is not optimal. See PR103.
612 rv = (int64_t)MCE.getGlobalValueAddress(F);
Misha Brukmana2196c12003-06-04 20:01:13 +0000613 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000614 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000615 // Function has not yet been code generated!
616 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
617 // Delayed resolution...
618 rv = TheJITResolver->getLazyResolver(F);
619 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000620 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000621 }
622 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000623 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukman0870e972003-08-06 22:19:18 +0000624 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000625 }
626 // The real target of the call is Addr = PC + (rv * 4)
627 // So undo that: give the instruction (Addr - PC) / 4
628 if (MI.getOpcode() == V9::CALL) {
629 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000630 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000631 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000632 int64_t CallInstTarget = (rv - CurrPC) >> 2;
633 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
634 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000635 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000636 // make an indirect jump-and-link
637 emitFarCall(rv);
638 // this invalidates the instruction so that the call with an incorrect
639 // address will not be emitted
640 rv = 0;
641 } else {
642 // The call fits into 30 bits, so just return the corrected address
643 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000644 }
Misha Brukman8f122222003-06-06 00:26:11 +0000645 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000646 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000647 } else {
648 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
649 abort();
650 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000651 } else if (MO.isPhysicalRegister() ||
652 MO.getType() == MachineOperand::MO_CCRegister)
653 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000654 // This is necessary because the Sparc backend doesn't actually lay out
655 // registers in the real fashion -- it skips those that it chooses not to
656 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000657 unsigned fakeReg = MO.getAllocatedRegNum();
658 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
659 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000660 << realRegByClass << " (LLC: "
661 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000662 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000663 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000664 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000665 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000666 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000667 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000668 rv = (int64_t)
669 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000670 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000671 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000672 // Duplicate code of the above case for VirtualRegister, BasicBlock...
673 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000674 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000675 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000676 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000677 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000678 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000679 // Sparc backend doesn't generate this (yet...)
680 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
681 abort();
682 } else if (MO.isFrameIndex()) {
683 // Sparc backend doesn't generate this (yet...)
684 int FrameIndex = MO.getFrameIndex();
685 std::cerr << "ERROR: Frame index unhandled.\n";
686 abort();
687 } else if (MO.isConstantPoolIndex()) {
Misha Brukmane5ad8152003-11-07 18:06:26 +0000688 unsigned Index = MO.getConstantPoolIndex();
689 rv = MCE.getConstantPoolEntryAddress(Index);
Misha Brukman3de36f52003-05-27 20:07:58 +0000690 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000691 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000692 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000693 }
694
695 // Finally, deal with the various bitfield-extracting functions that
696 // are used in SPARC assembly. (Some of these make no sense in combination
697 // with some of the above; we'll trust that the instruction selector
698 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000699 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000700 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000701 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000702 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000703 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000704 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000705 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000706 return rv >> 42;
707 } else { // (unadorned) val
708 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000709 }
710}
711
712unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
713 Val >>= bit;
714 return (Val & 1);
715}
716
Misha Brukman3de36f52003-05-27 20:07:58 +0000717bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000718 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000719 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000720 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000721 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000722
Misha Brukmane5ad8152003-11-07 18:06:26 +0000723 MCE.emitConstantPool(MF.getConstantPool());
Misha Brukman3de36f52003-05-27 20:07:58 +0000724 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
725 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000726 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000727
Misha Brukman9cedd432003-07-03 18:36:47 +0000728 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000729
730 // Resolve branches to BasicBlocks for the entire function
731 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
732 long Location = BBLocations[BBRefs[i].first];
733 unsigned *Ref = BBRefs[i].second.first;
734 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000735 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
736 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000737 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
738 MachineOperand &op = MI->getOperand(ii);
739 if (op.isPCRelativeDisp()) {
740 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000741 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000742 // Location is the target of the branch
743 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000744 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000745 // Save the flags.
746 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
747 if (op.opLoBits32()) { loBits32=true; }
748 if (op.opHiBits32()) { hiBits32=true; }
749 if (op.opLoBits64()) { loBits64=true; }
750 if (op.opHiBits64()) { hiBits64=true; }
751 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
752 branchTarget);
753 if (loBits32) { MI->setOperandLo32(ii); }
754 else if (hiBits32) { MI->setOperandHi32(ii); }
755 else if (loBits64) { MI->setOperandLo64(ii); }
756 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000757 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000758 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
759 *Ref = fixedInstr;
760 break;
761 }
762 }
763 }
764 BBRefs.clear();
765 BBLocations.clear();
766
Misha Brukman3de36f52003-05-27 20:07:58 +0000767 return false;
768}
769
770void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000771 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000772 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000773 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
774 unsigned binCode = getBinaryCodeForInstr(**I);
775 if (binCode == (1 << 30)) {
776 // this is an invalid call: the addr is out of bounds. that means a code
777 // sequence has already been emitted, and this is a no-op
778 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
779 } else {
780 emitWord(binCode);
781 }
782 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000783}
784
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000785void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
786 bool isPCRelative)
787{
788 if (isPCRelative) { // must be a call, this is a major hack!
789 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000790 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
791 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000792 // The real target of the call is Addr = PC + (target * 4)
793 // CurByte is the PC, Addr we just received
794 return (void*) (((long)Addr - (long)CurByte) >> 2);
795 } else {
796 if (Function *F = dyn_cast<Function>(V)) {
797 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000798 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000799 cast<Function>(V));
800 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000801 return
802 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000803 } else {
804 std::cerr << "Unhandled global: " << *V << "\n";
805 abort();
806 }
807 }
808 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000809 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000810 }
811}
812
Misha Brukmanc6b15842003-11-13 00:23:05 +0000813#include "SparcV9CodeEmitter.inc"
814
Brian Gaeked0fde302003-11-11 22:41:34 +0000815} // End llvm namespace
816