blob: b69408d2bacc01edbb1a21c95c36f704f52e5272 [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"
Alkis Evlogimenosaf862112004-02-11 05:55:00 +000032#include "llvm/Target/MRegisterInfo.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000033#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000034#include "llvm/Target/TargetData.h"
Chris Lattner556d89d2003-08-01 22:19:03 +000035#include "Support/Debug.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000036#include "Support/hash_set"
Misha Brukman103f0c32003-09-05 22:59:31 +000037#include "Support/Statistic.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000038#include "SparcInternals.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000039#include "SparcTargetMachine.h"
40#include "SparcRegInfo.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000041#include "SparcV9CodeEmitter.h"
Misha Brukmancfd67c92003-08-29 04:22:54 +000042#include "Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000043
Brian Gaeked0fde302003-11-11 22:41:34 +000044namespace llvm {
45
Misha Brukman103f0c32003-09-05 22:59:31 +000046namespace {
47 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
48 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
49 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
50}
51
Misha Brukmand71295a2003-12-17 22:04:00 +000052bool SparcTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
53 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000054 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000055 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000056 PM.add(new SparcV9CodeEmitter(*this, *M));
Chris Lattner583b9d82003-12-20 09:17:40 +000057 PM.add(createSparcMachineCodeDestructionPass()); //Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000058 return false;
59}
60
Misha Brukmanf86aaa82003-06-02 04:12:39 +000061namespace {
62 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000063 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000064 MachineCodeEmitter &MCE;
65
Misha Brukman0897c602003-08-06 16:20:22 +000066 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
67 /// lazily resolved.
68 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000069 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000070
Misha Brukman0897c602003-08-06 16:20:22 +000071 /// LazyResolverMap - Keep track of the lazy resolver created for a
72 /// particular function so that we can reuse them if necessary.
73 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000074 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000075
76 public:
77 enum CallType { ShortCall, FarCall };
78
79 private:
80 /// We need to keep track of whether we used a simple call or a far call
81 /// (many instructions) in sequence. This means we need to keep track of
82 /// what type of stub we generate.
83 static std::map<uint64_t, CallType> LazyCallFlavor;
84
Misha Brukmanf86aaa82003-06-02 04:12:39 +000085 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000086 JITResolver(SparcV9CodeEmitter &V9,
87 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
88 uint64_t getLazyResolver(Function *F);
89 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000090 void deleteFunctionReference(uint64_t Address);
91 void addCallFlavor(uint64_t Address, CallType Flavor) {
92 LazyCallFlavor[Address] = Flavor;
93 }
Misha Brukmana2196c12003-06-04 20:01:13 +000094
95 // Utility functions for accessing data from static callback
96 uint64_t getCurrentPCValue() {
97 return MCE.getCurrentPCValue();
98 }
99 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
100 return SparcV9.getBinaryCodeForInstr(MI);
101 }
102
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000103 inline void insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
104 void insertJumpAtAddr(int64_t Value, uint64_t &Addr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000105
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000106 private:
Misha Brukmana2196c12003-06-04 20:01:13 +0000107 uint64_t emitStubForFunction(Function *F);
Misha Brukmand71295a2003-12-17 22:04:00 +0000108 static void SaveRegisters(uint64_t DoubleFP[], uint64_t CC[],
109 uint64_t Globals[]);
110 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t CC[],
111 uint64_t Globals[]);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000112 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +0000113 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000114
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000115 };
116
117 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +0000118 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000119}
120
121/// addFunctionReference - This method is called when we need to emit the
122/// address of a function that has not yet been emitted, so we don't know the
123/// address. Instead, we emit a call to the CompilationCallback method, and
124/// keep track of where we are.
125///
Misha Brukmana2196c12003-06-04 20:01:13 +0000126uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000127 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000128 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000129}
130
Misha Brukman0897c602003-08-06 16:20:22 +0000131/// deleteFunctionReference - If we are emitting a far call, we already added a
132/// reference to the function, but it is now incorrect, since the address to the
133/// JIT resolver is too far away to be a simple call instruction. This is used
134/// to remove the address from the map.
135///
136void JITResolver::deleteFunctionReference(uint64_t Address) {
137 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
138 assert(I != LazyCodeGenMap.end() && "Not in map!");
139 LazyCodeGenMap.erase(I);
140}
141
Misha Brukmana2196c12003-06-04 20:01:13 +0000142uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
143 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000144 assert(I != LazyCodeGenMap.end() && "Not in map!");
145 Function *F = I->second;
146 LazyCodeGenMap.erase(I);
147 return MCE.forceCompilationOf(F);
148}
149
Misha Brukmana2196c12003-06-04 20:01:13 +0000150uint64_t JITResolver::getLazyResolver(Function *F) {
151 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000152 if (I != LazyResolverMap.end() && I->first == F) return I->second;
153
Misha Brukmana2196c12003-06-04 20:01:13 +0000154 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000155 LazyResolverMap.insert(I, std::make_pair(F, Stub));
156 return Stub;
157}
158
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000159void JITResolver::insertJumpAtAddr(int64_t JumpTarget, uint64_t &Addr) {
160 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << JumpTarget << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000161
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000162 // If the target function is close enough to fit into the 19bit disp of
163 // BA, we should use this version, as it's much cheaper to generate.
164 int64_t BranchTarget = (JumpTarget-Addr) >> 2;
165 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
166 TheJITResolver->insertFarJumpAtAddr(JumpTarget, Addr);
167 } else {
168 // ba <target>
169 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
170 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
171 Addr += 4;
172 delete I;
173
174 // nop
175 I = BuildMI(V9::NOP, 0);
176 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*I);
177 delete I;
178 }
179}
180
181void JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000182 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000183 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
184 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000185
Misha Brukman0897c602003-08-06 16:20:22 +0000186 MachineInstr* BinaryCode[] = {
187 //
Misha Brukman0870e972003-08-06 22:19:18 +0000188 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000189 //
Misha Brukman0870e972003-08-06 22:19:18 +0000190 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
191 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
192 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
193 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
194 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
195 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
196 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
197 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000198 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000199 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
200 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
201 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
202 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
203 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
204 // nop ;; delay slot
205 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000206 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000207
Misha Brukman0897c602003-08-06 16:20:22 +0000208 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
209 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
210 delete BinaryCode[i];
211 Addr += 4;
212 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000213}
214
Misha Brukmand71295a2003-12-17 22:04:00 +0000215void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t CC[],
216 uint64_t Globals[]) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000217#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000218
219 __asm__ __volatile__ (// Save condition-code registers
220 "stx %%fsr, %0;\n\t"
221 "rd %%fprs, %1;\n\t"
222 "rd %%ccr, %2;\n\t"
Misha Brukmand71295a2003-12-17 22:04:00 +0000223 : "=m"(CC[0]), "=r"(CC[1]), "=r"(CC[2]));
224
225 __asm__ __volatile__ (// Save globals g1 and g5
226 "stx %%g1, %0;\n\t"
227 "stx %%g5, %0;\n\t"
228 : "=m"(Globals[0]), "=m"(Globals[1]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000229
230 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000231 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
232 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
233 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
234 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
235 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
236 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
237 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
238 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
239 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
240 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
241 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
242 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
243 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
244 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
245 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
246 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
247 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000248
Misha Brukmancfd67c92003-08-29 04:22:54 +0000249 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000250 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000251 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000252 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
253 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
254 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
255 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
256 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
257 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000258 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
259 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
260 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
261 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
262 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
263 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
264 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
265 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000266#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000267}
Misha Brukmanfad49292003-08-15 00:26:50 +0000268
Misha Brukmanfad49292003-08-15 00:26:50 +0000269
Misha Brukmand71295a2003-12-17 22:04:00 +0000270void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t CC[],
271 uint64_t Globals[])
Misha Brukman103f0c32003-09-05 22:59:31 +0000272{
Misha Brukmanfad49292003-08-15 00:26:50 +0000273#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000274
Misha Brukmanfad49292003-08-15 00:26:50 +0000275 __asm__ __volatile__ (// Restore condition-code registers
276 "ldx %0, %%fsr;\n\t"
277 "wr %1, 0, %%fprs;\n\t"
278 "wr %2, 0, %%ccr;\n\t"
Misha Brukmand71295a2003-12-17 22:04:00 +0000279 :: "m"(CC[0]), "r"(CC[1]), "r"(CC[2]));
280
281 __asm__ __volatile__ (// Restore globals g1 and g5
282 "ldx %0, %%g1;\n\t"
283 "ldx %0, %%g5;\n\t"
284 :: "m"(Globals[0]), "m"(Globals[1]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000285
286 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000287 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
288 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
289 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
290 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
291 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
292 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
293 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
294 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
295 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
296 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
297 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
298 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
299 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
300 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
301 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
302 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
303 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000304
Misha Brukmancfd67c92003-08-29 04:22:54 +0000305 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000306 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
307 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
308 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
309 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
310 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
311 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
312 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
313 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000314 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
315 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
316 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
317 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
318 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
319 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
320 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
321 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000322#endif
323}
324
Misha Brukmancfd67c92003-08-29 04:22:54 +0000325void JITResolver::CompilationCallback() {
Misha Brukmand71295a2003-12-17 22:04:00 +0000326 // Local space to save the registers
Misha Brukmancfd67c92003-08-29 04:22:54 +0000327 uint64_t DoubleFP[32];
Misha Brukmand71295a2003-12-17 22:04:00 +0000328 uint64_t CC[3];
329 uint64_t Globals[2];
Misha Brukmancfd67c92003-08-29 04:22:54 +0000330
Misha Brukmand71295a2003-12-17 22:04:00 +0000331 SaveRegisters(DoubleFP, CC, Globals);
Misha Brukman103f0c32003-09-05 22:59:31 +0000332 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000333
334 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000335 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000336 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000337 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000338 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000339#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000340 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
341 DEBUG(std::cerr << "Read i7 (return addr) = "
342 << std::hex << returnAddr << ", value: "
343 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000344#endif
345
Misha Brukman103f0c32003-09-05 22:59:31 +0000346 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
347 // trampoline function stub!!
348 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
349 int64_t OrigTarget = (Target-CameFrom1) >> 2;
350 if ((OrigCallInst & (1 << 30)) &&
351 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
352 {
353 // The original call instruction was CALL <immed>, which means we can
354 // overwrite it directly, since the offset will fit into 30 bits
355 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
356 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
357 delete C;
358 ++OverwrittenCalls;
359 } else {
360 ++UnmodifiedCalls;
361 }
362
Misha Brukman0897c602003-08-06 16:20:22 +0000363 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000364 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000365
Misha Brukman0897c602003-08-06 16:20:22 +0000366 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000367
Misha Brukman0897c602003-08-06 16:20:22 +0000368 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000369 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000370 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000371 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000372 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000373
374 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
375 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000376 // disables frame pointer elimination currently, even if it's dead. This is
377 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000378 alloca(42+Offset);
379 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000380
381 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000382 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000383 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
384 delete SV;
385 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000386 if (CodeInMem != SaveInst) {
387 std::cerr << "About to overwrite smthg not a save instr!";
388 abort();
389 }
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000390 // Overwrite it
391 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000392
Misha Brukmana59f41f2003-11-21 23:48:54 +0000393 // Flush the I-Cache: FLUSH clears out a doubleword at a given address
394 // Self-modifying code MUST clear out the I-Cache to be portable
395#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
396 for (int i = -Offset, e = 32-((int64_t)Offset); i < e; i += 8)
397 __asm__ __volatile__ ("flush %%i7 + %0" : : "r" (i));
398#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000399
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000400 // Change the return address to re-execute the restore, then the jump.
Misha Brukman103f0c32003-09-05 22:59:31 +0000401 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000402 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000403#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
404 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
405#endif
Misha Brukmana59f41f2003-11-21 23:48:54 +0000406
Misha Brukmand71295a2003-12-17 22:04:00 +0000407 RestoreRegisters(DoubleFP, CC, Globals);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000408}
409
410/// emitStubForFunction - This method is used by the JIT when it needs to emit
411/// the address of a function for a function whose code has not yet been
412/// generated. In order to do this, it generates a stub which jumps to the lazy
413/// function compiler, which will eventually get fixed to call the function
414/// directly.
415///
Misha Brukmana2196c12003-06-04 20:01:13 +0000416uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000417 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000418
Misha Brukman8f122222003-06-06 00:26:11 +0000419 DEBUG(std::cerr << "Emitting stub at addr: 0x"
420 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000421
Misha Brukman0897c602003-08-06 16:20:22 +0000422 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
423
424 // restore %g0, 0, %g0
425 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
426 .addMReg(g0, MOTy::Def);
427 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
428 delete R;
429
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000430 // save %sp, -192, %sp
431 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
432 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
433 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000434
435 int64_t CurrPC = MCE.getCurrentPCValue();
436 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
437 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000438 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
439 // Since this is a far call, the actual address of the call is shifted
440 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000441 deleteFunctionReference(CurrPC);
442 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000443 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000444 // call CallTarget ;; invoke the callback
445 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
446 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
447 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000448
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000449 // nop ;; call delay slot
450 MachineInstr *Nop = BuildMI(V9::NOP, 0);
451 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
452 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000453
454 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000455 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000456
457 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000458 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000459}
460
Misha Brukmana2196c12003-06-04 20:01:13 +0000461SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
462 MachineCodeEmitter &M): TM(tm), MCE(M)
463{
464 TheJITResolver = new JITResolver(*this, M);
465}
466
467SparcV9CodeEmitter::~SparcV9CodeEmitter() {
468 delete TheJITResolver;
469}
470
471void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000472 // Output the constant in big endian byte order...
473 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000474 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000475 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000476 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000477 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000478}
479
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000480unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000481SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000482 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000483 const TargetRegInfo &RI = TM.getRegInfo();
484 unsigned regClass, regType = RI.getRegType(fakeReg);
485 // At least map fakeReg into its class
486 fakeReg = RI.getClassRegNum(fakeReg, regClass);
487
Misha Brukman9cedd432003-07-03 18:36:47 +0000488 switch (regClass) {
Misha Brukmand71295a2003-12-17 22:04:00 +0000489 case SparcRegInfo::IntRegClassID: {
Misha Brukman9cedd432003-07-03 18:36:47 +0000490 // Sparc manual, p31
491 static const unsigned IntRegMap[] = {
492 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
493 8, 9, 10, 11, 12, 13, 15,
494 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
495 16, 17, 18, 19, 20, 21, 22, 23,
496 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
497 24, 25, 26, 27, 28, 29, 30, 31,
498 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
499 0, 1, 2, 3, 4, 5, 6, 7,
500 // "o6"
501 14
502 };
503
504 return IntRegMap[fakeReg];
505 break;
506 }
Misha Brukmand71295a2003-12-17 22:04:00 +0000507 case SparcRegInfo::FloatRegClassID: {
Misha Brukman9cedd432003-07-03 18:36:47 +0000508 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukmand71295a2003-12-17 22:04:00 +0000509 if (regType == SparcRegInfo::FPSingleRegType) {
Misha Brukman173e2502003-07-14 23:26:03 +0000510 // only numbered 0-31, hence can already fit into 5 bits (and 6)
511 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
Misha Brukmand71295a2003-12-17 22:04:00 +0000512 } else if (regType == SparcRegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000513 // FIXME: This assumes that we only have 5-bit register fields!
Misha Brukman173e2502003-07-14 23:26:03 +0000514 // From Sparc Manual, page 40.
515 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
516 fakeReg |= (fakeReg >> 5) & 1;
517 fakeReg &= 0x1f;
518 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
519 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000520 return fakeReg;
521 }
Misha Brukmand71295a2003-12-17 22:04:00 +0000522 case SparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000523 /* xcc, icc, ccr */
524 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000525
Misha Brukmandfbfc572003-07-16 20:30:40 +0000526 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
527 && "CC register out of bounds for IntCCReg map");
528 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
529 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000530 }
Misha Brukmand71295a2003-12-17 22:04:00 +0000531 case SparcRegInfo::FloatCCRegClassID: {
Misha Brukman9cedd432003-07-03 18:36:47 +0000532 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
533 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
534 return fakeReg;
535 }
536 default:
537 assert(0 && "Invalid unified register number in getRegType");
538 return fakeReg;
539 }
540}
541
542
Misha Brukman07d45162003-07-15 19:09:43 +0000543// WARNING: if the call used the delay slot to do meaningful work, that's not
544// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000545inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000546 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000547 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
548 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000549
Misha Brukman0897c602003-08-06 16:20:22 +0000550 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000551 //
Misha Brukman0870e972003-08-06 22:19:18 +0000552 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000553 //
Misha Brukman0870e972003-08-06 22:19:18 +0000554 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
555 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
556 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
557 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000558 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000559 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
560 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
561 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000562 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000563 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
564 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
565 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000566 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000567 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000568 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000569 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000570 };
Misha Brukman07d45162003-07-15 19:09:43 +0000571
Misha Brukman0897c602003-08-06 16:20:22 +0000572 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
573 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000574 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000575 uint64_t CurrPC = MCE.getCurrentPCValue();
576 TheJITResolver->addFunctionReference(CurrPC, F);
577 // Remember that this is a far call, to subtract appropriate offset later
578 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
579 }
Misha Brukman07d45162003-07-15 19:09:43 +0000580
Misha Brukman0897c602003-08-06 16:20:22 +0000581 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
582 delete BinaryCode[i];
583 }
Misha Brukman07d45162003-07-15 19:09:43 +0000584}
585
Chris Lattner1e60a912003-12-20 01:22:19 +0000586void SparcJITInfo::replaceMachineCodeForFunction (void *Old, void *New) {
Brian Gaeke0522b082003-10-20 17:59:09 +0000587 assert (TheJITResolver &&
588 "Can only call replaceMachineCodeForFunction from within JIT");
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000589 uint64_t Target = (uint64_t)(intptr_t)New;
590 uint64_t CodeBegin = (uint64_t)(intptr_t)Old;
591 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000592}
Misha Brukman07d45162003-07-15 19:09:43 +0000593
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000594int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
595 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000596 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
597 // or things that get fixed up later by the JIT.
Chris Lattnerebcd7942004-02-10 20:47:24 +0000598 if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000599 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000600 Value *V = MO.getVRegValue();
601 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000602 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000603 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000604 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000605 } else if (const Constant *C = dyn_cast<Constant>(V)) {
Misha Brukman52709452003-11-07 21:07:30 +0000606 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
607 rv = CI->getRawValue() - MCE.getCurrentPCValue();
608 } else {
609 std::cerr << "Cannot have non-integral const in instruction: "
610 << *C;
611 abort();
612 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000613 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
614 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000615 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000616 // external function calls, etc.?
617 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000618 DEBUG(std::cerr << "Function: ");
Brian Gaeke1654bdb2003-11-09 07:08:34 +0000619 // NOTE: This results in stubs being generated even for
620 // external, native functions, which is not optimal. See PR103.
621 rv = (int64_t)MCE.getGlobalValueAddress(F);
Misha Brukmana2196c12003-06-04 20:01:13 +0000622 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000623 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000624 // Function has not yet been code generated!
625 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
626 // Delayed resolution...
627 rv = TheJITResolver->getLazyResolver(F);
628 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000629 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000630 }
631 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000632 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukman0870e972003-08-06 22:19:18 +0000633 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000634 }
635 // The real target of the call is Addr = PC + (rv * 4)
636 // So undo that: give the instruction (Addr - PC) / 4
637 if (MI.getOpcode() == V9::CALL) {
638 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000639 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000640 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000641 int64_t CallInstTarget = (rv - CurrPC) >> 2;
642 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
643 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000644 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000645 // make an indirect jump-and-link
646 emitFarCall(rv);
647 // this invalidates the instruction so that the call with an incorrect
648 // address will not be emitted
649 rv = 0;
650 } else {
651 // The call fits into 30 bits, so just return the corrected address
652 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000653 }
Misha Brukman8f122222003-06-06 00:26:11 +0000654 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000655 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000656 } else {
657 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
658 abort();
659 }
Alkis Evlogimenosaf862112004-02-11 05:55:00 +0000660 } else if (MO.isRegister() || MO.getType() == MachineOperand::MO_CCRegister)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000661 {
Alkis Evlogimenosaf862112004-02-11 05:55:00 +0000662 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
663 "virtual register in machine code!");
Misha Brukman9cedd432003-07-03 18:36:47 +0000664 // This is necessary because the Sparc backend doesn't actually lay out
665 // registers in the real fashion -- it skips those that it chooses not to
666 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000667 unsigned fakeReg = MO.getAllocatedRegNum();
668 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
669 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000670 << realRegByClass << " (LLC: "
671 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000672 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000673 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000674 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000675 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000676 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000677 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000678 rv = (int64_t)
679 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000680 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000681 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000682 // Duplicate code of the above case for VirtualRegister, BasicBlock...
683 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000684 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000685 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000686 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000687 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000688 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000689 // Sparc backend doesn't generate this (yet...)
690 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
691 abort();
692 } else if (MO.isFrameIndex()) {
693 // Sparc backend doesn't generate this (yet...)
694 int FrameIndex = MO.getFrameIndex();
695 std::cerr << "ERROR: Frame index unhandled.\n";
696 abort();
697 } else if (MO.isConstantPoolIndex()) {
Misha Brukmane5ad8152003-11-07 18:06:26 +0000698 unsigned Index = MO.getConstantPoolIndex();
699 rv = MCE.getConstantPoolEntryAddress(Index);
Misha Brukman3de36f52003-05-27 20:07:58 +0000700 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000701 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000702 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000703 }
704
705 // Finally, deal with the various bitfield-extracting functions that
706 // are used in SPARC assembly. (Some of these make no sense in combination
707 // with some of the above; we'll trust that the instruction selector
708 // will not produce nonsense, and not check for valid combinations here.)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000709 if (MO.isLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000710 return rv & 0x03ff;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000711 } else if (MO.isHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000712 return (rv >> 10) & 0x03fffff;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000713 } else if (MO.isLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000714 return (rv >> 32) & 0x03ff;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000715 } else if (MO.isHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000716 return rv >> 42;
717 } else { // (unadorned) val
718 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000719 }
720}
721
722unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
723 Val >>= bit;
724 return (Val & 1);
725}
726
Misha Brukman3de36f52003-05-27 20:07:58 +0000727bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000728 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000729 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000730 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000731 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000732
Misha Brukmane5ad8152003-11-07 18:06:26 +0000733 MCE.emitConstantPool(MF.getConstantPool());
Misha Brukman3de36f52003-05-27 20:07:58 +0000734 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
735 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000736 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000737
Misha Brukman9cedd432003-07-03 18:36:47 +0000738 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000739
740 // Resolve branches to BasicBlocks for the entire function
741 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
742 long Location = BBLocations[BBRefs[i].first];
743 unsigned *Ref = BBRefs[i].second.first;
744 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000745 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
746 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000747 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
748 MachineOperand &op = MI->getOperand(ii);
749 if (op.isPCRelativeDisp()) {
750 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000751 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000752 // Location is the target of the branch
753 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000754 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000755 // Save the flags.
756 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000757 if (op.isLoBits32()) { loBits32=true; }
758 if (op.isHiBits32()) { hiBits32=true; }
759 if (op.isLoBits64()) { loBits64=true; }
760 if (op.isHiBits64()) { hiBits64=true; }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000761 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
762 branchTarget);
763 if (loBits32) { MI->setOperandLo32(ii); }
764 else if (hiBits32) { MI->setOperandHi32(ii); }
765 else if (loBits64) { MI->setOperandLo64(ii); }
766 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000767 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000768 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
769 *Ref = fixedInstr;
770 break;
771 }
772 }
773 }
774 BBRefs.clear();
775 BBLocations.clear();
776
Misha Brukman3de36f52003-05-27 20:07:58 +0000777 return false;
778}
779
780void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000781 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000782 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000783 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
784 unsigned binCode = getBinaryCodeForInstr(**I);
785 if (binCode == (1 << 30)) {
786 // this is an invalid call: the addr is out of bounds. that means a code
787 // sequence has already been emitted, and this is a no-op
788 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
789 } else {
790 emitWord(binCode);
791 }
792 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000793}
794
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000795void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
796 bool isPCRelative)
797{
798 if (isPCRelative) { // must be a call, this is a major hack!
799 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000800 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
801 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000802 // The real target of the call is Addr = PC + (target * 4)
803 // CurByte is the PC, Addr we just received
804 return (void*) (((long)Addr - (long)CurByte) >> 2);
805 } else {
806 if (Function *F = dyn_cast<Function>(V)) {
807 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000808 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000809 cast<Function>(V));
810 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000811 return
812 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000813 } else {
814 std::cerr << "Unhandled global: " << *V << "\n";
815 abort();
816 }
817 }
818 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000819 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000820 }
821}
822
Misha Brukmanc6b15842003-11-13 00:23:05 +0000823#include "SparcV9CodeEmitter.inc"
824
Brian Gaeked0fde302003-11-11 22:41:34 +0000825} // End llvm namespace
826