blob: fccc3e7cfaaea052bea9d320f00246213658c67a [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 Brukman3de36f52003-05-27 20:07:58 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000031#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000032#include "llvm/Target/TargetData.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/Debug.h"
34#include "llvm/ADT/hash_set"
35#include "llvm/ADT/Statistic.h"
Brian Gaekee3d68072004-02-25 18:44:15 +000036#include "SparcV9Internals.h"
37#include "SparcV9TargetMachine.h"
38#include "SparcV9RegInfo.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000039#include "SparcV9CodeEmitter.h"
Chris Lattner85015a02004-08-16 21:55:02 +000040#include "MachineFunctionInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000042
Brian Gaeked0fde302003-11-11 22:41:34 +000043namespace llvm {
44
Misha Brukman103f0c32003-09-05 22:59:31 +000045namespace {
46 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
47 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
48 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
49}
50
Brian Gaekee3d68072004-02-25 18:44:15 +000051bool SparcV9TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman7a750e12004-08-04 21:48:00 +000052 MachineCodeEmitter &MCE) {
Brian Gaekeeb6c29b2004-07-27 19:37:37 +000053 PM.add(new SparcV9CodeEmitter(*this, MCE));
Misha Brukman7a750e12004-08-04 21:48:00 +000054 PM.add(createSparcV9MachineCodeDestructionPass());
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 Brukmand71295a2003-12-17 22:04:00 +0000105 static void SaveRegisters(uint64_t DoubleFP[], uint64_t CC[],
106 uint64_t Globals[]);
107 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t CC[],
108 uint64_t Globals[]);
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
Brian Gaekee3d68072004-02-25 18:44:15 +0000180 o6 = SparcV9IntRegClass::o6, g0 = SparcV9IntRegClass::g0,
181 g1 = SparcV9IntRegClass::g1, g5 = SparcV9IntRegClass::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 Brukmand71295a2003-12-17 22:04:00 +0000212void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t CC[],
213 uint64_t Globals[]) {
Misha Brukman1155d312004-09-29 23:01:17 +0000214#if defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000215
216 __asm__ __volatile__ (// Save condition-code registers
217 "stx %%fsr, %0;\n\t"
218 "rd %%fprs, %1;\n\t"
219 "rd %%ccr, %2;\n\t"
Misha Brukmand71295a2003-12-17 22:04:00 +0000220 : "=m"(CC[0]), "=r"(CC[1]), "=r"(CC[2]));
221
222 __asm__ __volatile__ (// Save globals g1 and g5
223 "stx %%g1, %0;\n\t"
224 "stx %%g5, %0;\n\t"
225 : "=m"(Globals[0]), "=m"(Globals[1]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000226
227 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000228 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
229 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
230 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
231 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
232 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
233 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
234 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
235 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
236 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
237 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
238 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
239 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
240 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
241 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
242 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
243 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
244 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000245
Misha Brukmancfd67c92003-08-29 04:22:54 +0000246 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000247 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000248 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000249 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
250 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
251 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
252 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
253 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
254 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000255 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
256 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
257 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
258 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
259 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
260 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
261 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
262 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Chris Lattner83d72bc2004-10-09 21:13:51 +0000263#else
264 std::cerr << "ERROR: RUNNING CODE THAT ONLY WORKS ON A SPARCV9 HOST!\n";
265 abort();
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 Brukman1155d312004-09-29 23:01:17 +0000273#if 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]));
Chris Lattner83d72bc2004-10-09 21:13:51 +0000322#else
323 std::cerr << "ERROR: RUNNING CODE THAT ONLY WORKS ON A SPARCV9 HOST!\n";
324 abort();
Misha Brukmanfad49292003-08-15 00:26:50 +0000325#endif
326}
327
Misha Brukmancfd67c92003-08-29 04:22:54 +0000328void JITResolver::CompilationCallback() {
Misha Brukmand71295a2003-12-17 22:04:00 +0000329 // Local space to save the registers
Misha Brukmancfd67c92003-08-29 04:22:54 +0000330 uint64_t DoubleFP[32];
Misha Brukmand71295a2003-12-17 22:04:00 +0000331 uint64_t CC[3];
332 uint64_t Globals[2];
Misha Brukmancfd67c92003-08-29 04:22:54 +0000333
Misha Brukmand71295a2003-12-17 22:04:00 +0000334 SaveRegisters(DoubleFP, CC, Globals);
Misha Brukman103f0c32003-09-05 22:59:31 +0000335 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000336
337 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000338 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000339 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000340 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000341 register int64_t returnAddr = 0;
Misha Brukman1155d312004-09-29 23:01:17 +0000342#if defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000343 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
344 DEBUG(std::cerr << "Read i7 (return addr) = "
345 << std::hex << returnAddr << ", value: "
346 << std::hex << *(unsigned*)returnAddr << "\n");
Chris Lattner83d72bc2004-10-09 21:13:51 +0000347#else
348 std::cerr << "ERROR: RUNNING CODE THAT ONLY WORKS ON A SPARCV9 HOST!\n";
349 abort();
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000350#endif
351
Misha Brukman103f0c32003-09-05 22:59:31 +0000352 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
353 // trampoline function stub!!
354 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
355 int64_t OrigTarget = (Target-CameFrom1) >> 2;
356 if ((OrigCallInst & (1 << 30)) &&
357 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
358 {
359 // The original call instruction was CALL <immed>, which means we can
360 // overwrite it directly, since the offset will fit into 30 bits
361 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
362 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
363 delete C;
364 ++OverwrittenCalls;
365 } else {
366 ++UnmodifiedCalls;
367 }
368
Misha Brukman0897c602003-08-06 16:20:22 +0000369 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000370 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000371
Brian Gaekee3d68072004-02-25 18:44:15 +0000372 static const unsigned o6 = SparcV9IntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000373
Misha Brukman0897c602003-08-06 16:20:22 +0000374 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000375 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000376 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000377 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000378 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000379
380 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
381 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000382 // disables frame pointer elimination currently, even if it's dead. This is
383 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000384 alloca(42+Offset);
385 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000386
387 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000388 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000389 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
390 delete SV;
391 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000392 if (CodeInMem != SaveInst) {
393 std::cerr << "About to overwrite smthg not a save instr!";
394 abort();
395 }
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000396 // Overwrite it
397 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000398
Misha Brukmana59f41f2003-11-21 23:48:54 +0000399 // Flush the I-Cache: FLUSH clears out a doubleword at a given address
400 // Self-modifying code MUST clear out the I-Cache to be portable
Misha Brukman1155d312004-09-29 23:01:17 +0000401#if defined(__sparcv9)
Misha Brukmana59f41f2003-11-21 23:48:54 +0000402 for (int i = -Offset, e = 32-((int64_t)Offset); i < e; i += 8)
403 __asm__ __volatile__ ("flush %%i7 + %0" : : "r" (i));
404#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000405
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000406 // Change the return address to re-execute the restore, then the jump.
Misha Brukman103f0c32003-09-05 22:59:31 +0000407 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000408 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukman1155d312004-09-29 23:01:17 +0000409#if defined(__sparcv9)
Misha Brukmancfd67c92003-08-29 04:22:54 +0000410 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
411#endif
Misha Brukmana59f41f2003-11-21 23:48:54 +0000412
Misha Brukmand71295a2003-12-17 22:04:00 +0000413 RestoreRegisters(DoubleFP, CC, Globals);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000414}
415
416/// emitStubForFunction - This method is used by the JIT when it needs to emit
417/// the address of a function for a function whose code has not yet been
418/// generated. In order to do this, it generates a stub which jumps to the lazy
419/// function compiler, which will eventually get fixed to call the function
420/// directly.
421///
Misha Brukmana2196c12003-06-04 20:01:13 +0000422uint64_t JITResolver::emitStubForFunction(Function *F) {
Chris Lattner3bf285a2004-11-20 23:53:26 +0000423 MCE.startFunctionStub(44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000424
Misha Brukman8f122222003-06-06 00:26:11 +0000425 DEBUG(std::cerr << "Emitting stub at addr: 0x"
426 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000427
Brian Gaekee3d68072004-02-25 18:44:15 +0000428 unsigned o6 = SparcV9IntRegClass::o6, g0 = SparcV9IntRegClass::g0;
Misha Brukman0897c602003-08-06 16:20:22 +0000429
430 // restore %g0, 0, %g0
431 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
Alkis Evlogimenos890f9232004-02-22 19:23:26 +0000432 .addMReg(g0, MachineOperand::Def);
Misha Brukman0897c602003-08-06 16:20:22 +0000433 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
434 delete R;
435
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000436 // save %sp, -192, %sp
437 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
438 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
439 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000440
441 int64_t CurrPC = MCE.getCurrentPCValue();
442 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
443 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000444 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
445 // Since this is a far call, the actual address of the call is shifted
446 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000447 deleteFunctionReference(CurrPC);
448 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000449 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000450 // call CallTarget ;; invoke the callback
451 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
452 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
453 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000454
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000455 // nop ;; call delay slot
456 MachineInstr *Nop = BuildMI(V9::NOP, 0);
457 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
458 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000459
460 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000461 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000462
463 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Chris Lattner3bf285a2004-11-20 23:53:26 +0000464 return (intptr_t)MCE.finishFunctionStub(F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000465}
466
Misha Brukmana2196c12003-06-04 20:01:13 +0000467SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
468 MachineCodeEmitter &M): TM(tm), MCE(M)
469{
470 TheJITResolver = new JITResolver(*this, M);
471}
472
473SparcV9CodeEmitter::~SparcV9CodeEmitter() {
474 delete TheJITResolver;
475}
476
477void SparcV9CodeEmitter::emitWord(unsigned Val) {
Brian Gaeke62c6f872004-04-23 17:11:15 +0000478 MCE.emitWord(Val);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000479}
480
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000481unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000482SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000483 MachineInstr &MI) {
Brian Gaeke498231b2004-06-03 02:45:09 +0000484 const SparcV9RegInfo &RI = *TM.getRegInfo();
Misha Brukman173e2502003-07-14 23:26:03 +0000485 unsigned regClass, regType = RI.getRegType(fakeReg);
486 // At least map fakeReg into its class
487 fakeReg = RI.getClassRegNum(fakeReg, regClass);
488
Misha Brukman9cedd432003-07-03 18:36:47 +0000489 switch (regClass) {
Brian Gaekee3d68072004-02-25 18:44:15 +0000490 case SparcV9RegInfo::IntRegClassID: {
491 // SparcV9 manual, p31
Misha Brukman9cedd432003-07-03 18:36:47 +0000492 static const unsigned IntRegMap[] = {
493 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
494 8, 9, 10, 11, 12, 13, 15,
495 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
496 16, 17, 18, 19, 20, 21, 22, 23,
497 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
498 24, 25, 26, 27, 28, 29, 30, 31,
499 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
500 0, 1, 2, 3, 4, 5, 6, 7,
501 // "o6"
502 14
503 };
504
505 return IntRegMap[fakeReg];
506 break;
507 }
Brian Gaekee3d68072004-02-25 18:44:15 +0000508 case SparcV9RegInfo::FloatRegClassID: {
Misha Brukman9cedd432003-07-03 18:36:47 +0000509 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Brian Gaekee3d68072004-02-25 18:44:15 +0000510 if (regType == SparcV9RegInfo::FPSingleRegType) {
Misha Brukman173e2502003-07-14 23:26:03 +0000511 // only numbered 0-31, hence can already fit into 5 bits (and 6)
512 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
Brian Gaekee3d68072004-02-25 18:44:15 +0000513 } else if (regType == SparcV9RegInfo::FPDoubleRegType) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000514 // FIXME: This assumes that we only have 5-bit register fields!
Brian Gaekee3d68072004-02-25 18:44:15 +0000515 // From SparcV9 Manual, page 40.
Misha Brukman173e2502003-07-14 23:26:03 +0000516 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
517 fakeReg |= (fakeReg >> 5) & 1;
518 fakeReg &= 0x1f;
519 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
520 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000521 return fakeReg;
522 }
Brian Gaekee3d68072004-02-25 18:44:15 +0000523 case SparcV9RegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000524 /* xcc, icc, ccr */
525 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000526
Misha Brukmandfbfc572003-07-16 20:30:40 +0000527 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
528 && "CC register out of bounds for IntCCReg map");
529 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
530 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000531 }
Brian Gaekee3d68072004-02-25 18:44:15 +0000532 case SparcV9RegInfo::FloatCCRegClassID: {
Misha Brukman9cedd432003-07-03 18:36:47 +0000533 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
534 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
535 return fakeReg;
536 }
Brian Gaeke7fa84b72004-06-09 21:54:59 +0000537 case SparcV9RegInfo::SpecialRegClassID: {
538 // Currently only "special" reg is %fsr, which is encoded as 1 in
539 // instructions and 0 in SparcV9SpecialRegClass.
540 static const unsigned SpecialReg[] = { 1 };
541 assert(fakeReg < sizeof(SpecialReg)/sizeof(SpecialReg[0])
542 && "Special register out of bounds for SpecialReg map");
543 DEBUG(std::cerr << "Special reg: " << SpecialReg[fakeReg] << "\n");
544 return SpecialReg[fakeReg];
545 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000546 default:
Brian Gaekee8a6bee2004-06-09 20:44:42 +0000547 assert(0 && "Invalid unified register number in getRealRegNum");
Misha Brukman9cedd432003-07-03 18:36:47 +0000548 return fakeReg;
549 }
550}
551
552
Misha Brukman07d45162003-07-15 19:09:43 +0000553// WARNING: if the call used the delay slot to do meaningful work, that's not
554// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000555inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Brian Gaekee3d68072004-02-25 18:44:15 +0000556 static const unsigned o6 = SparcV9IntRegClass::o6,
557 o7 = SparcV9IntRegClass::o7, g0 = SparcV9IntRegClass::g0,
558 g1 = SparcV9IntRegClass::g1, g5 = SparcV9IntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000559
Misha Brukman0897c602003-08-06 16:20:22 +0000560 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000561 //
Misha Brukman0870e972003-08-06 22:19:18 +0000562 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000563 //
Misha Brukman0870e972003-08-06 22:19:18 +0000564 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
565 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
566 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
567 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000568 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000569 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
570 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
571 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000572 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000573 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
574 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
575 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000576 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000577 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000578 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000579 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000580 };
Misha Brukman07d45162003-07-15 19:09:43 +0000581
Misha Brukman0897c602003-08-06 16:20:22 +0000582 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
583 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000584 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000585 uint64_t CurrPC = MCE.getCurrentPCValue();
586 TheJITResolver->addFunctionReference(CurrPC, F);
587 // Remember that this is a far call, to subtract appropriate offset later
588 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
589 }
Misha Brukman07d45162003-07-15 19:09:43 +0000590
Misha Brukman0897c602003-08-06 16:20:22 +0000591 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
592 delete BinaryCode[i];
593 }
Misha Brukman07d45162003-07-15 19:09:43 +0000594}
595
Brian Gaekee3d68072004-02-25 18:44:15 +0000596void SparcV9JITInfo::replaceMachineCodeForFunction (void *Old, void *New) {
Brian Gaeke0522b082003-10-20 17:59:09 +0000597 assert (TheJITResolver &&
Misha Brukman7a750e12004-08-04 21:48:00 +0000598 "Can only call replaceMachineCodeForFunction from within JIT");
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000599 uint64_t Target = (uint64_t)(intptr_t)New;
600 uint64_t CodeBegin = (uint64_t)(intptr_t)Old;
601 TheJITResolver->insertJumpAtAddr(Target, CodeBegin);
Brian Gaeke4ca7e532003-10-17 18:27:37 +0000602}
Misha Brukman07d45162003-07-15 19:09:43 +0000603
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000604int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
605 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000606 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
607 // or things that get fixed up later by the JIT.
Chris Lattnerebcd7942004-02-10 20:47:24 +0000608 if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000609 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000610 Value *V = MO.getVRegValue();
611 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000612 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000613 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000614 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Brian Gaekea9004522004-05-19 21:30:01 +0000615 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Brian Gaekeceabd972004-05-20 07:43:40 +0000616 // The real target of the branch is CI = PC + (rv * 4)
617 // So undo that: give the instruction (CI - PC) / 4
618 rv = (CI->getRawValue() - MCE.getCurrentPCValue()) / 4;
Misha Brukmana2196c12003-06-04 20:01:13 +0000619 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
620 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000621 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000622 // external function calls, etc.?
623 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000624 DEBUG(std::cerr << "Function: ");
Brian Gaeke1654bdb2003-11-09 07:08:34 +0000625 // NOTE: This results in stubs being generated even for
626 // external, native functions, which is not optimal. See PR103.
627 rv = (int64_t)MCE.getGlobalValueAddress(F);
Misha Brukmana2196c12003-06-04 20:01:13 +0000628 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000629 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000630 // Function has not yet been code generated!
631 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
632 // Delayed resolution...
633 rv = TheJITResolver->getLazyResolver(F);
634 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000635 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000636 }
637 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000638 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukman0870e972003-08-06 22:19:18 +0000639 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000640 }
641 // The real target of the call is Addr = PC + (rv * 4)
642 // So undo that: give the instruction (Addr - PC) / 4
643 if (MI.getOpcode() == V9::CALL) {
644 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000645 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000646 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000647 int64_t CallInstTarget = (rv - CurrPC) >> 2;
648 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
649 DEBUG(std::cerr << "Making far call!\n");
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000650 // address is out of bounds for the 30-bit call,
Misha Brukman07d45162003-07-15 19:09:43 +0000651 // make an indirect jump-and-link
652 emitFarCall(rv);
653 // this invalidates the instruction so that the call with an incorrect
654 // address will not be emitted
655 rv = 0;
656 } else {
657 // The call fits into 30 bits, so just return the corrected address
658 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000659 }
Misha Brukman8f122222003-06-06 00:26:11 +0000660 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000661 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000662 } else {
663 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
664 abort();
665 }
Alkis Evlogimenosaf862112004-02-11 05:55:00 +0000666 } else if (MO.isRegister() || MO.getType() == MachineOperand::MO_CCRegister)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000667 {
Brian Gaekee3d68072004-02-25 18:44:15 +0000668 // This is necessary because the SparcV9 backend doesn't actually lay out
Misha Brukman9cedd432003-07-03 18:36:47 +0000669 // registers in the real fashion -- it skips those that it chooses not to
670 // allocate, i.e. those that are the FP, SP, etc.
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000671 unsigned fakeReg = MO.getReg();
Misha Brukman173e2502003-07-14 23:26:03 +0000672 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
673 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000674 << realRegByClass << " (LLC: "
Chris Lattnerd029cd22004-06-02 05:55:25 +0000675 << TM.getRegInfo()->getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000676 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000677 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000678 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000679 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000680 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000681 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000682 rv = (int64_t)
683 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000684 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000685 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000686 // Duplicate code of the above case for VirtualRegister, BasicBlock...
Brian Gaekee3d68072004-02-25 18:44:15 +0000687 // It should really hit this case, but SparcV9 backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000688 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000689 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000690 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000691 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000692 } else if (MO.isExternalSymbol()) {
Brian Gaekee3d68072004-02-25 18:44:15 +0000693 // SparcV9 backend doesn't generate this (yet...)
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000694 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
695 abort();
696 } else if (MO.isFrameIndex()) {
Brian Gaekee3d68072004-02-25 18:44:15 +0000697 // SparcV9 backend doesn't generate this (yet...)
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000698 int FrameIndex = MO.getFrameIndex();
699 std::cerr << "ERROR: Frame index unhandled.\n";
700 abort();
701 } else if (MO.isConstantPoolIndex()) {
Misha Brukmane5ad8152003-11-07 18:06:26 +0000702 unsigned Index = MO.getConstantPoolIndex();
703 rv = MCE.getConstantPoolEntryAddress(Index);
Misha Brukman3de36f52003-05-27 20:07:58 +0000704 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000705 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000706 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000707 }
708
709 // Finally, deal with the various bitfield-extracting functions that
710 // are used in SPARC assembly. (Some of these make no sense in combination
711 // with some of the above; we'll trust that the instruction selector
712 // will not produce nonsense, and not check for valid combinations here.)
Brian Gaekee3d68072004-02-25 18:44:15 +0000713 if (MO.isLoBits32()) { // %lo(val) == %lo() in SparcV9 ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000714 return rv & 0x03ff;
Brian Gaekee3d68072004-02-25 18:44:15 +0000715 } else if (MO.isHiBits32()) { // %lm(val) == %hi() in SparcV9 ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000716 return (rv >> 10) & 0x03fffff;
Brian Gaekee3d68072004-02-25 18:44:15 +0000717 } else if (MO.isLoBits64()) { // %hm(val) == %ulo() in SparcV9 ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000718 return (rv >> 32) & 0x03ff;
Brian Gaekee3d68072004-02-25 18:44:15 +0000719 } else if (MO.isHiBits64()) { // %hh(val) == %uhi() in SparcV9 ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000720 return rv >> 42;
721 } else { // (unadorned) val
722 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000723 }
724}
725
726unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
727 Val >>= bit;
728 return (Val & 1);
729}
730
Misha Brukman3de36f52003-05-27 20:07:58 +0000731bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000732 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000733 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000734 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000735 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000736
Misha Brukmane5ad8152003-11-07 18:06:26 +0000737 MCE.emitConstantPool(MF.getConstantPool());
Misha Brukman3de36f52003-05-27 20:07:58 +0000738 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
739 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000740 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000741
Misha Brukman9cedd432003-07-03 18:36:47 +0000742 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000743
744 // Resolve branches to BasicBlocks for the entire function
745 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
746 long Location = BBLocations[BBRefs[i].first];
747 unsigned *Ref = BBRefs[i].second.first;
748 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000749 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
750 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000751 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
752 MachineOperand &op = MI->getOperand(ii);
753 if (op.isPCRelativeDisp()) {
754 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000755 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000756 // Location is the target of the branch
757 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000758 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000759 // Save the flags.
760 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000761 if (op.isLoBits32()) { loBits32=true; }
762 if (op.isHiBits32()) { hiBits32=true; }
763 if (op.isLoBits64()) { loBits64=true; }
764 if (op.isHiBits64()) { hiBits64=true; }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000765 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
766 branchTarget);
Chris Lattnerf4fc36e2004-07-19 07:52:35 +0000767 if (loBits32) { MI->getOperand(ii).markLo32(); }
768 else if (hiBits32) { MI->getOperand(ii).markHi32(); }
769 else if (loBits64) { MI->getOperand(ii).markLo64(); }
770 else if (hiBits64) { MI->getOperand(ii).markHi64(); }
Misha Brukman8f122222003-06-06 00:26:11 +0000771 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000772 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
Brian Gaeke62c6f872004-04-23 17:11:15 +0000773 MCE.emitWordAt (fixedInstr, Ref);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000774 break;
775 }
776 }
777 }
778 BBRefs.clear();
779 BBLocations.clear();
780
Misha Brukman3de36f52003-05-27 20:07:58 +0000781 return false;
782}
783
784void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000785 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000786 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000787 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000788 unsigned binCode = getBinaryCodeForInstr(*I);
Misha Brukman07d45162003-07-15 19:09:43 +0000789 if (binCode == (1 << 30)) {
790 // this is an invalid call: the addr is out of bounds. that means a code
791 // sequence has already been emitted, and this is a no-op
Brian Gaekea9004522004-05-19 21:30:01 +0000792 DEBUG(std::cerr << "Call suppressed: already emitted far call.\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000793 } else {
794 emitWord(binCode);
795 }
796 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000797}
798
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000799void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
800 bool isPCRelative)
801{
802 if (isPCRelative) { // must be a call, this is a major hack!
803 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000804 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
805 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000806 // The real target of the call is Addr = PC + (target * 4)
807 // CurByte is the PC, Addr we just received
808 return (void*) (((long)Addr - (long)CurByte) >> 2);
809 } else {
810 if (Function *F = dyn_cast<Function>(V)) {
811 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000812 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000813 cast<Function>(V));
814 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000815 return
816 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000817 } else {
818 std::cerr << "Unhandled global: " << *V << "\n";
819 abort();
820 }
821 }
822 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000823 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000824 }
825}
826
Misha Brukmanc6b15842003-11-13 00:23:05 +0000827#include "SparcV9CodeEmitter.inc"
828
Brian Gaeked0fde302003-11-11 22:41:34 +0000829} // End llvm namespace
830