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