Chris Lattner | 556d89d | 2003-08-01 22:19:03 +0000 | [diff] [blame] | 1 | //===-- SparcV9CodeEmitter.cpp --------------------------------------------===// |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 556d89d | 2003-08-01 22:19:03 +0000 | [diff] [blame] | 3 | // FIXME: document |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 7 | #include "llvm/Constants.h" |
| 8 | #include "llvm/Function.h" |
| 9 | #include "llvm/GlobalVariable.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 10 | #include "llvm/PassManager.h" |
| 11 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineConstantPool.h" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunctionInfo.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 15 | #include "llvm/CodeGen/MachineInstr.h" |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetMachine.h" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 556d89d | 2003-08-01 22:19:03 +0000 | [diff] [blame] | 18 | #include "Support/Debug.h" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 19 | #include "Support/hash_set" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 20 | #include "SparcInternals.h" |
Misha Brukman | 0cc640e | 2003-05-27 21:45:05 +0000 | [diff] [blame] | 21 | #include "SparcV9CodeEmitter.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 22 | |
| 23 | bool UltraSparc::addPassesToEmitMachineCode(PassManager &PM, |
| 24 | MachineCodeEmitter &MCE) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 25 | MachineCodeEmitter *M = &MCE; |
Misha Brukman | de07be3 | 2003-06-06 04:41:22 +0000 | [diff] [blame] | 26 | DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE)); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 27 | PM.add(new SparcV9CodeEmitter(*this, *M)); |
Misha Brukman | dcbe712 | 2003-05-31 06:26:06 +0000 | [diff] [blame] | 28 | PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 29 | return false; |
| 30 | } |
| 31 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class JITResolver { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 34 | SparcV9CodeEmitter &SparcV9; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 35 | MachineCodeEmitter &MCE; |
| 36 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 37 | /// LazyCodeGenMap - Keep track of call sites for functions that are to be |
| 38 | /// lazily resolved. |
| 39 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 40 | std::map<uint64_t, Function*> LazyCodeGenMap; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 41 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 42 | /// LazyResolverMap - Keep track of the lazy resolver created for a |
| 43 | /// particular function so that we can reuse them if necessary. |
| 44 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 45 | std::map<Function*, uint64_t> LazyResolverMap; |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
| 48 | enum CallType { ShortCall, FarCall }; |
| 49 | |
| 50 | private: |
| 51 | /// We need to keep track of whether we used a simple call or a far call |
| 52 | /// (many instructions) in sequence. This means we need to keep track of |
| 53 | /// what type of stub we generate. |
| 54 | static std::map<uint64_t, CallType> LazyCallFlavor; |
| 55 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 56 | public: |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 57 | JITResolver(SparcV9CodeEmitter &V9, |
| 58 | MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {} |
| 59 | uint64_t getLazyResolver(Function *F); |
| 60 | uint64_t addFunctionReference(uint64_t Address, Function *F); |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 61 | void deleteFunctionReference(uint64_t Address); |
| 62 | void addCallFlavor(uint64_t Address, CallType Flavor) { |
| 63 | LazyCallFlavor[Address] = Flavor; |
| 64 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 65 | |
| 66 | // Utility functions for accessing data from static callback |
| 67 | uint64_t getCurrentPCValue() { |
| 68 | return MCE.getCurrentPCValue(); |
| 69 | } |
| 70 | unsigned getBinaryCodeForInstr(MachineInstr &MI) { |
| 71 | return SparcV9.getBinaryCodeForInstr(MI); |
| 72 | } |
| 73 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 74 | inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr); |
| 75 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 76 | private: |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 77 | uint64_t emitStubForFunction(Function *F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 78 | static void CompilationCallback(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 79 | uint64_t resolveFunctionReference(uint64_t RetAddr); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 80 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | JITResolver *TheJITResolver; |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 84 | std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /// addFunctionReference - This method is called when we need to emit the |
| 88 | /// address of a function that has not yet been emitted, so we don't know the |
| 89 | /// address. Instead, we emit a call to the CompilationCallback method, and |
| 90 | /// keep track of where we are. |
| 91 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 92 | uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) { |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 93 | LazyCodeGenMap[Address] = F; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 94 | return (intptr_t)&JITResolver::CompilationCallback; |
| 95 | } |
| 96 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 97 | /// deleteFunctionReference - If we are emitting a far call, we already added a |
| 98 | /// reference to the function, but it is now incorrect, since the address to the |
| 99 | /// JIT resolver is too far away to be a simple call instruction. This is used |
| 100 | /// to remove the address from the map. |
| 101 | /// |
| 102 | void JITResolver::deleteFunctionReference(uint64_t Address) { |
| 103 | std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address); |
| 104 | assert(I != LazyCodeGenMap.end() && "Not in map!"); |
| 105 | LazyCodeGenMap.erase(I); |
| 106 | } |
| 107 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 108 | uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) { |
| 109 | std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 110 | assert(I != LazyCodeGenMap.end() && "Not in map!"); |
| 111 | Function *F = I->second; |
| 112 | LazyCodeGenMap.erase(I); |
| 113 | return MCE.forceCompilationOf(F); |
| 114 | } |
| 115 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 116 | uint64_t JITResolver::getLazyResolver(Function *F) { |
| 117 | std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 118 | if (I != LazyResolverMap.end() && I->first == F) return I->second; |
| 119 | |
| 120 | //std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n"; |
| 121 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 122 | uint64_t Stub = emitStubForFunction(F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 123 | LazyResolverMap.insert(I, std::make_pair(F, Stub)); |
| 124 | return Stub; |
| 125 | } |
| 126 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 127 | uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) { |
| 128 | |
| 129 | static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2, |
| 130 | i7 = SparcIntRegClass::i7, |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 131 | o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0, |
| 132 | g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 133 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 134 | MachineInstr* BinaryCode[] = { |
| 135 | // |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 136 | // Get address to branch into %g1, using %g5 as a temporary |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 137 | // |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 138 | // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5 |
| 139 | BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5), |
| 140 | // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5 |
| 141 | BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5), |
| 142 | // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word |
| 143 | BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5), |
| 144 | // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg |
| 145 | BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1), |
| 146 | // or %g5, %g1, %g1 ;; get upper word (in %i1) into %g1 |
| 147 | BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1), |
| 148 | // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1 |
| 149 | BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1), |
| 150 | // jmpl %g1, %g0, %g0 ;; indirect branch on %g1 |
| 151 | BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0), |
| 152 | // nop ;; delay slot |
| 153 | BuildMI(V9::NOP, 0) |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 154 | }; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 155 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 156 | for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) { |
| 157 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]); |
| 158 | delete BinaryCode[i]; |
| 159 | Addr += 4; |
| 160 | } |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 161 | |
| 162 | return Addr; |
| 163 | } |
| 164 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 165 | void JITResolver::CompilationCallback() { |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 166 | uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0); |
| 167 | int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 168 | DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n"); |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 169 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 170 | register int64_t returnAddr; |
| 171 | __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : ); |
| 172 | DEBUG(std::cerr << "Read i7 (return addr) = " |
| 173 | << std::hex << returnAddr << ", value: " |
| 174 | << std::hex << *(unsigned*)returnAddr << "\n"); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 177 | // Rewrite the call target so that we don't fault every time we execute it. |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 178 | // |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 179 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 180 | static const unsigned o6 = SparcIntRegClass::o6; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 181 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 182 | // Subtract enough to overwrite up to the 'save' instruction |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 183 | // This depends on whether we made a short call (1 instruction) or the |
| 184 | // farCall (long form: 10 instructions, short form: 7 instructions) |
| 185 | uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28; |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 186 | uint64_t CodeBegin = CameFrom - Offset; |
| 187 | |
| 188 | // Make sure that what we're about to overwrite is indeed "save" |
| 189 | MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6); |
| 190 | unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV); |
| 191 | delete SV; |
| 192 | unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin; |
| 193 | assert(CodeInMem == SaveInst && "About to overwrite smthg not a save instr!"); |
| 194 | DEBUG(std::cerr << "Emitting a far jump to 0x" << std::hex << Target << "\n"); |
| 195 | TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 196 | |
| 197 | // FIXME: if the target function is close enough to fit into the 19bit disp of |
| 198 | // BA, we should use this version, as its much cheaper to generate. |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 199 | #if 0 |
| 200 | uint64_t InstAddr = CodeBegin; |
| 201 | // ba <target> |
| 202 | MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(Target); |
| 203 | *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*MI); |
| 204 | InstAddr += 4; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 205 | delete MI; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 206 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 207 | // nop |
| 208 | MI = BuildMI(V9::NOP, 0); |
| 209 | *((unsigned*)(intptr_t))=TheJITResolver->getBinaryCodeForInstr(*Nop); |
| 210 | delete MI; |
| 211 | #endif |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 212 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 213 | // Change the return address to reexecute the restore, then the jump |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 214 | // The return address is really %o7, but will disappear after this function |
| 215 | // returns, and the register windows are rotated away. |
| 216 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 217 | __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12)); |
| 218 | DEBUG(std::cerr << "Callback setting return addr to " |
| 219 | << std::hex << (CameFrom-Offset-12) << "\n"); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 220 | #endif |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /// emitStubForFunction - This method is used by the JIT when it needs to emit |
| 224 | /// the address of a function for a function whose code has not yet been |
| 225 | /// generated. In order to do this, it generates a stub which jumps to the lazy |
| 226 | /// function compiler, which will eventually get fixed to call the function |
| 227 | /// directly. |
| 228 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 229 | uint64_t JITResolver::emitStubForFunction(Function *F) { |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 230 | MCE.startFunctionStub(*F, 20); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 231 | |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 232 | DEBUG(std::cerr << "Emitting stub at addr: 0x" |
| 233 | << std::hex << MCE.getCurrentPCValue() << "\n"); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 234 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 235 | unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0; |
| 236 | |
| 237 | // restore %g0, 0, %g0 |
| 238 | MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0) |
| 239 | .addMReg(g0, MOTy::Def); |
| 240 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R)); |
| 241 | delete R; |
| 242 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 243 | // save %sp, -192, %sp |
| 244 | MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6); |
| 245 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV)); |
| 246 | delete SV; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 247 | |
| 248 | int64_t CurrPC = MCE.getCurrentPCValue(); |
| 249 | int64_t Addr = (int64_t)addFunctionReference(CurrPC, F); |
| 250 | int64_t CallTarget = (Addr-CurrPC) >> 2; |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 251 | //if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) { |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 252 | // Since this is a far call, the actual address of the call is shifted |
| 253 | // by the number of instructions it takes to calculate the exact address |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 254 | deleteFunctionReference(CurrPC); |
| 255 | SparcV9.emitFarCall(Addr, F); |
| 256 | #if 0 |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 257 | else { |
Misha Brukman | a1f1fea | 2003-07-29 19:00:58 +0000 | [diff] [blame] | 258 | // call CallTarget ;; invoke the callback |
| 259 | MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget); |
| 260 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call)); |
| 261 | delete Call; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 262 | |
Misha Brukman | a1f1fea | 2003-07-29 19:00:58 +0000 | [diff] [blame] | 263 | // nop ;; call delay slot |
| 264 | MachineInstr *Nop = BuildMI(V9::NOP, 0); |
| 265 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop)); |
| 266 | delete Nop; |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 267 | |
| 268 | addCallFlavor(CurrPC, ShortCall); |
Misha Brukman | a1f1fea | 2003-07-29 19:00:58 +0000 | [diff] [blame] | 269 | } |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 270 | #endif |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 271 | |
| 272 | SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 273 | return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */ |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 277 | SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm, |
| 278 | MachineCodeEmitter &M): TM(tm), MCE(M) |
| 279 | { |
| 280 | TheJITResolver = new JITResolver(*this, M); |
| 281 | } |
| 282 | |
| 283 | SparcV9CodeEmitter::~SparcV9CodeEmitter() { |
| 284 | delete TheJITResolver; |
| 285 | } |
| 286 | |
| 287 | void SparcV9CodeEmitter::emitWord(unsigned Val) { |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 288 | // Output the constant in big endian byte order... |
| 289 | unsigned byteVal; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 290 | for (int i = 3; i >= 0; --i) { |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 291 | byteVal = Val >> 8*i; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 292 | MCE.emitByte(byteVal & 255); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 293 | } |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 296 | unsigned |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 297 | SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg, |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 298 | MachineInstr &MI) { |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 299 | const TargetRegInfo &RI = TM.getRegInfo(); |
| 300 | unsigned regClass, regType = RI.getRegType(fakeReg); |
| 301 | // At least map fakeReg into its class |
| 302 | fakeReg = RI.getClassRegNum(fakeReg, regClass); |
| 303 | |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 304 | switch (regClass) { |
| 305 | case UltraSparcRegInfo::IntRegClassID: { |
| 306 | // Sparc manual, p31 |
| 307 | static const unsigned IntRegMap[] = { |
| 308 | // "o0", "o1", "o2", "o3", "o4", "o5", "o7", |
| 309 | 8, 9, 10, 11, 12, 13, 15, |
| 310 | // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", |
| 311 | 16, 17, 18, 19, 20, 21, 22, 23, |
| 312 | // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7", |
| 313 | 24, 25, 26, 27, 28, 29, 30, 31, |
| 314 | // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", |
| 315 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 316 | // "o6" |
| 317 | 14 |
| 318 | }; |
| 319 | |
| 320 | return IntRegMap[fakeReg]; |
| 321 | break; |
| 322 | } |
| 323 | case UltraSparcRegInfo::FloatRegClassID: { |
| 324 | DEBUG(std::cerr << "FP reg: " << fakeReg << "\n"); |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 325 | if (regType == UltraSparcRegInfo::FPSingleRegType) { |
| 326 | // only numbered 0-31, hence can already fit into 5 bits (and 6) |
| 327 | DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n"); |
| 328 | } else if (regType == UltraSparcRegInfo::FPDoubleRegType) { |
| 329 | // FIXME: This assumes that we only have 5-bit register fiels! |
| 330 | // From Sparc Manual, page 40. |
| 331 | // The bit layout becomes: b[4], b[3], b[2], b[1], b[5] |
| 332 | fakeReg |= (fakeReg >> 5) & 1; |
| 333 | fakeReg &= 0x1f; |
| 334 | DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n"); |
| 335 | } |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 336 | return fakeReg; |
| 337 | } |
| 338 | case UltraSparcRegInfo::IntCCRegClassID: { |
Misha Brukman | dfbfc57 | 2003-07-16 20:30:40 +0000 | [diff] [blame] | 339 | /* xcc, icc, ccr */ |
| 340 | static const unsigned IntCCReg[] = { 6, 4, 2 }; |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 341 | |
Misha Brukman | dfbfc57 | 2003-07-16 20:30:40 +0000 | [diff] [blame] | 342 | assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0]) |
| 343 | && "CC register out of bounds for IntCCReg map"); |
| 344 | DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n"); |
| 345 | return IntCCReg[fakeReg]; |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 346 | } |
| 347 | case UltraSparcRegInfo::FloatCCRegClassID: { |
| 348 | /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */ |
| 349 | DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n"); |
| 350 | return fakeReg; |
| 351 | } |
| 352 | default: |
| 353 | assert(0 && "Invalid unified register number in getRegType"); |
| 354 | return fakeReg; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 359 | // WARNING: if the call used the delay slot to do meaningful work, that's not |
| 360 | // being accounted for, and the behavior will be incorrect!! |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 361 | inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) { |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 362 | static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2, |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 363 | i7 = SparcIntRegClass::i7, o6 = SparcIntRegClass::o6, |
| 364 | o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0, |
| 365 | g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5; |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 366 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 367 | MachineInstr* BinaryCode[] = { |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 368 | // |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 369 | // Get address to branch into %g1, using %g5 as a temporary |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 370 | // |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 371 | // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5 |
| 372 | BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5), |
| 373 | // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1 |
| 374 | BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5), |
| 375 | // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word |
| 376 | BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5), |
| 377 | // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg |
| 378 | BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1), |
| 379 | // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1 |
| 380 | BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1), |
| 381 | // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1 |
| 382 | BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1), |
| 383 | // jmpl %g1, %g0, %o7 ;; indirect call on %g1 |
| 384 | BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7), |
| 385 | // nop ;; delay slot |
| 386 | BuildMI(V9::NOP, 0) |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 387 | }; |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 388 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 389 | for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) { |
| 390 | // This is where we save the return address in the LazyResolverMap!! |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 391 | if (i == 6 && F != 0) { // Do this right before the JMPL |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 392 | uint64_t CurrPC = MCE.getCurrentPCValue(); |
| 393 | TheJITResolver->addFunctionReference(CurrPC, F); |
| 394 | // Remember that this is a far call, to subtract appropriate offset later |
| 395 | TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall); |
| 396 | } |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 397 | |
Misha Brukman | 0897c60 | 2003-08-06 16:20:22 +0000 | [diff] [blame] | 398 | emitWord(getBinaryCodeForInstr(*BinaryCode[i])); |
| 399 | delete BinaryCode[i]; |
| 400 | } |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 404 | int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI, |
| 405 | MachineOperand &MO) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 406 | int64_t rv = 0; // Return value; defaults to 0 for unhandled cases |
| 407 | // or things that get fixed up later by the JIT. |
| 408 | |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 409 | if (MO.isVirtualRegister()) { |
Misha Brukman | 3339459 | 2003-06-06 03:35:37 +0000 | [diff] [blame] | 410 | std::cerr << "ERROR: virtual register found in machine code.\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 411 | abort(); |
| 412 | } else if (MO.isPCRelativeDisp()) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 413 | DEBUG(std::cerr << "PCRelativeDisp: "); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 414 | Value *V = MO.getVRegValue(); |
| 415 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 416 | DEBUG(std::cerr << "Saving reference to BB (VReg)\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 417 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 418 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 419 | } else if (const Constant *C = dyn_cast<Constant>(V)) { |
| 420 | if (ConstantMap.find(C) != ConstantMap.end()) { |
| 421 | rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 422 | DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 423 | } else { |
Misha Brukman | 3339459 | 2003-06-06 03:35:37 +0000 | [diff] [blame] | 424 | std::cerr << "ERROR: constant not in map:" << MO << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 425 | abort(); |
| 426 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 427 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 428 | // same as MO.isGlobalAddress() |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 429 | DEBUG(std::cerr << "GlobalValue: "); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 430 | // external function calls, etc.? |
| 431 | if (Function *F = dyn_cast<Function>(GV)) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 432 | DEBUG(std::cerr << "Function: "); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 433 | if (F->isExternal()) { |
| 434 | // Sparc backend broken: this MO should be `ExternalSymbol' |
| 435 | rv = (int64_t)MCE.getGlobalValueAddress(F->getName()); |
| 436 | } else { |
| 437 | rv = (int64_t)MCE.getGlobalValueAddress(F); |
| 438 | } |
| 439 | if (rv == 0) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 440 | DEBUG(std::cerr << "not yet generated\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 441 | // Function has not yet been code generated! |
| 442 | TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F); |
| 443 | // Delayed resolution... |
| 444 | rv = TheJITResolver->getLazyResolver(F); |
| 445 | } else { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 446 | DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 447 | } |
| 448 | } else { |
Misha Brukman | 3339459 | 2003-06-06 03:35:37 +0000 | [diff] [blame] | 449 | rv = (int64_t)MCE.getGlobalValueAddress(GV); |
Misha Brukman | de07be3 | 2003-06-06 04:41:22 +0000 | [diff] [blame] | 450 | if (rv == 0) { |
| 451 | if (Constant *C = ConstantPointerRef::get(GV)) { |
| 452 | if (ConstantMap.find(C) != ConstantMap.end()) { |
| 453 | rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
| 454 | } else { |
Misha Brukman | 8631ac4 | 2003-06-06 09:53:28 +0000 | [diff] [blame] | 455 | std::cerr << "Constant: 0x" << std::hex << (intptr_t)C |
Misha Brukman | de07be3 | 2003-06-06 04:41:22 +0000 | [diff] [blame] | 456 | << ", " << *V << " not found in ConstantMap!\n"; |
| 457 | abort(); |
| 458 | } |
| 459 | } |
| 460 | } |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 461 | DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 462 | } |
| 463 | // The real target of the call is Addr = PC + (rv * 4) |
| 464 | // So undo that: give the instruction (Addr - PC) / 4 |
| 465 | if (MI.getOpcode() == V9::CALL) { |
| 466 | int64_t CurrPC = MCE.getCurrentPCValue(); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 467 | DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n" |
Misha Brukman | 0870e97 | 2003-08-06 22:19:18 +0000 | [diff] [blame^] | 468 | << "curr PC: 0x" << std::hex << CurrPC << "\n"); |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 469 | int64_t CallInstTarget = (rv - CurrPC) >> 2; |
| 470 | if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) { |
| 471 | DEBUG(std::cerr << "Making far call!\n"); |
| 472 | // addresss is out of bounds for the 30-bit call, |
| 473 | // make an indirect jump-and-link |
| 474 | emitFarCall(rv); |
| 475 | // this invalidates the instruction so that the call with an incorrect |
| 476 | // address will not be emitted |
| 477 | rv = 0; |
| 478 | } else { |
| 479 | // The call fits into 30 bits, so just return the corrected address |
| 480 | rv = CallInstTarget; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 481 | } |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 482 | DEBUG(std::cerr << "returning addr: 0x" << rv << "\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 483 | } |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 484 | } else { |
| 485 | std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n"; |
| 486 | abort(); |
| 487 | } |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 488 | } else if (MO.isPhysicalRegister() || |
| 489 | MO.getType() == MachineOperand::MO_CCRegister) |
| 490 | { |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 491 | // This is necessary because the Sparc backend doesn't actually lay out |
| 492 | // registers in the real fashion -- it skips those that it chooses not to |
| 493 | // allocate, i.e. those that are the FP, SP, etc. |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 494 | unsigned fakeReg = MO.getAllocatedRegNum(); |
| 495 | unsigned realRegByClass = getRealRegNum(fakeReg, MI); |
| 496 | DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => " |
Misha Brukman | dfbfc57 | 2003-07-16 20:30:40 +0000 | [diff] [blame] | 497 | << realRegByClass << " (LLC: " |
| 498 | << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n"); |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 499 | rv = realRegByClass; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 500 | } else if (MO.isImmediate()) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 501 | rv = MO.getImmedValue(); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 502 | DEBUG(std::cerr << "immed: " << rv << "\n"); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 503 | } else if (MO.isGlobalAddress()) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 504 | DEBUG(std::cerr << "GlobalAddress: not PC-relative\n"); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 505 | rv = (int64_t) |
| 506 | (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()), |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 507 | MI, MO.isPCRelative()); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 508 | } else if (MO.isMachineBasicBlock()) { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 509 | // Duplicate code of the above case for VirtualRegister, BasicBlock... |
| 510 | // It should really hit this case, but Sparc backend uses VRegs instead |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 511 | DEBUG(std::cerr << "Saving reference to MBB\n"); |
Chris Lattner | 6856d11 | 2003-07-26 23:04:00 +0000 | [diff] [blame] | 512 | const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 513 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 514 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 515 | } else if (MO.isExternalSymbol()) { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 516 | // Sparc backend doesn't generate this (yet...) |
| 517 | std::cerr << "ERROR: External symbol unhandled: " << MO << "\n"; |
| 518 | abort(); |
| 519 | } else if (MO.isFrameIndex()) { |
| 520 | // Sparc backend doesn't generate this (yet...) |
| 521 | int FrameIndex = MO.getFrameIndex(); |
| 522 | std::cerr << "ERROR: Frame index unhandled.\n"; |
| 523 | abort(); |
| 524 | } else if (MO.isConstantPoolIndex()) { |
| 525 | // Sparc backend doesn't generate this (yet...) |
| 526 | std::cerr << "ERROR: Constant Pool index unhandled.\n"; |
| 527 | abort(); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 528 | } else { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 529 | std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 530 | abort(); |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Finally, deal with the various bitfield-extracting functions that |
| 534 | // are used in SPARC assembly. (Some of these make no sense in combination |
| 535 | // with some of the above; we'll trust that the instruction selector |
| 536 | // will not produce nonsense, and not check for valid combinations here.) |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 537 | if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 538 | return rv & 0x03ff; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 539 | } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 540 | return (rv >> 10) & 0x03fffff; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 541 | } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 542 | return (rv >> 32) & 0x03ff; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 543 | } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 544 | return rv >> 42; |
| 545 | } else { // (unadorned) val |
| 546 | return rv; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) { |
| 551 | Val >>= bit; |
| 552 | return (Val & 1); |
| 553 | } |
| 554 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 555 | bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 556 | MCE.startFunction(MF); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 557 | DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName() |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 558 | << ", address: " << "0x" << std::hex |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 559 | << (long)MCE.getCurrentPCValue() << "\n"); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 560 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 561 | // The Sparc backend does not use MachineConstantPool; |
| 562 | // instead, it has its own constant pool implementation. |
| 563 | // We create a new MachineConstantPool here to be compatible with the emitter. |
| 564 | MachineConstantPool MCP; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 565 | const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues(); |
| 566 | for (hash_set<const Constant*>::const_iterator I = pool.begin(), |
| 567 | E = pool.end(); I != E; ++I) |
| 568 | { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 569 | Constant *C = (Constant*)*I; |
| 570 | unsigned idx = MCP.getConstantPoolIndex(C); |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 571 | DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 572 | ConstantMap[C] = idx; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 573 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 574 | MCE.emitConstantPool(&MCP); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 575 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 576 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 577 | emitBasicBlock(*I); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 578 | MCE.finishFunction(MF); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 579 | |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 580 | DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n"); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 581 | ConstantMap.clear(); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 582 | |
| 583 | // Resolve branches to BasicBlocks for the entire function |
| 584 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 585 | long Location = BBLocations[BBRefs[i].first]; |
| 586 | unsigned *Ref = BBRefs[i].second.first; |
| 587 | MachineInstr *MI = BBRefs[i].second.second; |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 588 | DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location |
| 589 | << " in instr: " << std::dec << *MI); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 590 | for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) { |
| 591 | MachineOperand &op = MI->getOperand(ii); |
| 592 | if (op.isPCRelativeDisp()) { |
| 593 | // the instruction's branch target is made such that it branches to |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 594 | // PC + (branchTarget * 4), so undo that arithmetic here: |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 595 | // Location is the target of the branch |
| 596 | // Ref is the location of the instruction, and hence the PC |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 597 | int64_t branchTarget = (Location - (long)Ref) >> 2; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 598 | // Save the flags. |
| 599 | bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false; |
| 600 | if (op.opLoBits32()) { loBits32=true; } |
| 601 | if (op.opHiBits32()) { hiBits32=true; } |
| 602 | if (op.opLoBits64()) { loBits64=true; } |
| 603 | if (op.opHiBits64()) { hiBits64=true; } |
| 604 | MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed, |
| 605 | branchTarget); |
| 606 | if (loBits32) { MI->setOperandLo32(ii); } |
| 607 | else if (hiBits32) { MI->setOperandHi32(ii); } |
| 608 | else if (loBits64) { MI->setOperandLo64(ii); } |
| 609 | else if (hiBits64) { MI->setOperandHi64(ii); } |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 610 | DEBUG(std::cerr << "Rewrote BB ref: "); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 611 | unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI); |
| 612 | *Ref = fixedInstr; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | BBRefs.clear(); |
| 618 | BBLocations.clear(); |
| 619 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
| 623 | void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Misha Brukman | 0d60345 | 2003-05-27 22:41:44 +0000 | [diff] [blame] | 624 | currBB = MBB.getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 625 | BBLocations[currBB] = MCE.getCurrentPCValue(); |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 626 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){ |
| 627 | unsigned binCode = getBinaryCodeForInstr(**I); |
| 628 | if (binCode == (1 << 30)) { |
| 629 | // this is an invalid call: the addr is out of bounds. that means a code |
| 630 | // sequence has already been emitted, and this is a no-op |
| 631 | DEBUG(std::cerr << "Call supressed: already emitted far call.\n"); |
| 632 | } else { |
| 633 | emitWord(binCode); |
| 634 | } |
| 635 | } |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 638 | void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI, |
| 639 | bool isPCRelative) |
| 640 | { |
| 641 | if (isPCRelative) { // must be a call, this is a major hack! |
| 642 | // Try looking up the function to see if it is already compiled! |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 643 | if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) { |
| 644 | intptr_t CurByte = MCE.getCurrentPCValue(); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 645 | // The real target of the call is Addr = PC + (target * 4) |
| 646 | // CurByte is the PC, Addr we just received |
| 647 | return (void*) (((long)Addr - (long)CurByte) >> 2); |
| 648 | } else { |
| 649 | if (Function *F = dyn_cast<Function>(V)) { |
| 650 | // Function has not yet been code generated! |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 651 | TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 652 | cast<Function>(V)); |
| 653 | // Delayed resolution... |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 654 | return |
| 655 | (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V)); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 656 | |
| 657 | } else if (Constant *C = ConstantPointerRef::get(V)) { |
| 658 | if (ConstantMap.find(C) != ConstantMap.end()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 659 | return (void*) |
| 660 | (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 661 | } else { |
| 662 | std::cerr << "Constant: 0x" << std::hex << &*C << std::dec |
| 663 | << ", " << *V << " not found in ConstantMap!\n"; |
| 664 | abort(); |
| 665 | } |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 666 | } else { |
| 667 | std::cerr << "Unhandled global: " << *V << "\n"; |
| 668 | abort(); |
| 669 | } |
| 670 | } |
| 671 | } else { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 672 | return (void*)(intptr_t)MCE.getGlobalValueAddress(V); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 677 | #include "SparcV9CodeEmitter.inc" |